[英]Expected primary-expression before '{' token error
Followed codes runs properly with Eclipse but when i run on Dev C++ IDE i am getting followed error; 后面的代码可以在Eclipse中正常运行,但是当我在Dev C ++ IDE上运行时,我会遇到错误;
City.cpp:6: error: expected primary-expression before '{' token City.cpp:6:错误:在'{'标记之前预期的primary-expression
City.h City.h
#include <string>
using namespace std;
#ifndef CITY_H
#define CITY_H
class City
{
public:
City();
string arrCity[10];
};
#endif // CITY_H
City.cpp City.cpp
#include <string>
#include "City.h"
City::City()
{
arrCity[10] = {"Tbilisi", "Batumi", "Kutaisi", "Gori", "Poti"};
}
arrCity[10] = {"Tbilisi", "Batumi", "Kutaisi", "Gori", "Poti"};
doesn't do what you expected. 不按你的意愿行事。 It's trying to assign
arrCity[10]
(ie a std::string
) by a braced initializer list; 它试图通过一个支撑的初始化列表来分配
arrCity[10]
(即一个std::string
); that won't work. 这是行不通的。 And it's getting out of the bound of the array.
它已经脱离了数组的界限。
Note that array can't be assigned directly, you can use member intializer list to initialize it like: 请注意,无法直接分配数组,您可以使用成员初始化列表将其初始化为:
City::City() : arrCity {"Tbilisi", "Batumi", "Kutaisi", "Gori", "Poti"}
{
}
or use default member initializer: 或使用默认成员初始化程序:
class City
{
public:
City();
string arrCity[10] = {"Tbilisi", "Batumi", "Kutaisi", "Gori", "Poti"};
// or
string arrCity[10] {"Tbilisi", "Batumi", "Kutaisi", "Gori", "Poti"};
};
Note that both the above solutions needs C++11 supports, otherwise, you might need to assign every element one by one in the constructor's body. 请注意,上述两种解决方案都需要C ++ 11支持,否则,您可能需要在构造函数体中逐个分配每个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.