功能样式转换从 'int' 到 'ItemType' 的匹配转换



我有一项家庭作业,其中我必须为链表编写一些方法,并使用教授的驱动程序进行测试。我一直遇到这个错误:no matching conversion for functional-style cast from 'int' to 'ItemType'

以下是我的"节点"类ItemType:的文件

// ItemType.h. 
#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType  {LESS, GREATER, EQUAL};
class ItemType{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(std::ostream&) const;
void Initialize(int number);
private: int value;
};

和ItemType.cpp

#include <fstream>
#include <iostream>
#include "ItemType.h"
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const 
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number) 
{
value = number;
}
void ItemType::Print(std::ostream& out) const 
// pre:  out has been opened.
// post: value has been sent to the stream out.
{
out << value;
}

当我尝试使用professors驱动程序时,我在使用构造函数初始化ItemType类时出错。我这样初始化它们:classList.putItem(ItemType(4))但我最终出现了上述错误,我不确定我错在哪里了,这是我的驱动程序:

#include "unsorted.h"
using namespace std;
int main() {
UnsortedType classList;

classList.PutItem(ItemType(4));
classList.PutItem(ItemType(5));
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(8));
cout << "(original) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.ShiftRight();
cout << "(shifted right) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.DeleteItem(ItemType(4));
cout << "(delete all 4s) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.ShiftRight();
cout << "(shift right) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
return 0;
}

ItemType没有接受int的构造函数。一个简单的解决方案是定义构造函数:

ItemType(int v) : value{v} { }

相关内容

  • 没有找到相关文章

最新更新