我必须创建一个内部定义为短值 vector 的类。 vector 初始大小为 40。现在,我必须创建一个构造函数,将用户输入的长整数转换为类对象。否则, vector 的所有元素都应为 0。
我尝试编写代码,但可以写到这个级别,但现在我卡住了。 附言我是c++初学者
class HugeInteger
{
public:
HugeInteger (long = 0);
void output (ostream& outs);
private:
vector<short> v;
};
/*HugeInteger::HugeInteger (long = 0)
{
int i=0;
for (i=0;i<40;i++)
{
v.push_back (0);
}
}*/
void HugeInteger ::output (ostream& outs)
{
int i = 0;
outs << "Values in the vector are initialized to" << endl;
while(i < 40)
{
outs << v[i] << "\t";
i++;
}
}
int main()
{
long integer;
cout << "Enter a long integer" << endl;
// for(int i=0;i<40;i++)
cin >> integer;
HugeInteger test (integer);
test.output(cout);
return 0;
}
出现以下错误:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall HugeInteger::HugeInteger(long)"(??0HugeInteger@@QAE@J@Z) 在函数_main中引用
最佳答案
默认参数在声明中显式显示,而不是在定义中。从定义中删除它,例如
class HugeInteger
{
public:
HugeInteger (long = 0);
void output (ostream& outs);
private:
vector<short> v;
};
HugeInteger::HugeInteger (long) <--
{
}
关于c++ - 使用构造函数c++将长整数转换为类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26070118/