C++ 错误 - '.' 之前的预期主表达式 令牌|

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18942205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:21:24  来源:igfitidea点击:

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

c++class

提问by Vaaljan

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:

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

error: expected primary-expression before '.' token

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.

不是 100% 确定它们是如何工作的。

回答by LihO

In your class Exam: module, venueand dateare private members, which can be access only within the scope of this class. Even if you change the access modifier to public:

在你的class Exammodulevenue并且date是私有成员,这只能是这个类的范围内访问。即使您将访问修饰符更改为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 staticmembers would be). To use members of this kind, you need an object:

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

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

etc. Also note that module,venue,date = "";doesn't modify moduleand venuein any way, what you actually meant was:

此外,请注意module,venue,date = "";不修改module,并venue以任何方式,你实际的意思是:

module = venue = date = "";

although std::stringobjects are initialized to empty string automatically, thus this line is useless anyway.

尽管std::string对象会自动初始化为空字符串,因此无论如何这一行是无用的。

回答by Ranalee B.

You need the mutators function to accept input from the user to store in your variables module,venue and date

您需要 mutators 函数来接受来自用户的输入以存储在您的变量 module、venue 和 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