繁体   English   中英

C ++错误 - 在'。'之前预期的primary-expression 令牌|

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

我只是想说我还在学习C ++所以我从关于类和结构的模块开始,虽然我不了解所有内容,但我觉得我有点对了。 编译器给我的错误是:

error: expected primary-expression before '.' token

这是代码:

#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;
}

该问题要求使用访问器和Mutators,但我不知道为什么我应该使用Mutators。

不是100%肯定他们如何工作。

在您的class Exammodulevenuedate是私人成员,只能在本课程范围内访问。 即使您将访问修饰符更改为public

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

那些仍然是与具体对象(这个类的实例)相关联的成员而不是类定义本身(就像static成员一样)。 要使用此类成员,您需要一个对象:

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

还要注意module,venue,date = ""; 不以任何方式修改modulevenue ,你实际意味着:

module = venue = date = "";

虽然std::string对象被自动初始化为空字符串,但是这条线无论如何都是无用的。

您需要mutators函数来接受来自用户的输入,以存储在变量模块,场地和日期中

例:

void setdetails()
{
    cin.ignore();
    cout<<"Please Enter venue"<<endl;
    getline(cin,venue);
    cout<<"Please Enter module"<<endl;
    getline(cin,module);
}//end of mutator function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM