没有从'int'到'Student'的可行转换
no viable conversion from 'int' to 'Student'
(编辑版)
我目前正在编写二叉搜索树算法。(使用 xCode IDE)我正在从 txt 文件中获取数据并将其作为二进制搜索树插入。这是来自 txt 文件的数据示例。
3800 李,维克多; 2.8
3000 布朗,乔安妮; 4.0
正如您在 student.h 中看到的,有 2 个变量是 id 和 student。Id 包含数据"3800",学生包含"Lee, Victor;2.8".txt 的每一行都被视为一个根。现在,我必须通过一个唯一的键进行搜索,即id(例如"3800"),如果它在树中找到,则打印出来。我有5个文件,BinaryNode.h,BinaryTree.h,BinarySearchTree.h,Student.h,main.cpp。所有 3 个二进制头文件都使用模板,而 Student.h 没有模板。所以,这是我的int main。
int main()
{
BinarySearchTree<Student> tree;
getData(tree); (I didn't include getData part, but consider tree has txtfile data.)
bool found = false;
char input;
do
{
cout << "Enter a key letter to access to a corresponding menu." << endl << endl;
cout << "T – Print tree as an indented list" << endl;
cout << "S – Search by a unique key (student ID)" << endl;
cout << "B – Tree Breadth-First Traversal: Print by level" << endl;
cout << "D – Depth-First Traversals: inorder, preorder, postorder" << endl;
cout << "R – Find the longest branch and print it (from leaf to root)" << endl;
cout << "H – Help" << endl;
cout << "Q – Quit" << endl << endl;
cout << "Input: ";
cin >> input;
cout << endl;
if(input == 'T' || input == 'S' || input == 'B' || input == 'D' || input == 'R' || input == 'H' || input == 'Q' || input == 'A')
{
if(input == 'T')
{
//print tree as indented
}
else if(input == 'S')
{
//search by student ID
Student *result = new Student;
int id;
cout << "Enter the student ID to search the matching student." << endl;
cin >> id;
result->setId(id);
found = tree.getEntry(*result);
}
我将输入数据放入结果中并尝试搜索数据。
公共函数定义中的我的 getEntry 函数
template<class ItemType>
bool BinarySearchTree<ItemType>::getEntry(ItemType& anEntry)
{
BinaryNode<ItemType>* returnedItem = findNode(BinaryTree<ItemType>::rootPtr, anEntry);
if (returnedItem)
{
anEntry = returnedItem->getItem();
return true;
}
else return false;
}
//findNode 函数
template<class ItemType>
BinaryNode<ItemType>*
BinarySearchTree<ItemType>::findNode(BinaryNode<ItemType>* nodePtr,
ItemType & target)
{
ItemType result = result.getId(); <------- ******error here*******
ItemType root = nodePtr->getItem().getId();
if (nodePtr == nullptr)
return nullptr;
if (result == root)
return root;
if (result > root)
root = findNode(nodePtr->getRightPtr(), target);
else
root = findNode(nodePtr->getLeftPtr(), target);
return root;
}
我的 findNode 函数有错误。
->没有从'int'到'学生'的可行转换
//学生.h
class Student
{
private:
int id;
std::string name;
public:
Student() { id = 0; name = ""; }
Student(int newId, std::string newName) { id = newId; name = newName; }
friend bool operator >= (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
friend bool operator == (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
friend bool operator < (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
friend bool operator > (const Student l, const Student& r)
{
return std::tie(l.id, l.name) < std::tie(r.id, r.name);
}
/*
Student& operator = (Student& t_id)
{
if(this != &t_id)
id = t_id.getId();
return *this;
}
*/
void getStudent() { std::cin >> id; }
int getId() const { return id; }
void setId(int t_id) { id = t_id; }
std::string getName() const { return name; }
void setName(std::string t_name) { name = t_name; }
//ItemType getGpa() const { return gpa; }
//virtual void setGpa(std::string t_gpa) { gpa = t_gpa; }
我需要 = 运算符来解决这个问题吗?实际上,我创建了 = 运算符,但如果我启用 = 运算符代码,其他函数中的其他等号代码遇到错误。(没有可行的重载"=")
如何修复此错误?谢谢你的帮助。
该错误只是说您没有定义允许您为Student
分配int
或string
的operator=
。 getId()
和 getName()
分别返回 int
和 string
并且您正在尝试将它们的结果分配给在您的情况下Student
ItemType
。
您也没有学生构造函数采用int
或string
,这让我认为您可能不希望从int
或string
隐式转换为Student
(我个人不建议这样做)。
因此,请执行以下任一操作:
- 你的代码在语义上是错误的,你想做的是调用另一个函数而不是
getId()
和getName()
(我认为是这样) - 您的
getId()
和getName()
函数错误,应返回Student
以匹配模板要求
您的 - 模板不适合您的对象:)
修复的一个选项是:
ItemType x(a.getId(), a.getName());
调用构造函数需要int
和string
。请注意,我更改了变量名称,因为您的代码实际上没有意义:您将result.getId()
分配给您刚刚定义的result
!
您的operator=
不起作用的原因是您没有将const
引用作为参数,因此它不能与=
右侧const
对象一起使用,从而产生新的错误。但是,只要您没有隐式构造函数来获取这些,修复它就不会使其适用于int
和string
。
代码中还有许多其他奇怪的东西,比如你的getStudent()
方法不是getter,你应该以不同的方式命名它,并可能让它接受istream
引用作为参数,或者在构造函数中使用赋值而不是初始化列表:
Student() : id(0), name("") {}
Student(int newId, std::string newName) : ud(newId), name(newName) {}
或者你的operator>=
、operator>
和operator==
实际上都与<
相比。
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C2679:二进制"<<":未找到采用类型为"student"的右操作数的运算符(或没有可接受的转换)
- 编译错误:无法将参数 1 从 'student[20]' 转换为 'student'
- 没有从'int'到'Student'的可行转换
- 没有从 'int' 到 'student' C++ 的可行转换