我只是想说我还在学习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;
}
Run Code Online (Sandbox Code Playgroud)
该问题要求使用访问器和Mutators,但我不知道为什么我应该使用Mutators.
不是100%肯定他们如何工作.
Lih*_*ihO 11
在您的class Exam:module,venue并且date是私有成员,只能在此类的范围内访问.即使您将访问修饰符更改为public:
class Exam {
public:
string module,venue,date;
}
Run Code Online (Sandbox Code Playgroud)
那些仍然是与具体对象(此类的实例)相关联的成员,而不是类定义本身(就像static成员一样).要使用此类成员,您需要一个对象:
Exam e;
e.date = "09/22/2013";
Run Code Online (Sandbox Code Playgroud)
另请注意,module,venue,date = "";不会修改module,venue无论如何,您实际意味着:
module = venue = date = "";
Run Code Online (Sandbox Code Playgroud)
虽然std::string对象被自动初始化为空字符串,但是这条线无论如何都是无用的.
| 归档时间: |
|
| 查看次数: |
86805 次 |
| 最近记录: |