主.cpp(12):错误 C2061:语法错误:标识符"vec1"

main.cpp(12): error C2061: syntax error: identifier 'vec1'

本文关键字:错误 标识符 vec1 语法 C2061 cpp      更新时间:2023-10-16

我不知道我的代码出了什么问题,但它给出了这个错误。

主.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));

不是数据成员的有效声明。 一是因为语法无效,二是因为构造成员时vec1vec2的值未知。

尝试更多类似的东西:

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;
}