Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error - expected primary-expression before '.' token|

Tags:

c++

class

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.

like image 778
Vaaljan Avatar asked Sep 22 '13 09:09

Vaaljan


1 Answers

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.

like image 194
LihO Avatar answered Nov 04 '22 15:11

LihO