我不知道我的代码有什么问题,但它会出现此错误。
main.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();
}
答案 0 :(得分:0)
在你的班级声明中:
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] << "|";
}
}
}
...
};
答案 1 :(得分:0)
首先,你没有主 - 因此,你的程序无法运行。 其次,你需要在vEC的decliration中定义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...*/
}
答案 2 :(得分:0)
最好的想法是让构造函数初始化向量。
#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;
}