如何在 C++ 中使用我自己的类中的库?

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

本文关键字:自己的 我自己 C++      更新时间:2023-10-16

我想实现一个可以处理任意大数字的类。我知道我可以使用其他库,如 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 声明,在遇到非限定vector类型的任何地方,它将使用std::vector类型。

请记住,由于此类是在标头中定义的,因此如果使用选项 2,则在#include "Int.h"的任何地方,您还将包含using std::vector;声明。

代码的旁注:我不确定您对Int类的全部意图是什么,特别是因为您的类提供了类似于序列容器的成员函数,但不要忘记您的赋值运算符(例如Int& operator=(std::uint32_t i) ...(。

希望能有所帮助。