bind gives error C2065: '_1' : undeclared identifier
Trying to make one of the programs in "Efficient C++" work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include<iostream>
#include<functional>
class GameCharacter; // forward declaration
int defaultHealthCalc(const GameCharacter& gc) {
return 10;
}
class GameCharacter{ // Base class
public:
typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc;
explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc) :healthFunc(hcf){} // constructor
void setHealthFunction(HealthCalcFunc hcf){ // resets object's health calculator
healthFunc = hcf;
}
int healthValue() const { // non-virtual fn, derived classes inherit this
return healthFunc(*this);
}
private:
HealthCalcFunc healthFunc; // pointer to function
};
class EvilBadGuy : public GameCharacter{
public:
explicit EvilBadGuy(HealthCalcFunc hcf = defaultHealthCalc) : GameCharacter(hcf) {} // constructor
};
class GameLevel{
public:
GameLevel(int l = 1) : level(l){}
int health(const GameCharacter& gc) const {
return level * 10000;
}
void setLevel(int l) {
level = l;
}
private:
int level;
};
int main(){
GameLevel currentLevel; // default construct
EvilBadGuy ebg3(std::tr1::bind(&GameLevel::health, std::tr1::cref(currentLevel),_1));
std::cout << ebg3.healthValue() << std::endl;
return 0;
}
| |
The "bind" gives the error in the title though it seems it should work based on the book.
With g++, I have to add the following:
1 2 3
|
#include <tr1/functional>
using namespace std::tr1::placeholders;
| |
You probably just need the second line.
The second line helped. -Thanks.
Topic archived. No new replies allowed.