2014-09-24 92 views
0

我想实现一个biginteger类,并且在我创建了一个biginteger类和一个合适的头文件之后,起初我试图定义一个运算符=()运算符,所以当我一个新的biginteger对象,我将能够使它与一个整数相等。C++ biginteger类运算符=

这是main.cpp中:

#include <iostream> 
#include "bigint.h" 

using namespace std; 

int main() 
{ 
    bigint bela = 15; 
    cout << "Hello world!" << bela.mennyi() <<endl; 
    return 0; 
} 

这是BigInteger的标题:

#ifndef BIGINT_H 
#define BIGINT_H 

#include <vector> 
#include <iostream> 

class bigint 
{ 
    public: 
     bigint(); 
     void operator=(const int &a); 
     int mennyi(); 

    protected: 
    private: 
     std::vector<int> numarray; 
}; 

#endif // BIGINT_H 

而且biginteger.cpp文件:

#include "bigint.h" 
#include <iostream> 

using namespace std; 


bigint::bigint() 
{ 
    numarray.resize(0); 
} 

void bigint::operator=(const int &a) 
{ 
    int b = a; 
    if(b >= 0) 
    { 
     numarray.resize(0); 
     while(b!=0){ 
     numarray.push_back(b%10); 
      b = b/10; 
     } 
    } 
} 

int bigint::mennyi() 
{ 
    int ki = 0; 
    for(int i = (numarray.size())-1; i>=0; i--) 
    { 
     ki = ki*10 + numarray[i]; 
    } 
    return ki; 
    } 

当我开始调试我得到一个错误说:从'int'转换为非标量类型'bigint'请求。

+0

你只有赋值操作符,而不是类型转换操作符... 编译器的错误信息会导致你的解决方案。 (你也可以在这里发帖) – 2014-09-24 17:17:29

回答

1

你应该实现这个构造函数:

bigint::bigint(int); 
+0

注意:'bigint bela = 15;'不是赋值,而是一个构造函数调用。 – 2014-09-24 17:22:37