Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ overloading operator= in template
    text
    copied!<p>Hi all I'm having trouble with C++ template operator=</p> <p>What I'm trying to do: I'm working on a graph algorithm project using cuda and we have several different formats for benchmarking graphs. Also, I'm not entirely sure what type we'll end up using for the individual elements of a graph.<br> My goal is to have a templated graph class and a number of other classes, each of which will know how to load a particular format. Everything seems to work alright except the point where the graphCreator class returns a graph type from the generate function. Here is my code:</p> <p>Graph opertator=:</p> <pre><code> MatrixGraph&lt;T&gt;&amp; operator=(MatrixGraph&lt;T&gt;&amp; rhs) { width = rhs.width; height = rhs.height; pGraph = rhs.pGraph; pitch = rhs.pitch; sizeOfGraph = rhs.sizeOfGraph; rhs.Reset(); } </code></pre> <p>the rhs.reset() call removes all references to allocated memory so they will not be deallocated by rhs. Only one graph is allowed to have a reference to the allocated graph memory.</p> <p>Graph copy constructor:</p> <pre><code> MatrixGraph(MatrixGraph&lt;T&gt;&amp; graph) { (*this) = graph; } </code></pre> <p>Graph Creator load function:</p> <pre><code>MatrixGraph&lt;T&gt; LoadDIMACSGraphFile(std::istream&amp; dimacsFile) { char inputType; std::string input; GetNumberOfNodesAndEdges(dimacsFile, nodes, edges); MatrixGraph&lt;T&gt; myNewMatrixGraph(nodes); while(dimacsFile &gt;&gt; inputType) { switch(inputType) { case 'e': int w,v; dimacsFile &gt;&gt; w &gt;&gt; v; myNewMatrixGraph[w - 1][v - 1] = 1; myNewMatrixGraph[v - 1][w - 1] = 1; break; default: std::getline(dimacsFile, input); break; } } return myNewMatrixGraph; } </code></pre> <p>And finally in main.cpp where I'm trying to unit test this I use it:</p> <pre><code>DIMACSGraphCreator&lt;short&gt; creator; myGraph = creator.LoadDIMACSGraphFile(instream); </code></pre> <p>When I try to compile I get this error:</p> <pre><code>main.cpp: In function 'int main(int, char**)': main.cpp:31: error: no match for 'operator=' in 'myGraph = DIMACSGraphCreator&lt;T&gt;::LoadDIMACSGraphFile(std::istream&amp;) [with T = short int](((std::istream&amp;)(&amp; instream.std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;::&lt;anonymous&gt;)))' MatrixGraph.h:103: note: candidates are: MatrixGraph&lt;T&gt;&amp; MatrixGraph&lt;T&gt;::operator=(MatrixGraph&lt;T&gt;&amp;) [with T = short int] make: *** [matrixTest] Error 1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload