Reputation:
I'm having a problem in my code, I hacked up this simple example to illustrate it.
I have a Player class that has a public enum PlayerType. I have a Manager class with a createPlayer method that accepts a reference to a player and a player type.
But I can't get it to compile -- note, I can't change the signature of the createPlayer method, each param must be an object reference.
What am I doing wrong?
Player.h
#ifndef PLAYER_H
#define PLAYER_H
using namespace std;
class Player {
public:
Player();
enum PlayerType { FORWARD, DEFENSEMAN, GOALIE };
void setType(PlayerType);
private:
PlayerType type;
};
#endif
Player.cc
#include <iostream>
using namespace std;
#include "Player.h"
Player::Player() {
}
void Player::setType(PlayerType type) {
this->type = type;
}
Manager.h
#ifndef MANAGER_H
#define MANAGER_H
using namespace std;
#include "Player.h"
class Manager {
public:
void createPlayer(Player& player, PlayerType& playerType);
};
#endif
Manager.cc
#include <iostream>
using namespace std;
#include "Player.h"
#include "Manager.h"
void Manager::createPlayer(Player& player, PlayerType& playerType) {
cout << "inside Manager::createPlayer" << endl;
}
Main.cc
#include <iostream>
using namespace std;
#include "Player.h"
#include "Manager.h"
int main() {
Manager manager;
Player player;
manager.createPlayer(player, Player::FORWARD);
return 0;
}
When I compile gcc -o a.out *.cc *.h
I get this error:
In file included from Main.cc:6:0:
Manager.h:10:39: error: ‘PlayerType’ has not been declared
Main.cc: In function ‘int main()’:
Main.cc:12:47: error: no matching function for call to ‘Manager::createPlayer(Player&, Player::PlayerType)’
Main.cc:12:47: note: candidate is:
Manager.h:10:10: note: void Manager::createPlayer(Player&, int&)
Manager.h:10:10: note: no known conversion for argument 2 from ‘Player::PlayerType’ to ‘int&’
In file included from Manager.cc:7:0:
Manager.h:10:39: error: ‘PlayerType’ has not been declared
Manager.cc:9:44: error: ‘PlayerType’ has not been declared
Manager.h:10:39: error: ‘PlayerType’ has not been declared
What do I do to make this work?
Upvotes: 0
Views: 720
Reputation: 310950
The problem is that in this call
manager.createPlayer(player, Player::FORWARD);
the second argument is a temporary object, You may not bind non const reference with a temporary object.
So you should declare function as
void Manager::createPlayer(Player& player, const Player::PlayerType& playerType)
Though I do not see a great sense to use the reference. I would declare the function simpler
void Manager::createPlayer(Player& player, Player::PlayerType playerType)
withou the reference.
Upvotes: 1
Reputation: 16867
to be able to use a reference you need to store the value into a variable.
int main() {
Manager manager;
Player player;
Player::PlayerType t = Player::FORWARD;
manager.createPlayer(player,t);
return 0;
}
Upvotes: 2
Reputation: 754
You need to change
void createPlayer(Player& player, Player::PlayerType& playerType);
to
void createPlayer(Player& player, const Player::PlayerType& playerType);
Passing Player::FORWARD as a parameter creates a temporary PlayerType value that can only be passed as a const reference or by value.
Upvotes: 1