我目前正在用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; }
总结问题似乎与我的私人T&amp; retrieve()函数。它一直说'int&amp;'类型的无效初始化来自类型为'const int'的表达式引用它返回的数据,它是类型为T的私有成员数据(在main中实例化为int)。任何帮助将不胜感激!感谢
答案 0 :(得分:1)
T data;
...
T& retrieve() const{ return data; }
const
修饰符使此方法成为const
方法。这意味着,基本上,它的this
是指向const
类实例的指针。由于data
未使用mutable
修饰符声明,data
是const
值,并且您的编译器正在抱怨,因为它无法返回对mutable的引用值,因为data
是一个常量值。
将方法更改为:
const T& retrieve() const{ return data; }
或者,我不明白为什么需要这里的参考,只是:
T retrieve() const{ return data; }
P.S。在放弃之前,您应该尝试理解这些编译器错误。确实,C ++编译错误因其典型的迟钝而具有传奇色彩,但在这种情况下,您应该能够在盯着这些错误消息几分钟后弄清楚编译器告诉您的内容。