class Rectangle {
   private:
			int num;
public:
	int method1(int,int);
}classOne;

int Rectangle::method1(int num1,int num2)
{
	classOne.num = (num1 * num2);
	return (classOne.num);
}

int main()
{
	Rectangle* one = new Rectangle();
	
	cout << (one->method1(2,10));

NewClass* newClassPointer = new NewClass();
//I want to access NewClass here....

}
NewClass::NewClass()
{
method1();
}
void method1()
{
	cout << "in NewClass";
}

};

I have another added another class in to the project, in a seperate file, but I cannot seem to access this class from my code??

I think I have added a constructor in NewClass and would like to call this constructor to call method1 in turn from Rectangle.

Any help please???

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Rectangle {
public:
			int num;
	
	int method1(int,int);
}classOne;

int Rectangle::method1(int num1,int num2)
{
	classOne.num = (num1 * num2);
	return (classOne.num);
}
int SquareClass::method2(int num1,int num2)
{
	classTwo.num2 = (num1* num2);
	return (classTwo.num2);
}
int main()
{
	Rectangle* one = new Rectangle();
	
	cout << (one->method1(2,10));


	SquareClass* squareClassPointer = new SquareClass();
	cout << (squareClassPointer->method2(5,5));

}

class SquareClass
{
public: 
	int num2;

	int method2(int,int);
}classTwo;

I also have this with 2 classes in one file. But I am getting many errors...it works fine without Square class and it's function but when I add it in, it does not want to compile, giving many errors about undeclared identifiers????

My error, must be a compiler bug, I often find that visual C++ will not compile properly, then you delete something, try again and it will compile, silly thing...does anyone else have this problem?

silly thing...does anyone else have this problem?

In older versions I found that I needed to leave a blank line underneath the last closing brace or it would not compile.

So, is it fixed?
Do I need to make some sample code? :)

Edited 5 Years Ago by thines01: n/a

Well, in your second file (if the code is really ordered like this), you have declared your SquareClass at the end of the file, after the main function, which causes the "undeclared identifiers" errors. The compiler reads the file from the top to the bottom, so don't expect it to "know" about SquareClass until the moment it reads its declaration. Suggested solutions are:

  • Move the declaration of SquareClass after Rectangle class and before coding method2. That will help the compiler know about the class when he will be asked to initialise an object or point to it.
This article has been dead for over six months. Start a new discussion instead.