C ++ error - expected primary expression before '.' marker |

I just want to say that I'm still learning C ++, so I started with a module on classes and structures, and although I don't understand everything, I think I got a somewhat correct answer. The error that the compiler gives me is:

error: expected primary expression before '.' marker |

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 do not know why I should use mutators.

Not 100% sure how they work.

Please, help.

+4
source share
2 answers

In your class Exam : module , venue and date are private members, access to which is possible only within this class. Even if you change the access modifier to public :

 class Exam { public: string module,venue,date; } 

these are elements that are associated with specific objects (instances of this class), and not with the class definition itself (for example, static members). To use such elements you need an object:

 Exam e; e.date = "09/22/2013"; 

etc .. Also note that module,venue,date = ""; doesn’t change module and venue in any way, what did you really mean:

 module = venue = date = ""; 

although std::string objects are initialized automatically for an empty string, so this string is useless anyway.

+10
source

You need a mutators function to accept user input to store variables, place and date in your module

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 
0
source

All Articles