Reputation: 832
I just want to say that I am still learning C++ so I started with the module about Classes and Structures, and while I do not understand everything, I think I got it somewhat right. The error the compiler keeps giving me is:
error: expected primary-expression before '.' token
Here is the Code:
#include <iostream>
using namespace std;
class Exam{
private:
string module,venue,date;
int numberStudent;
public:
//constructors:
Exam(){
numberStudent = 0;
module,venue,date = "";
}
//accessors:
int getnumberStudent(){ return numberStudent; }
string getmodule(){ return module; }
string getvenue(){ return venue; }
string getdate(){ return date; }
};
int main()
{
cout << "Module in which examination is written"<< Exam.module;
cout << "Venue of examination : " << Exam.venue;
cout << "Number of Students : " << Exam.numberStudent;
cout << "Date of examination : " << Exam.date
<< endl;
return 0;
}
The Question asked to use accessors and Mutators, But I don't know why I should use the Mutators.
Not 100% sure how they work anyways.
Upvotes: 7
Views: 113916
Reputation: 1
You need the mutators function to accept input from the user to store in your variables module,venue and date
EXAMPLE:
void setdetails()
{
cin.ignore();
cout<<"Please Enter venue"<<endl;
getline(cin,venue);
cout<<"Please Enter module"<<endl;
getline(cin,module);
}//end of mutator function
Upvotes: -1
Reputation: 42083
In your class Exam
: module
, venue
and date
are private members, which can be access only within the scope of this class. Even if you change the access modifier to public
:
class Exam {
public:
string module,venue,date;
}
those are still members that are associated with a concrete objects (instances of this class) rather than the class definition itself (like static
members would be). To use members of this kind, you need an object:
Exam e;
e.date = "09/22/2013";
etc. Also note that module,venue,date = "";
doesn't modify module
and venue
in any way, what you actually meant was:
module = venue = date = "";
although std::string
objects are initialized to empty string automatically, thus this line is useless anyway.
Upvotes: 14