Hi guys,
I've came across a problem now; There is a class I've made in order to perform my H.W. This is a header file and it won't compile, I get this error:
C2460. After I've checked about this error online, I jave found that it says:
"'identifier1' : uses 'identifier2', which is being defined
A class or structure (identifier2) is declared as a member of itself (identifier1). Recursive definitions of classes and structures are not allowed.
Instead, use a pointer reference in the class.
". I didn't understand their explanation of this error, this is the code the use to show the problem:
1 2 3 4 5 6 7 8 9
|
// C2460.cpp
class C {
C aC; // C2460
};
//The code fixing the problem:
// C2460.cpp
class C {
C * aC; // OK
};
|
How come the first code creates recursive definition?
(I've change the bolded line the code to: Person *next, *back; and it worked, why?)
Cheers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
int age;
string _pname;
string _last_name;
public:
Person(); //default C'tor
Person(string _pname_, string _last_name_, int _age); //non-defualt C'tor
Person* next, back; //pointers to the next and the back object in the list
}
|