-
Help! Error!!!!
Code:
#include <iostream>
#include <string>
using namespace std;
class course
{
private:
string name;
int credits;
int total;
public:
string getinfo()
{
int total;
cout << "Please ask student the number of courses taken: " << endl;
cin >> total;
cout << "Test print of " << total << endl;
}
void printIt()
{
cout<<"Total courses: "<<total<<endl;
}
~course()
{
cout<<"Destructor is called " <<endl;
}
};
class registration:public course
{
private:
char *ptr;
int credits;
string classes;
string total;
public:
void printinfo();
registration();
// ~registration();
void printIt()
{
course::printIt();
}
~registration()
{
cout<<"Destructor is called " <<endl;
}
};
registration::registration()
{
cout << "Enter class information that student is signing up for." << endl;
getline(cin,classes);
cout << "Please enter the total number of credits the student is signed up for." <<endl;
cin >> credits;
};
/*registration::~registration()
{
cout << "Student x is signed up for: " << classes << endl;
cout << "Total number of credits signed up for = " << credits << endl;
};*/
void registration::printinfo()
{
cout << "Student x signed up for: " << total << endl;
};
int main()
{
course *p;
string id;
registration *ptr;
p.printIt(); (THIS IS THE PROBLEM LINE)
cout<<"Ask student of courses she or he is registering."<<endl;
getline(cin,id);
return 0;
}
-
what is the error? what compiler?
-
im compiling in visual studio C++ its telling me " : error C2228: left of '.printIt' must have class/struct/union type" i dont understand what its asking for! I'm really new at this and im not very good with solving errors!!!
-
p is a pointer, so you should use the -> operator:
-
Thanks that cleared up my errors, but no i have a warning for the PTR.
-
Equivalently, p->printIt() is the same as (*p).printIt() though the first is prefered.
Helps to know what the warning is, but my guess is that it is just mentioning that you have an unused variable. This just means that ptr never actually does anything, and does not need to be there.
-
Yeah it is saying that ptre is an unused variable, BUT when i run the program with the warning VISUAL STUDIO CRASHES!! Somethings deffinitly wrong!! i just dont understand there has to e something im not seeing!!!
Thanks!
-
The pointer of p is not pointing to anything. You need to initialize the pointer first.
Code:
course* p = new course;
...
delete p;
And DON'T forget to delete the memory used by new before the program exits.