1

When I try to compile my program, my Race.cc file has an error.

 error: cannot bind non-const lvalue reference of type ‘Position&’ to an rvalue of type ‘Position’
 view.update(tortoise->getCurrPos(), tortoise->getCurrPos(), tortoise->getAvatar());
             ~~~~~~~~~~~~~~~~~~~~^~

I'm struggling to figure out how to resolve these and would appreciate some help. I don't know why calling update(Position&, Position&, char) from View.cc gets an error on the first parameter only and not the second considering I'm inputting the exact same thing. The line tortoise->getCurrPos() should return a Position data type as shown in Runner.cc, and the parameter asks for a Position type so I don't get why it's not working.

Race.cc

Race::Race(){ 
    Runner* tortoise = new Runner("Tortoise", 'T', 100, 1);
  
    view.update(tortoise->getCurrPos(), tortoise->getCurrPos(), tortoise->getAvatar());
}

View.cc

void update(Position& oldPos, Position& newPos, char avatar){
}
steveo
  • 51
  • 5

1 Answers1

1

Your update() is looking to take a Position by reference so that it can make modifications and have them percolate back to the object you're passing. However, tortoise->getCurrPos() returns a Position copy. If you want the changes in update() to affect your tortoise's Position member, you'll need to have getCurrPos() return its Position by reference.

Position Runner::getCurrPos() { 

Should become

Position& Runner::getCurrPos() { 

For more, you can read up at What is a reference variable in C++?

scohe001
  • 15,110
  • 2
  • 31
  • 51