Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 468767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:42:54+00:00 2026-05-12T23:42:54+00:00

Hi all I’m having trouble with C++ template operator= What I’m trying to do:

  • 0

Hi all I’m having trouble with C++ template operator=

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.
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:

Graph opertator=:

      MatrixGraph<T>& operator=(MatrixGraph<T>& rhs)
      {
         width = rhs.width;
         height = rhs.height;
         pGraph = rhs.pGraph;
         pitch = rhs.pitch;
         sizeOfGraph = rhs.sizeOfGraph;    
         rhs.Reset();
      }

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.

Graph copy constructor:

   MatrixGraph(MatrixGraph<T>& graph)
   {
        (*this) = graph;
   }

Graph Creator load function:

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;
}

And finally in main.cpp where I’m trying to unit test this I use it:

DIMACSGraphCreator<short> creator;
myGraph = creator.LoadDIMACSGraphFile(instream);

When I try to compile I get this error:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:42:54+00:00Added an answer on May 12, 2026 at 11:42 pm

    The problem is you’re returning by value (correctly) but trying to bind that temporary object to a non-const reference (for the op= parameter). You can’t do this.

    The solution is to change things around, which can result in non-idiomatic code; use an auto_ptr_ref-like construct, which gets around this in a fairly bad-but-encapsulated way; or use r-value references, which are designed for exactly this situation. However, r-value references are only available as part of C++0x, and your compiler may not support them yet.

    Make sure to return *this in your op= as well. Without warnings turned on your compiler may silently (and against the standard) accept that function without a return statement. (I don’t know why.)

    Example of the first solution:

    // move return value into output-parameter:
    void LoadDIMACSGraphFile(std::istream& dimacsFile, MatrixGraph<T>& dest);
    
    // ...
    
    DIMACSGraphCreator<short> creator;
    creator.LoadDIMACSGraphFile(instream, myGraph);
    

    std::auto_ptr is in the the stdlib, and uses a special “holder” class named auto_ptr_ref to implement move semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have some script tags that are not working in Wordpress. If I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
All, I'm using wordpress with this and for some reason the eventDrop stopped working.
All I am trying to do is take a standard range on an excel
All the examples I've come across using Google Maps API seem to show a
All of the examples I've seen of using yield return x; inside a C#
All programs that I develop utilize the default Windows Design template: Besides from changing
All, Say I have the following bit of code: select: function(start, end, allDay) {
All, I am building a small site using PHP. In the site, I receive
All, I am trying to create a table to receive user inputs (UGC). This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.