无法创建构造函数为空的类的实例 我已经为C++中的SET类编写了以下代码。我面临的问题是 当我做Set*Set=newset()时在主方法中。它正在显示错误- 类模板“Set”的参数列表缺少C/C++(441) 这意味着它正在寻找带有参数的构造函数。即使没有 参数已指定。这可能是什么原因
我遇到的另一个错误是在无法创建构造函数为空的类的实例 我已经为C++中的SET类编写了以下代码。我面临的问题是 当我做Set*Set=newset()时在主方法中。它正在显示错误- 类模板“Set”的参数列表缺少C/C++(441) 这意味着它正在寻找带有参数的构造函数。即使没有 参数已指定。这可能是什么原因,c++,compiler-errors,C++,Compiler Errors,我遇到的另一个错误是在模板节点*Set::getPointerTo(const ItemType&target)const行。 错误:声明与不兼容 这可以看作是在private内部的Set类中声明的。虽然它的声明完全符合 在Set类和实现类中相同。我不知道它为什么会抛出那个错误 #包括“Set.h” #包括“Node.h” 模板 Set::Set():headPtr(nullptr),itemCount(0) { }//结束默认构造函数 模板 集合::集合(常量集合和项目) { itemCou
模板节点*Set::getPointerTo(const ItemType&target)const
行。错误:
声明与
不兼容这可以看作是在private内部的Set类中声明的。虽然它的声明完全符合
在Set类和实现类中相同。我不知道它为什么会抛出那个错误
#包括“Set.h”
#包括“Node.h”
模板
Set::Set():headPtr(nullptr),itemCount(0)
{
}//结束默认构造函数
模板
集合::集合(常量集合和项目)
{
itemCount=项目->项目计数;
节点*origChainPtr=item->headPtr if(origChainPtr==nullptr)
headPtr=nullptr;//原始包是空的;副本也是空的
}//结束复制构造函数
模板
节点*集::
getPointerTo(常量项类型和目标)常量
{
bool-found=false;
节点*curPtr=headPtr;
而(!found&(curPtr!=nullptr))
{
如果(目标==curPtr->getItem())
发现=真;
其他的
curPtr=curPtr->getNext();
}//结束时
返回电流;
}//结束getPointerT
模板
类集:公共设置接口
{
私人:
节点*headPtr;
整数项计数;
Node*getPointerTo(const ItemType&target)const;
公众:
Set();
Set(const Set&item);//复制构造函数
}; // 结束链接数据库
#包括
#包括“Set.h”
使用名称空间std;
int main()
{
Set*Set=newset();
返回0;
}
Set
似乎是一个模板,而不是一种类型。无法创建模板的对象。是否要执行类似于Set*Set=new Set()的操作代码>。或者更好的Set代码>。使用new
和delete
通常是最后的选择。通常,C++中不需要它。@超代码> set *set =新set();<代码>这似乎有效。可能我没有指定模板的值,所以它抛出了一个error@DanielLangr正如我在前面的评论中提到的,这种方法是有效的。但我想理解你的观点。因为我声明了template class Set:public SetInterface
并没有使Set成为一个类。或者你说的是我必须在集合中传递模板类型的相同内容?还有,你知道我在实现方法-getpointer to
#include "Set.h"
#include "Node.h"
template <class ItemType>
Set<ItemType>::Set() : headPtr(nullptr), itemCount(0)
{
} // end default constructor
template <class ItemType>
Set<ItemType>::Set(const Set<ItemType> &item)
{
itemCount = item->itemCount;
Node<ItemType> *origChainPtr = item->headPtr if (origChainPtr == nullptr)
headPtr = nullptr; // Original bag is empty; so is copy
} // end copy constructor
template <class ItemType>
Node<ItemType> *Set<ItemType>::
getPointerTo(const ItemType &target) const
{
bool found = false;
Node<ItemType> *curPtr = headPtr;
while (!found && (curPtr != nullptr))
{
if (target == curPtr->getItem())
found = true;
else
curPtr = curPtr->getNext();
} // end while
return curPtr;
} // end getPointerT
template <class ItemType>
class Set : public SetInterface<ItemType>
{
private:
Node<ItemType> *headPtr;
int itemCount;
Node<ItemType> *getPointerTo(const ItemType &target) const;
public:
Set();
Set(const Set<ItemType> &item); // Copy constructor
}; // end LinkedBag
#include <iostream>
#include "Set.h"
using namespace std;
int main()
{
Set *set = new Set();
return 0;
}