gpt4 book ai didi

c++ - 如何解决有关迭代器构造函数的一系列错误,涉及预期左值、缺少转换和可行性

转载 作者:行者123 更新时间:2023-11-28 06:09:51 25 4
gpt4 key购买 nike

我有一个程序是我在 2000 年为一个类(class)编写的,我想我会尝试运行它只是为了好玩。它曾经在 2000 年编译过,但我现在遇到了一堆错误。自那门课以来,我几乎没有看过 C++ 代码,所以我有点困惑。我解决了一堆关于 iostream.h 和 list.h 的投诉(把“std::”放在一堆像 std::cerr 等东西的前面)。我不知道这是否导致了当前错误,或者我的迭代器是否存在固有错误,但唯一需要解决的问题涉及一堆迭代器构造函数。首先,这是错误示例:

SolutionList.cpp:10:22: error: no matching constructor for initialization of 'PieceList::iterator'
PieceList::iterator i=rhs.first();
^ ~~~~~~~~~~~
./PieceList.h:27:5: note: candidate constructor not viable: expects an l-value for 1st argument
iterator(iterator& rhs){data = rhs.data;}
^
./PieceList.h:28:5: note: candidate constructor not viable: no known conversion from 'PieceList::iterator' to 'std::list<Piece>::iterator &'
(aka '__list_iterator<value_type, __void_pointer> &') for 1st argument
iterator(std::list<Piece>::iterator& rhs){data = rhs;}
^
./PieceList.h:26:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
iterator(){}
^

我认为这是代码的相关部分:

class PieceList
{
public:
PieceList(){}
PieceList(PieceList& rhs){pieces = rhs.pieces;}
PieceList(std::list<Piece>& rhs){pieces = rhs;}

friend std::ifstream& operator>>(std::ifstream&,PieceList&);
friend std::istream& operator>>(std::istream&,PieceList&);

class iterator
{
public:
friend class PieceList;

iterator(){}
iterator(iterator& rhs){data = rhs.data;}
iterator(std::list<Piece>::iterator& rhs){data = rhs;}

Piece operator*(){return *data;}
iterator& operator=(iterator& rhs){data=rhs.data;return *this;}
iterator& operator=(std::list<Piece>::iterator& rhs){data = rhs;return *this;}
bool operator==(iterator& rhs){return data==rhs.data;}
bool operator==(const iterator& rhs) const{return data==rhs.data;}
bool operator!=(iterator rhs){return data != rhs.data;}
iterator& operator++(){++data;return *this;}
iterator& operator--(){--data;return *this;}
private:
std::list<Piece>::iterator data;
};

iterator first(){iterator i;i.data=pieces.begin();return i;}
iterator last(){iterator i;i.data=pieces.end();return i;}

private:
std::list<Piece> pieces;
};

所以我知道它不喜欢第二个构造函数,但我不确定如何修复它。我无法通过谷歌搜索错误来理解我的任何谷歌结果。谁能指出我正确的方向?

最佳答案

您不能将临时变量绑定(bind)到非常量引用。

    iterator(iterator const& rhs) : data(rhs.data) {}
iterator(std::list<Piece>::iterator rhs) : data(rhs) {}

请注意,迭代器通常按值传递

Live On Coliru

#include <iterator>
#include <list>
#include <string>
#include <iostream>

struct Piece {
std::string name;
};

class PieceList
{
public:
PieceList(){}
PieceList(PieceList const& rhs){pieces = rhs.pieces;}
PieceList(std::list<Piece> const& rhs){pieces = rhs;}

friend std::ifstream& operator>>(std::ifstream&,PieceList&);
friend std::istream& operator>>(std::istream&,PieceList&);

class iterator
{
public:
friend class PieceList;

iterator(){}
iterator(iterator const& rhs) : data(rhs.data) {}
iterator(std::list<Piece>::iterator rhs) : data(rhs) {}

Piece operator*(){return *data;}
iterator& operator=(iterator& rhs){data=rhs.data;return *this;}
iterator& operator=(std::list<Piece>::iterator& rhs){data = rhs;return *this;}
bool operator==(iterator& rhs){return data==rhs.data;}
bool operator==(const iterator& rhs) const{return data==rhs.data;}
bool operator!=(iterator rhs){return data != rhs.data;}
iterator& operator++(){++data;return *this;}
iterator& operator--(){--data;return *this;}
private:
std::list<Piece>::iterator data;
};

iterator first(){iterator i;i.data=pieces.begin();return i;}
iterator last(){iterator i;i.data=pieces.end();return i;}

private:
std::list<Piece> pieces;
};

int main() {
PieceList pl { {
{ "aap" },
{ "noot" },
{ "mies" },
} };
for(auto it=pl.first(); it!=pl.last(); ++it) {
std::cout << (*it).name << "\n";
}
}

打印

aap
noot
mies

PieceList 的构造函数也有类似的问题。您的迭代器类也缺少 operator->。我将其保留为 ExerciseForTheReader™

关于c++ - 如何解决有关迭代器构造函数的一系列错误,涉及预期左值、缺少转换和可行性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31495163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com