0
votes

I have a scenario in which i am not able to create a class object inside another class. The scenario is as follows:

class base
{
public:
base();
~base();
int getbase(void){return baseVar;}
void setbase(int arg){baseVar = arg;}
private:
int baseVar;
};

class derived1 : public base
{
public:
derived1();
~derived1();
int getbase(void);
void setbase(int arg);
private:
int derVar1;
};

class derived2 : public base
{
public:
derived2();
~derived2();
int getbase(void);
void setbase(int arg);
private:
int derVar2;
};

class container
{
public:
container();
~container();

derived1 derObj1;
derived2 derObj2;

private:
int containerVar;
};

class usage
{
public:
usage();
~usage();
private:
container containerObj; // compilation error
int usegeVar;
};

compilation error is : error C2146: syntax error : missing ';' before identifier 'containerObj' error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C4430: missing type specifier - int assumed. Note: C++ does not support default-int i am using vc++

I request some one how to rectify this compilation error? Thanks In advance.

1
Have you included the header file defining class container in file which defines class usage? The error means that compiler cannot see defintion of type container. - Alok Save
As we used to say in the Air Force: CND (could not duplicate). It compiled with no errors with both VC++10 and gcc 4.7. - Jerry Coffin

1 Answers

0
votes

Copy an pasted your code on an empty VisualC++ 2k8 project and is OK!