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.
#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)
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.
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?
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!
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.