私は自分のグラフプロジェクトを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 & retrieve()関数で。 'int &'型の無効な初期化は、 'const int'型の式から戻ってくるデータを参照しています。これは型Tのプライベートメンバーデータです(intとしてmainでインスタンス化されます)。どんな助けでも大歓迎です!おかげ
ありがとうございました!それを固定した私はあなたがまだ投票することを許されていないので、あなたにアップの投票を与えることはできませんが、誰かがこれを見れば、この人に最高の投票を与えます! –