2017-07-30 38 views
-1

我想实现一个可以处理任意大数字的类。我知道我可以使用BigInteger之类的其他库,但我只是想将自己的事情作为练习来实现。如何在C++中使用我自己的类中的库?

我的头文件:

#ifndef INT_H 
#define INT_H 

//#ifndef vector 
#include <vector> 

class Int{ 
private: 
    vector<int> v; 
public: 
    Int(); 
    Int(int); 
    void clear(); 
    void push_back(); 
    void resize(); 
    vector<int>::iterator begin(); 
    vector<int>::iterator end(); 
    int size(); 
    void sum(Int &, Int, Int); 
    void sub(Int &, Int, Int); 
    void prod(Int &, Int, Int); 
    Int operator+(const Int &); 
    Int operator-(const Int &); 
    Int operator*(const Int &); 
    Int operator>(Int &); 
    Int operator<(Int &); 
    Int operator>=(Int &); 
    Int operator<=(Int &); 
    int& operator[] (Int); 
}; 

//#endif // vector 
#endif // INT_H 

的问题是它给了我一个错误向量的第一次相遇在第9行,即“预期不合格-ID之前‘<’令牌”

任何帮助将非常感激。

编辑:混淆define与include。 现在我得到的矢量没有命名一个类型

+0

你真的认为这与你试图编写一个大整数类有关吗? – juanchopanza

+3

将'vector'的所有实例更改为'std :: vector'。 –

+0

...或写'使用命名空间标准;' –

回答

2

vector类型从#include <vector>std命名空间;自vector<int>明确的类型是不是在你的代码中定义的,你需要做以下操作之一来解决这个问题:

  1. 重命名的vector<T>所有实例std::vector<T>其中T为载体将含有类型(你的情况是int)。

  • #include <vector>后需要添加行using std::vector;。使用此using declaration时,遇到不合格vector类型的任何地方,它将使用std::vector类型。
  • 记住,因为这个类是在头文件中定义,如果您使用选项2,那么任何你#include "Int.h",还包括using std::vector;声明会。

    一个侧面说明你的代码:我不知道你与你的Int类满意图是什么,特别是因为你的类提供了类似于一个序列容器的成员函数,但不要忘记你的assignment operators(如Int& operator=(std::uint32_t i) ... )。

    希望能有所帮助。

    相关问题