主.cpp(12):错误 C2061:语法错误:标识符"vec1"
main.cpp(12): error C2061: syntax error: identifier 'vec1'
我不知道我的代码出了什么问题,但它给出了这个错误。
主.cpp(12(:错误 C2061:语法错误:标识符"vec1"。
在构造函数中声明这样的向量没有问题,但我似乎不能作为类成员这样做。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class vecList
{
private:
int row, column, vec1, vec2, part;
vector< vector <int> > vEC(vec1,vector<int>(vec2));
public:
vecList(int vRow, int vColumn, int vPart)
{
cin >> vRow;
cin >> vColumn;
row = vRow;
column = vColumn;
vector< vector <int> > vEC(vColumn, vector<int>(vRow));
for(int i = 0; i < vColumn; i++)
{
cout << endl << "|";
for(int j = 0; j < vRow; j++)
{
vEC[i][j] = 14;
cout << vEC[i][j] << "|";
}
}
}
void getValue()
{
int a, b;
a = column;
b = row;
cout << a << b;
}
};
int main()
{
vecList a(2,4,6);
a.getValue();
a.getVect();
}
在你的类声明中:
vector< vector <int> > vEC(vec1,vector<int>(vec2));
不是数据成员的有效声明。 一是因为语法无效,二是因为构造成员时vec1
和vec2
的值未知。
尝试更多类似的东西:
class vecList
{
private:
int row, column, part;
vector< vector<int> > vEC;
public:
vecList(int vRow, int vColumn, int vPart)
{
/* these values are provided by the caller, so you shouldn't
be reading them from std::cin here. That is the caller's
responsibility to do instead...
cin >> vRow;
cin >> vColumn;
*/
row = vRow;
column = vColumn;
part = vPart;
vEC.resize(vColumn);
for(int i = 0; i < vColumn; i++)
{
vEC[i] = vector<int>(vRow);
cout << endl << "|";
for(int j = 0; j < vRow; j++)
{
vEC[i][j] = 14;
cout << vEC[i][j] << "|";
}
}
}
...
};
可以简化为:
class vecList
{
private:
int row, column, part;
vector< vector <int> > vEC;
public:
vecList(int vRow, int vColumn, int vPart)
: row(vRow), column(vColumn), part(vPart), vEC(vColumn, vector<int>(vRow, 14))
{
for(int i = 0; i < vColumn; i++)
{
cout << endl << "|";
for(int j = 0; j < vRow; j++)
{
cout << vEC[i][j] << "|";
}
}
}
...
};
首先,您没有主 - 因此,您的程序无法运行。 其次,您需要在 vEC 的描述中定义 vec1 变量的类型。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class vecList
{
private:
int row, column, vec2, part;
vector< vector <int> > vEC(int(vec1), vector<int>(vec2));
public:
vecList(int vRow, int vColumn, int vPart)
{
cin >> vRow;
cin >> vColumn;
row = vRow;
column = vColumn;
vector< vector <int> > vEC(vColumn, vector<int>(vRow));
for (int i = 0; i<vColumn; i++)
{
cout << endl << "|";
for (int j = 0; j<vRow; j++)
{
vEC[i][j] = 14;
cout << vEC[i][j] << "|";
}
}
}
void getValue() {
int a, b;
a = column;
b = row;
cout << a << b;
}
};
int main()
{
/*...Your code here...*/
}
最好的主意是让构造函数初始化向量。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class vecList
{
public:
vecList(int vRow, int vColumn, int vPart)
:
vec1(vColumn),
vec2(vRow),
part(vPart),
vEC(vec1, vector<int>(vec2))
{
for(int i = 0; i < vColumn; i++)
{
cout << endl << "|";
for(int j = 0; j < vRow; j++)
{
vEC[i][j] = 14;
cout << vEC[i][j] << "|";
}
cout << endl;
}
}
void getValue()
{
int a, b;
a = column;
b = row;
cout << a << b;
}
private:
int row, column, vec1, vec2, part;
vector< vector <int> > vEC;
};
int main()
{
vecList veclist(3, 4, 2);
veclist.getValue();
return 0;
}
相关文章:
- 设置 Visual Studio for MPI: 找不到标识符错误
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- C++:枚举:错误:应使用标识符而不是"}"
- 我的代码中有错误,未声明的标识符
- 错误:在 C++ 'std'使用未声明的标识符
- 从子类访问模板类成员时出现"找不到标识符"错误
- 为什么更改包含 psapi.h 的顺序会产生编译错误?(标识符 BOOL 未定义)
- 未声明的标识符错误,但变量定义正确 (?)
- C++ 多态构造函数错误;标识符未定义
- 虚幻引擎4:UShape组件给出错误"标识符未定义"。我能做什么?
- 如何修复可视化工作室中的"C2061语法错误标识符"堆栈?
- C++结构错误 - 错误 C2061:语法错误:标识符
- 在给定的代码中获取错误(标识符未定义)
- 视觉 在调用它时用 c++ 创建一个方法后,我得到的错误标识符未找到
- C++Visual Studio类错误标识符字符串未定义
- C++错误C2061:语法错误:标识符
- 智能感知错误标识符"emlrtStack"未定义
- 错误:标识符未在c++中定义
- Visual Studio错误:语法错误:标识符
- 错误 C2061:语法错误:标识符,但已包含头文件