gpt4 book ai didi

c++ - 传递不同数据类型的问题

转载 作者:行者123 更新时间:2023-11-30 05:27:43 25 4
gpt4 key购买 nike

我目前正在用 C++ 开发我自己的图形项目,我遇到了一个问题,我确信它很容易解决,但我似乎无法正确解决。我正在构建一个模板图形类,其中声明了一个顶点类:

template <typename T>
class graph{

public:

class vertex{
public:
bool visited; //used for paths. True if vertex has been visited

//vertex constructor
vertex(const T& d = T{}, int i = 0): data(d), visited(false), id(i){
std::cout << "Just created a vertex using its constructor :)\n";}
//vertex move constructor
vertex(T&& d, int i = 0): data(std::move(d)), visited(false), id(i){}
//returns vertex ID
int returnID() const{ return id; };
//returns data in vertex
T& operator*(){ return retrieve(); }
//returns const reference to data in vertex
const T& operator*() const{ return retrieve(); }
//returns list of adjacent vertices
std::list<vertex>& getList() const{ return adjacent; }
//adds vertex to current vertex's adjacency list
void addToList(const vertex& add){
if(!isAdjacent(add))
adjacent.push_back(add);
}
//returns true if vertices are adjacent
bool isAdjacent(const vertex& add){
return (find(begin(adjacent), end(adjacent), add) == adjacent.end());
}
//overloaded equal operator for vertex class
bool operator==(const vertex& add){
if(data == *add && id == add.returnID()) return true;
return false;
}
private:
T data; //vertex stores data of any type
std::list<vertex> adjacent; //list of adjacent vertices
int id;

T& retrieve() const{ return data; }
];

];

我一直收到这个错误:

g++ -std=c++11 -Wno-reorder -Wall -pedantic -o executable.x main.cpp 
In file included from main.cpp:2:0:
graph.h: In instantiation of ‘T& graph<T>::vertex::retrieve() const [with T = int]’:
graph.h:70:47: required from ‘const T& graph<T>::vertex::operator*() const [with T = int]’
graph.h:84:17: required from ‘bool graph<T>::vertex::operator==(const graph<T>::vertex&) [with T = int]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/stl_algo.h:139:46: required from ‘_InputIterator std::__find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_List_iterator<graph<int>::vertex>; _Tp = graph<int>::vertex]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/stl_algo.h:4441:45: required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = std::_List_iterator<graph<int>::vertex>; _Tp = graph<int>::vertex]’
graph.h:80:54: required from ‘bool graph<T>::vertex::isAdjacent(const graph<T>::vertex&) [with T = int]’
graph.h:75:24: required from ‘void graph<T>::vertex::addToList(const graph<T>::vertex&) [with T = int]’
graph.hpp:25:4: required from ‘bool graph<T>::addEdge(graph<T>::vertex&, graph<T>::vertex&) [with T = int]’
main.cpp:12:18: required from here
graph.h:92:31: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’
T& retrieve() const{ return data; }
^
graph.h: In member function ‘T& graph<T>::vertex::retrieve() const [with T = int]’:
graph.h:92:37: warning: control reaches end of non-void function [-Wreturn-type]
T& retrieve() const{ return data; }

总结一下这个问题,我的私有(private) T& retrieve() 函数似乎有问题。它一直说类型“int&”的无效初始化来自类型为“const int”的表达式,指的是它返回的数据,它是类型 T 的私有(private)成员数据(在 main 中实例化为 int)。任何帮助将不胜感激!谢谢

最佳答案

            T data;

...

T& retrieve() const{ return data; }

const 修饰符使该方法成为const 方法。这意味着,本质上,它的 this 是指向 const 类实例的指针。由于 data 未使用 mutable 修饰符声明,因此 data 是一个 const 值,您的编译器会报错,因为它不能返回对可变值的引用,因为 data 是一个常量值。

要么将方法更改为:

            const T& retrieve() const{ return data; }

或者,我不明白为什么这里需要引用,只是:

            T retrieve() const{ return data; }

附言在如此迅速地放弃之前,您应该尝试理解这些编译器错误。的确,C++ 编译错误以其典型的迟钝而闻名,但在这种情况下,在盯着这些错误消息几分钟后,您应该能够弄清楚编译器告诉您的是什么。

关于c++ - 传递不同数据类型的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37106619/

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