【问题标题】:No instance of overloaded function matches the argument list, argument types are: (std::ifstream, std::string, char)没有重载函数的实例与参数列表匹配,参数类型为:(std::ifstream, std::string, char)
【发布时间】:2021-01-01 17:26:12
【问题描述】:

我尝试运行程序将 txt 文件加载到链接列表中,但遇到了“getline”错误。在 readLine 函数中,我尝试获取 txt 文件中的内容,但它不允许我这样做。有谁知道如何解决这一问题?我试图搜索问题并阅读有关它的答案,但我仍然不知道如何解决问题。提前致谢。

链表.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());
    }
}

项目.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; }
};

【问题讨论】:

    标签: c++ getline


    【解决方案1】:

    您的问题是您的访问器不返回字符串&,而是一个不能作为字符串& 传递的字符串的新副本。

        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,但是抛出一个异常......

    【讨论】:

    • 你试过你的答案了吗?这就是错误,您不能将右值用作左值。
    • 非常感谢,先生。你帮我解决我的问题。但是我不太明白你说的是什么,你能推荐一些书籍或网站来阅读吗?
    • 你可以看看SO非官方列表:stackoverflow.com/questions/388242/…
    • 非常感谢。我还有很长的路要走。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2013-10-19
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多