[英]C++ Error - expected primary-expression before '.' token|
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. 我只是想说我还在学习C ++所以我从关于类和结构的模块开始,虽然我不了解所有内容,但我觉得我有点对了。 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. 该问题要求使用访问器和Mutators,但我不知道为什么我应该使用Mutators。
Not 100% sure how they work anyways. 不是100%肯定他们如何工作。
In your class Exam
: module
, venue
and date
are private members, which can be access only within the scope of this class. 在您的class Exam
: module
, venue
和date
是私人成员,只能在本课程范围内访问。 Even if you change the access modifier to public
: 即使您将访问修饰符更改为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). 那些仍然是与具体对象(这个类的实例)相关联的成员而不是类定义本身(就像static
成员一样)。 To use members of this kind, you need an object: 要使用此类成员,您需要一个对象:
Exam e;
e.date = "09/22/2013";
etc. Also note that module,venue,date = "";
还要注意module,venue,date = "";
doesn't modify module
and venue
in any way, what you actually meant was: 不以任何方式修改module
和venue
,你实际意味着:
module = venue = date = "";
although std::string
objects are initialized to empty string automatically, thus this line is useless anyway. 虽然std::string
对象被自动初始化为空字符串,但是这条线无论如何都是无用的。
You need the mutators function to accept input from the user to store in your variables module,venue and date 您需要mutators函数来接受来自用户的输入,以存储在变量模块,场地和日期中
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.