大家好我在與C++模板操作麻煩=C++運算符重載=模板
我想要做的事: 我使用CUDA上的圖形算法的項目工作,我們有幾個不同的基準圖形格式。另外,我不完全確定我們最終將使用哪種類型的圖表的各個元素。
我的目標是擁有一個模板圖類和許多其他類,每個類都將知道如何加載特定格式。除了graphCreator類從generate函數返回一個圖類型的點之外,一切似乎都可以正常工作。 這裏是我的代碼:
圖opertator =:
MatrixGraph<T>& operator=(MatrixGraph<T>& rhs)
{
width = rhs.width;
height = rhs.height;
pGraph = rhs.pGraph;
pitch = rhs.pitch;
sizeOfGraph = rhs.sizeOfGraph;
rhs.Reset();
}
的rhs.reset()調用消除了分配的內存全部引用,以便它們不會被RHS被釋放。只允許一個圖形引用分配的圖形內存。
圖形拷貝構造函數:
MatrixGraph(MatrixGraph<T>& graph)
{
(*this) = graph;
}
圖造物主負載功能:
MatrixGraph<T> LoadDIMACSGraphFile(std::istream& dimacsFile)
{
char inputType;
std::string input;
GetNumberOfNodesAndEdges(dimacsFile, nodes, edges);
MatrixGraph<T> myNewMatrixGraph(nodes);
while(dimacsFile >> inputType)
{
switch(inputType)
{
case 'e':
int w,v;
dimacsFile >> w >> v;
myNewMatrixGraph[w - 1][v - 1] = 1;
myNewMatrixGraph[v - 1][w - 1] = 1;
break;
default:
std::getline(dimacsFile, input);
break;
}
}
return myNewMatrixGraph;
}
最後在main.cpp中,其中我試圖單元測試這一點,我使用它:
DIMACSGraphCreator<short> creator;
myGraph = creator.LoadDIMACSGraphFile(instream);
當我嘗試編譯時,我得到這個錯誤:
main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no match for 'operator=' in 'myGraph = DIMACSGraphCreator<T>::LoadDIMACSGraphFile(std::istream&) [with T = short int](((std::istream&)(& instream.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>)))'
MatrixGraph.h:103: note: candidates are: MatrixGraph<T>& MatrixGraph<T>::operator=(MatrixGraph<T>&) [with T = short int]
make: *** [matrixTest] Error 1
您不顯示'myGraph'的聲明。我們需要這個。 – 2009-11-21 20:22:39
此外,你似乎忽略了你的聲明中的「模板」部分...你可以添加它嗎? – 2009-11-21 20:23:30