簡體   English   中英

如何在C ++中在自己的類中使用庫?

[英]how to use libraries inside my own class in c++?

我想實現一個可以處理任意大數字的類。 我知道我可以使用BigInteger等其他庫,但我只是想在實踐中實現自己的東西。

我的頭文件:

#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”

任何幫助將不勝感激。

編輯:混淆包含。 現在我得到向量沒有命名類型

#include <vector>vector類型在std名稱空間中; 由於在代碼中未定義vector<int>的顯式類型,因此您需要執行以下一項操作來解決此問題:

  1. vector<T>所有實例重命名為std::vector<T> ,其中T是向量將包含的類型(在您的情況下為int )。

要么

  1. #include <vector>您需要using std::vector;添加該行using std::vector; 使用此聲明 ,在遇到不合格vector類型的任何地方,它將使用std::vector類型。

請記住,由於此類是在標頭中定義的,因此如果您使用選項2,則在#include "Int.h"任何地方,您還將包括一個using std::vector; 宣言。

您的代碼的旁注:我不確定您對Int類的全部意圖是什么,尤其是因為您的類提供了類似於序列容器的成員函數,但請不要忘記您的賦值運算符 (例如Int& operator=(std::uint32_t i) ... )。

希望能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM