[英]C++ redefinition error, Why am I getting this error?
我遇到了重新定义问题,但我注意到我已经在 .hpp 文件中包含了 .cpp 文件,所以我的错误是再次在 my.cpp 文件中包含 .hpp 文件现在我收到此错误,与模板有关。 另外,当您解决我的问题时,您能向我解释一下模板 class 是做什么的吗? cplusplus.com 没有那么描述性。 谢谢你。 :)
//implementation
template<class T>
ArrayBag<T>::ArrayBag() : item_count_(0){}
-------------警告您现在离开实施----------------------------
//interface
#ifndef ARRAY_BAG_H
#define ARRAY_BAG_H
#include <vector>
template<class T>
class ArrayBag
{
protected:
static const int DEFAULT_CAPACITY = 200;
T items_[DEFAULT_CAPACITY];
int item_count_;
int get_index_of_(const T& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
//adds a new element to the end, returns true if it was successfully been added
bool add(const T& new_entry);
bool remove(const T& an_entry);
void clear();
bool contains(const T& an_entry) const;
int getFrequencyOf(const T& an_entry) const;
std::vector<T> toVector() const;
void display() const;
//overloading operators for objects
void operator+=(const ArrayBag<T>& a_bag);
void operator-=(const ArrayBag<T>& a_bag);
void operator/=(const ArrayBag<T>& a_bag);
};
#include "ArrayBag.cpp"
#endif
-------------警告您现在正在离开界面----------------------------
//error
5 C:\Users\minahnoona\Desktop\ArrayBag.cpp expected constructor, destructor, or type conversion before '<' token
5 C:\Users\minahnoona\Desktop\ArrayBag.cpp expected `;' before '<' token
不要将ArrayBag.cpp
称为.cpp
文件。 header 文件中的模板实现 go,名称应反映这一点。
如果您希望在单独的文件中实现(您并不严格需要),请将其命名为ipp
或tpp
。 项目系统不会尝试自行编译的东西。
然后从.hpp
中包含它,而不是从.ipp
.hpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.