C ++编译时错误:数字常量之前的预期标识符 [英] C++ compile time error: expected identifier before numeric constant
本文介绍了C ++编译时错误:数字常量之前的预期标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我读过其他类似的帖子,但我不明白我做错了什么。我想我的声明是正确的。我甚至试图宣布没有大小,但即使这是行不通的。怎么了?
我的代码是:
#include< vector>
#include< string>
#include< sstream>
#include< fstream>
#include< cmath>
使用namespace std;
vector< string> v2(5,null);
vector<矢量<串GT; > v2d2(20,V2);
class属性//属性和熵计算
{
vector< string>名(5); //这两行错误
vector< int> VAL(5,0);
public:
属性(){}
int total,T,F;
};
int main()
{
属性属性;
返回0;
解决方案
您不能这样做:
vector< string>名(5); //这两行错误
vector< int> VAL(5,0);
在方法外的类中。
您可以在声明时初始化数据成员,但不能使用()
括号:
class Foo {
vector< string> name = vector< string>(5);
vector< int> VAL {矢量< INT>(5,0)};
};
在C ++ 11之前,您需要首先声明它们,然后在构造函数中初始化它们
class Foo {
vector< string>名称;
vector< int> VAL;
public:
Foo():name(5),val(5,0){}
};
I have read other similar posts but I just don't understand what I've done wrong. I think my declaration of the vectors is correct. I even tried to declare without size but even that isn't working.What is wrong?? My code is:
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
using namespace std;
vector<string> v2(5, "null");
vector< vector<string> > v2d2(20,v2);
class Attribute //attribute and entropy calculation
{
vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);
public:
Attribute(){}
int total,T,F;
};
int main()
{
Attribute attributes;
return 0;
}
解决方案
You cannot do this:
vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);
in a class outside of a method.
You can initialize the data members at the point of declaration, but not with ()
brackets:
class Foo {
vector<string> name = vector<string>(5);
vector<int> val{vector<int>(5,0)};
};
Before C++11, you need to declare them first, then initialize them e.g in a contructor
class Foo {
vector<string> name;
vector<int> val;
public:
Foo() : name(5), val(5,0) {}
};
这篇关于C ++编译时错误:数字常量之前的预期标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文