没有重载函数的实例匹配参数列表,参数类型是:(std::ifstream, std::string, char)
No instance of overloaded function matches the argument list, argument types are: (std::ifstream, std::string, char)
我尝试 运行 程序将 txt 文件加载到链表中,但卡在了“getline”错误中。在 readLine 函数中,我尝试获取 txt 文件中的内容,但它不允许我这样做。有谁知道如何解决这一问题?我试图搜索问题并阅读有关它的答案,但我仍然不知道如何解决问题。提前致谢。
Linkedlist.h
#include "LinkedList.h"
Node* Node::createNode(Item newData)
{
Node* point = new Node;
if (point == nullptr)
{
cout << "Failed." << endl;
exit(EXIT_FAILURE);
}
point->data = newData;
point->setNext(nullptr);
return point;
}
void LinkedList::createList(LinkedList& list)
{
list.head = nullptr;
list.tail = nullptr;
}
void LinkedList::insertHead(LinkedList& list, Node* point)
{
if (list.head == nullptr)
{
list.head = list.tail = point;
}
else
{
point->setNext(head);
list.head = point;
}
}
void LinkedList::appenTail(LinkedList& list, Node* point)
{
if (list.head == nullptr)
{
list.head = point;
list.tail = point;
}
else
{
list.tail->setNext(point);
list.tail = point;
}
}
void LinkedList::readLine(ifstream& file, Item& item)
{
**getline(file, item.getItemId(), ',');
getline(file, item.getTitle(), ',');
getline(file, item.getLoan(), ',');
getline(file, item.getCopy(), ',');
getline(file, item.getFee(), ',');
getline(file, item.getGenre(), ',');**
}
void LinkedList::readAllFile(ifstream& file, LinkedList& list)
{
while (!file.eof())
{
Node* point = new Node;
Item item;
Node node;
readLine(file, item);
point = node.createNode(item);
appenTail(list, point);
}
}
void LinkedList::printOneLine(Item item)
{
cout << item.getItemId() << ',' << item.getTitle() << ',' << item.getLoan() << ',' << item.getCopy() << ',' << item.getFee() << ',' << item.getGenre();
}
void LinkedList::printAll(LinkedList list)
{
for (Node* point = list.head; point != NULL; point = point->getNext())
{
printOneLine(point->getData());
}
}
Item.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Item
{
protected:
string itemId, title, type, loan, num_copy, fee, genre;
public:
Item() {}
Item(string itemId, string title, string type, string loan, string num_copy, string fee, string genre)
: itemId(itemId), title(title), type(type), loan(loan), num_copy(num_copy), fee(fee), genre(genre)
{}
~Item() {}
string getItemId() { return this->itemId; }
string getTitle() { return this->title; }
string getType() { return this->type; }
string getLoan() { return this->loan; }
string getCopy() { return this->num_copy; }
string getFee() { return this->fee; }
string getGenre() { return this->genre; }
};
你的问题是你的访问器不是return字符串&,而是一个不能作为字符串&传递的字符串的新副本。
string& getItemId() { return itemId; }
string& getTitle() { return title; }
string& getType() { return type; }
string& getLoan() { return loan; }
string& getCopy() { return num_copy; }
string& getFee() { return fee; }
string& getGenre() { return genre; }
最重要的是,不要使用 using namespace std
,在构造函数中使用 std::move
,不要使用 exit
但抛出异常...
我尝试 运行 程序将 txt 文件加载到链表中,但卡在了“getline”错误中。在 readLine 函数中,我尝试获取 txt 文件中的内容,但它不允许我这样做。有谁知道如何解决这一问题?我试图搜索问题并阅读有关它的答案,但我仍然不知道如何解决问题。提前致谢。
Linkedlist.h
#include "LinkedList.h"
Node* Node::createNode(Item newData)
{
Node* point = new Node;
if (point == nullptr)
{
cout << "Failed." << endl;
exit(EXIT_FAILURE);
}
point->data = newData;
point->setNext(nullptr);
return point;
}
void LinkedList::createList(LinkedList& list)
{
list.head = nullptr;
list.tail = nullptr;
}
void LinkedList::insertHead(LinkedList& list, Node* point)
{
if (list.head == nullptr)
{
list.head = list.tail = point;
}
else
{
point->setNext(head);
list.head = point;
}
}
void LinkedList::appenTail(LinkedList& list, Node* point)
{
if (list.head == nullptr)
{
list.head = point;
list.tail = point;
}
else
{
list.tail->setNext(point);
list.tail = point;
}
}
void LinkedList::readLine(ifstream& file, Item& item)
{
**getline(file, item.getItemId(), ',');
getline(file, item.getTitle(), ',');
getline(file, item.getLoan(), ',');
getline(file, item.getCopy(), ',');
getline(file, item.getFee(), ',');
getline(file, item.getGenre(), ',');**
}
void LinkedList::readAllFile(ifstream& file, LinkedList& list)
{
while (!file.eof())
{
Node* point = new Node;
Item item;
Node node;
readLine(file, item);
point = node.createNode(item);
appenTail(list, point);
}
}
void LinkedList::printOneLine(Item item)
{
cout << item.getItemId() << ',' << item.getTitle() << ',' << item.getLoan() << ',' << item.getCopy() << ',' << item.getFee() << ',' << item.getGenre();
}
void LinkedList::printAll(LinkedList list)
{
for (Node* point = list.head; point != NULL; point = point->getNext())
{
printOneLine(point->getData());
}
}
Item.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Item
{
protected:
string itemId, title, type, loan, num_copy, fee, genre;
public:
Item() {}
Item(string itemId, string title, string type, string loan, string num_copy, string fee, string genre)
: itemId(itemId), title(title), type(type), loan(loan), num_copy(num_copy), fee(fee), genre(genre)
{}
~Item() {}
string getItemId() { return this->itemId; }
string getTitle() { return this->title; }
string getType() { return this->type; }
string getLoan() { return this->loan; }
string getCopy() { return this->num_copy; }
string getFee() { return this->fee; }
string getGenre() { return this->genre; }
};
你的问题是你的访问器不是return字符串&,而是一个不能作为字符串&传递的字符串的新副本。
string& getItemId() { return itemId; }
string& getTitle() { return title; }
string& getType() { return type; }
string& getLoan() { return loan; }
string& getCopy() { return num_copy; }
string& getFee() { return fee; }
string& getGenre() { return genre; }
最重要的是,不要使用 using namespace std
,在构造函数中使用 std::move
,不要使用 exit
但抛出异常...