如何在 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>
,因此需要执行以下操作之一来解决此问题:
- 将所有
vector<T>
实例重命名为std::vector<T>
其中T
是矢量将包含的类型(在您的情况下为int
(。
或
#include <vector>
之后,您需要添加行using std::vector;
。使用此 using 声明,在遇到非限定vector
类型的任何地方,它将使用std::vector
类型。
请记住,由于此类是在标头中定义的,因此如果使用选项 2,则在#include "Int.h"
的任何地方,您还将包含using std::vector;
声明。
代码的旁注:我不确定您对Int
类的全部意图是什么,特别是因为您的类提供了类似于序列容器的成员函数,但不要忘记您的赋值运算符(例如Int& operator=(std::uint32_t i) ...
(。
希望能有所帮助。
相关文章:
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 将矩阵乘以我自己的输入的向量
- 如何正确包含我自己的标头?
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- 为什么__builtin_popcount比我自己的比特计数功能慢?
- 在C++中,我可以在定义自己的复制构造函数后跳过定义赋值运算符吗?
- 在wxWidgets的事件中包含我自己的数据的最佳方法是什么?
- 如何在我自己的staticLib中使用staticLib
- 在我自己的堆栈中实现top的问题
- 尝试检查输入的文本与我自己的自定义答案
- 我应该在服务模板中的什么位置添加自己的代码?
- 如何在我自己的类中重载"+"和"<<"运算符
- 我正在尝试在视觉工作室上创建自己的库/源函数
- C++:我自己类的动态数组
- 如何在 C++ 中使用我自己的类中的库?
- 如何链接我自己的 .so 文件而不是操作系统捆绑包 .so 文件?
- 如何构建默认使用我自己的构建libc ++的clang?
- 访问另一个类(系统)的非静态字段,就好像它是我自己的字段一样 - 优雅地
- 分发我自己的程序的几个问题
- 如何自己为我自己的shared_ptr实现实现别名构造函数