c++ - 使用构造函数c++将长整数转换为类对象

标签 c++ class vector constructor

我必须创建一个内部定义为短值 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/

相关文章:

c++ - 分配器无状态意味着什么?

c++ - C++中类的隐式成员函数

c++ - 许多具有相同构造函数的类

c++ - 如何使用 std::is_same 生成编译时错误?

c++ - C++ 编译器如何处理 const 数组

java - Android:如何在另一个包的 Activity 中显示异步任务的进度对话框?

Python 的属性装饰器没有按预期工作

python - 根据大小和方向为 matplotlib quiver field 上色

c++ - 创建自定义类型对象的 vector

r - R中两个向量之间的差异