I’m currently getting started in C++. For the homework I’m currently doing, I have to define a number of classes in one header file. I’m not sure If I’m doing this right. Here is sample of what I’m trying to do.
//classOne.h
class classOne{
public:
classOne();
~classOne();
class classInsideClass{
public:
classInsideClass
void hello();
void print();
};
}
(I have skipped some code in this sample, like constructor for classOne)
//classOne.cpp
classOne::classInsideClass::classInsideClass(){}
classOne::classInsideClass::hello(){
cout << ""Hello <<endl;
}
//main.cpp
classOne callingClass;
callingClass.classInsideClass.hello;
I have defined a class inside classOne’s header file. And I have created the functions for the this classInsideClass, inside the classOne’s cpp. Is this the right way of saying, classInsideClass belongs to classOne, or am I not allowed to do this?
Am I calling the functions of classInsideClass correctly in main.cpp? When I try to run this, I get following error;
error:invalid use of ‘class classOne::classInsideClass
If I don’t try and call a function of classInsideClass in main.cpp, it complies fine.
Thanks in advance.
classInsideClassis a type inside ofclassOne, not an object. If you want to callclassInsideClass::hello(), you need an actual instance ofclassInsideClass: