C2460 error, VS2012

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
}
Last edited on
1
2
3
4
5
6
class Person{ 
        // ...
        // pointers to the next and the back (previous?) object in the list
	Person* next ;
        Person* back ; // (previous?)
}
Last edited on
Why this separation makes it work?
Why defining 2 points in one code-line is invalid?
Two pointers on one line: Person *next, *back; //pointers
Never use such notation as

Person* next, back;

in C++.

It only confuses uses.

Instead use

Person *next, *back;

The meaning of

Person* next, back; in C++ is different compared with for example C#.

In C# Person* means that all identifiers in the identifier list will have type Person*.
In C++ Person* means that only the next identifier in the identifier list will have type Person*. All other identifiers in the identifier list will have type Person,

Now you made sure yourself that it is simply a bad style to use such notation in C++. Always use

Person *next, *back;

in C++

and

Person* next, back;

in C#.
Topic archived. No new replies allowed.