Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: const reference Parameter Problem  (Read 4071 times)

0 Members and 1 Guest are viewing this topic.

insa

  • Newbie
  • *
  • Posts: 7
    • View Profile
const reference Parameter Problem
« on: January 07, 2010, 01:13:05 pm »
Hey Guys,

I get this compiler error:

error C2664: 'Map::initMap' : cannot convert parameter 4 from 'const PlayerD' to 'PlayerD &'


on that code:

Code: [Select]
Map::Map(int x,int y,ImageHandler& ih,const PlayerD& p) {
initMap(ih,p);
}


initMap is supposed to take a const PlayerD& as well...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
const reference Parameter Problem
« Reply #1 on: January 07, 2010, 01:15:44 pm »
You shoud show the initMap function.
Laurent Gomila - SFML developer

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
const reference Parameter Problem
« Reply #2 on: January 07, 2010, 01:15:56 pm »
Could you show the initMap prototype as well ?

insa

  • Newbie
  • *
  • Posts: 7
    • View Profile
const reference Parameter Problem
« Reply #3 on: January 07, 2010, 01:35:46 pm »
.cpp

Code: [Select]
void Map::initMap(ImageHandler& ih,const PlayerD& p) {
std::vector<Tile> tmp;
float tmpPosX = p.getPosition().x;
float tmpPosY = p.getPosition().y;
int tmpTileX = p.getActTile().x;
int tmpTileY = p.getActTile().y;
}


.hpp

Code: [Select]

public:
Map(int x,int y,ImageHandler&,const PlayerD&);
private:
void initMap(ImageHandler&,const PlayerD&);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
const reference Parameter Problem
« Reply #4 on: January 07, 2010, 01:56:10 pm »
What line of code does the error refer to? Are you showing your real code?

The error says that you try to call Map::initMap with a const PlayerD as parameter 4 instead of a PlayerD&, but that doesn't match the code that you show.
Laurent Gomila - SFML developer

insa

  • Newbie
  • *
  • Posts: 7
    • View Profile
const reference Parameter Problem
« Reply #5 on: January 07, 2010, 02:01:48 pm »
This is the Code as intend it to use in the future, at the moment I also pass the two integers (x and y) to initMap() (therefore it expects to get 4 parameters).

And after my four lines of assigning the temporary int/float vals, i do not touch p anymore.

Should i declare the functions I call from p as well as const?


Edit:

I just solved the problem... I thought my functions WERE const, but in fact only the return type was const.

Code: [Select]
const sf::Vector2f& getPosition(); //as i had id before, wrong
Code: [Select]
const sf::Vector2f& getPosition() const; //as i have it now, correct

Anyway, would be a const sf::Vector2f be the better choice for the return type instead of a reference to it (in terms of speed/code size)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
const reference Parameter Problem
« Reply #6 on: January 07, 2010, 02:14:29 pm »
Quote
I just solved the problem... I thought my functions WERE const, but in fact only the return type was const.

So I guess that the error was pointing to these lines of code, not to the call to initMap... Don't forget to mention it in the future ;)

Quote
Anyway, would be a const sf::Vector2f be the better choice for the return type instead of a reference to it (in terms of speed/code size)?

Vector2f contains only 2 floats, so it doesn't really matter if you return it by value of by reference. However for bigger objects, use const references to avoid unnecessary copies.
Laurent Gomila - SFML developer

insa

  • Newbie
  • *
  • Posts: 7
    • View Profile
const reference Parameter Problem
« Reply #7 on: January 07, 2010, 02:17:39 pm »
The problem was that I was 100% sure that my functions were const... but in fact only the return type was :/

Thanks for the fast answers =)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
const reference Parameter Problem
« Reply #8 on: January 08, 2010, 12:11:35 am »
Quote from: "Laurent"
Vector2f contains only 2 floats, so it doesn't really matter if you return it by value of by reference. However for bigger objects, use const references to avoid unnecessary copies.
In the meantime, I got used to copy sf::Vector2f always. I don't think it will be slower. As a parameter, you can directly modify the copy and don't have to instantiate a local object. Besides, copies are syntactically more beautiful... ;)

However, returning a copy instead of a const reference has a real advantage. Let's look at a const reference implementation, there seems to be everything fine.
Code: [Select]
const sf::Vector2f& Player::GetPosition() const
{
    return MyPosition;
}

Okay, due to an implementation change, MyPosition isn't a member anymore. Instead, the position is calculated from the tile index at which the player is standing (just an example).
Code: [Select]
const sf::Vector2f& Player::GetPosition() const
{
    return sf::Vector2f(MyTileX * TileSize, MyTileY * TileSize);
}

Now we get a problem because we return a reference to a local temporary object*. This wouldn't happen when using a copy. Like this, you can keep the interface. This is particularly important at virtual functions inside a polymorphic hierarchy.

By the way, sorry for little off-topic :)
____
*) Note that the C++ rule which allows const-references to be bound at temporary objects doesn't apply here because we're dealing with return values.
Quote from: "C++ Standard, 12.2/5"
The temporary to which the reference is bound [...] persists for the lifetime of the reference except as specified below. [...]
A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything