我无法编译这C++代码
I can't get this C++ code to compile
我在编译此代码时遇到问题。我一直遇到令人困惑的错误,我似乎无法破解。我做错了什么?
此代码应该计算给定值的集合差、设置并集和设置交集。
#include <iostream>
#include <vector>
template<class ItemType>
int VectorBag<ItemType>::getCurrentSize() const {
return items.size();
}
template<class ItemType>
bool VectorBag<ItemType>::isEmpty() const {
return items.size() == 0;
}
template<class ItemType>
bool VectorBag<ItemType>::add(const ItemType& newEntry) {
items.push_back(newEntry);
return true;
}
template<class ItemType>
bool VectorBag<ItemType>::remove(const ItemType& anEntry) {
for( vector<ItemType>::iterator iter = items.begin(); iter != items.end(); ++iter ) {
if( *iter == anEntry )
{
items.erase( iter );
return true;
}
}
return false;
}
template<class ItemType>
void VectorBag<ItemType>::clear() {
items.clear();
}
template<class ItemType>
bool VectorBag<ItemType>::contains(const ItemType& anEntry) {
bool found = false;
int i = 0;
while (!found && (i < (int) items.size())) {
if (anEntry == items[i])
found = true;
i++;
}
return found;
}
template<class ItemType>
int VectorBag<ItemType>::getFrequencyOf(const ItemType& anEntry) {
int f = 0;
for (int i = 0; i < (int) items.size(); i++) {
if (items[i] == anEntry)
f++;
}
return f;
}
template<class ItemType>
vector<ItemType> VectorBag<ItemType>::toVector() {
vector<ItemType> vec;
//copy all elements to new set and return
for (int i = 0; i < (int) items.size(); i++)
vec.push_back(items[i]);
return vec;
}
template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
//use all elements from both sets
for (int i = 0; i < (int) items.size(); i++) {
newBag.add(items[i]);
}
for (int i = 0; i < (int) anotherBag.items.size(); i++) {
newBag.add(anotherBag.items[i]);
}
return newBag;
}
template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator*(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
vector<ItemType> v3;
//find intersection of sets
sort(this->items.begin(), this->items.end());
sort(anotherBag.items.begin(), anotherBag.items.end());
set_intersection(this->items.begin(), this->items.end(), anotherBag.items.begin(), anotherBag.items.end(),back_inserter(v3));
newBag.items = v3;
return newBag;
}
template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator-(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
//check if items in set1 exists in set2
for (int i = 0; i < (int) items.size(); i++) {
if (!anotherBag.contains(items[i])) {
newBag.add(items[i]);
}
}
return newBag;
}
通过查看您的错误消息,我相信这可能与模板类实现中单独的 .h 和 .cpp 文件的问题有关和为什么模板只能在头文件中实现?
基本上,您需要将整个模板代码放在头文件中,而不是单独的.cpp和.h文件。除非您显式实例化要在.cpp文件本身中使用的类型,如为什么只能在头文件中实现模板?中所述。
你也没有在你的cpp文件中包括标题,正如Pete Becker指出你和我第一眼就错过了。向他致敬。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值