Programming Error C2597 - What's going on?

The problem in my code is that its telling me that some private attributes of my class are being illegally referenced. I was hoping that someone here might be able to help me resolve this problem. Thank you for any help you can provide, I've looked all over using Google to try and find a solution with no luck.

Person.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _PERSON_H
#define _PERSON_H
#include "apstring.h"
#include "apvector.h"
#include<iostream.h>

class Person
{
	private:
		apstring name;
		int ID;
		int age;
	public:
		Person();
		Person(apstring, int, int);
		apstring getName();
		int getID();
		int getAge();
		void modName(apstring);
		void modID(int);
		void modAge(int);
		void modAll(apstring, int, int);
		void showall();
		static void getInfo();
};
#include "Person.cpp"
#endif 


Person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Person.h"
#include "apstring.h"
#include "apvector.h"
#include <iostream.h>

//default constructor
Person::Person()
{
	name = "";
	ID = 0;
	age = 0;
}
//nondefault constructor
Person::Person(apstring n, int i, int a)
{
	name = n;
	ID = i;
	age = a;
}
//accessors
apstring Person::getName()
{
	return name;
}
int Person::getID()
{
	return ID;
}
int Person::getAge()
{
	return age;
}
//modifiers
void Person::modName(apstring n)
{
	name = n;
}
void Person::modID(int i)
{
	ID = i;
}
void Person::modAge(int a)
{
	age = a;
}
//show all
void Person::showall()
{
	cout<<"Name : "<<name<<"\n";
	cout<<"ID : "<<ID<<"\n";
	cout<<"Age : "<<age<<"\n";
}
//getinfo
void Person::getInfo()
{
	cout<<"Type in the name of this person\n";
	cin>>name; //Error One
	cout<<"Type in the ID of this person\n";
	cin>>ID; // Error Two
	cout<<"Type in the age of this person\n";
	cin>>age; // Error Two
}

void Person::modAll(apstring n, int i, int a) {
	modAge(a);
	modID(i);
	modName(n);
}



Here is my error(s) message:
1
2
3
4
5
6
7
8
Compiling...
DistrictDriver.cpp
e:\school district project\dist 12-16-2011\a\district\person.cpp(57) : error C2597: illegal reference to data member 'Person::name' in a static member function
e:\school district project\dist 12-16-2011\a\district\person.cpp(59) : error C2597: illegal reference to data member 'Person::ID' in a static member function
e:\school district project\dist 12-16-2011\a\district\person.cpp(61) : error C2597: illegal reference to data member 'Person::age' in a static member function
Error executing cl.exe.

District.exe - 3 error(s), 0 warning(s)
Why is getInfo() static? I might be missing something, but I don't see a reason for it to be static at all.

-Albatross
Last edited on
only the static member are allowed in the static function .
If I create getInfo() as a non-static subroutine it gives me errors referencing istream and ostream (int iostream.h) and the class that we use for creating and using strings apstring.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall apstring::~apstring(void)" (??1apstring@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: class apstring const & __thiscall apstring::operator=(class apstring const &)" (??4apstring@@QAEABV0@ABV0@@Z)
main.obj : error LNK2001: unresolved external symbol "public: class apstring const & __thiscall apstring::operator=(char const *)" (??4apstring@@QAEABV0@PBD@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall apstring::apstring(void)" (??0apstring@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall apstring::apstring(class apstring const &)" (??0apstring@@QAE@ABV0@@Z)
main.obj : error LNK2001: unresolved external symbol "class ostream & __cdecl operator<<(class ostream &,class apstring const &)" (??6@YAAAVostream@@AAV0@ABVapstring@@@Z)
main.obj : error LNK2001: unresolved external symbol "class istream & __cdecl operator>>(class istream &,class apstring &)" (??5@YAAAVistream@@AAV0@AAVapstring@@@Z)
Debug/trial.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.

trial.exe - 8 error(s), 0 warning(s)
Okay, that's actually better. Now you're getting linker errors, which means you got further through the compilation process.

It seems that apstring has a lot of things that aren't implemented, namely:
1. A destructor.
2. Assignment operators (both from C-style strings and other apstrings)
3. Constructors (both default and copy).
4. >> and << operators to use with iostreams.


Are you remembering to link against the file that defines apstring's members? Also, why isn't your professor (I assume) using the std::string class?

-Albatross
Last edited on
It looks like a class issue, but you probobly knew that already.
With a little nudge from my teacher I figured out the problem. In the overall project I accidently included the wrong files (.h instead of .cpp), after I changed what I had included in the project, my errors went away. Something else that is new and strange has come up but its more a Windows thing not a C++ thing I believe. Thank you for all your help!


-- Joe
Whoa, whoa, whoa. What?

Did your teacher tell you to #include a .cpp file? O_o

-Albatross
Last edited on
Yes, but it was my driver file. When I created my project I had way too many files included and they were the incorrect files. We got it worked out though.
You compiler cpp files, you don't include them. While including them in only one cpp file has the same effect, that doesn't mean it isn't wrong. Things that work correctly can be wrong ;)
Maybe I was using the wrong terminology, which I probably was. I had to add my driver file to my project. The program runs as intended, the .exe itself is giving me issues with Windows and I don't know what would cause that, but that's a totally different problem.
Topic archived. No new replies allowed.