2017-07-30 37 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) ... )。

    希望能有所幫助。

    相關問題