C++ 错误 - 令牌之前的预期主表达式'.'|
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;
}
问题要求使用访问器和突变器,但我不知道为什么我应该使用突变器。
无论如何,不是 100% 确定它们是如何工作的。
在你的class Exam
中:module
、venue
和date
是私有成员,只能在此类的范围内访问。即使您将访问修饰符更改为 public
:
class Exam {
public:
string module,venue,date;
}
这些仍然是与具体对象(此类的实例)相关联的成员,而不是与类定义本身(如static
成员一样)。若要使用此类成员,需要一个对象:
Exam e;
e.date = "09/22/2013";
等。另请注意,module,venue,date = "";
不会以任何方式修改module
和venue
,您的实际意思是:
module = venue = date = "";
尽管std::string
对象会自动初始化为空字符串,但无论如何,此行都是无用的。
您需要突变器函数来接受用户的输入以存储在变量模块、地点和日期中
例:
void setdetails()
{
cin.ignore();
cout<<"Please Enter venue"<<endl;
getline(cin,venue);
cout<<"Please Enter module"<<endl;
getline(cin,module);
}//end of mutator function
相关文章:
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++ 标记之前预期主表达式'('错误
- 正则表达式中带有卷轴大括号的正则表达式错误
- C++初学者错误:主要表达式错误
- 代数树表达式C++错误:未解析的外部符号"Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."
- 使用多线程处理对象数组 - 无效使用 void 表达式错误
- 为什么 if constexpr 不会使这个核心常量表达式错误消失?
- 提升正则表达式错误:未初始化的提升::match_results
- C++比较表达式错误
- C++ "主表达式"错误
- C++ 正则表达式错误(未处理的异常)
- 'this'不能用于常量表达式错误 (C++)
- 代码块引发有关for_each的预期主表达式错误
- Boost Regex中的命名子表达式错误
- C++之前的主要表达式错误
- Ubuntu 上的正则表达式错误
- C++正则表达式错误
- 理解常量表达式错误
- 成员函数和预期的主表达式错误
- Xcode中应有表达式错误