-1

error C2440: '=' : cannot convert from 'const BWAPI::UpgradeType' to 'const BWAPI::Type *'

at this line

this->generalType = type;   

what is the problem? since UnitType extends Type shouldn't be permitted?

class CombatEvent {

public:
    CombatEvent& setUnitType(const UnitType& type);
    const Type* getGeneralType() const;

private:
    UnitType unitType;
    const Type* generalType;
}

// implementation

CombatEvent& CombatEvent::setUnitType(const UnitType& type) {

    this->generalType = type;
    this->unitType = type;

    return *this;
 }
thiagoh
  • 7,098
  • 8
  • 51
  • 77

3 Answers3

4

You need to take the address:

this->generalType = &type;
Alexander Chertov
  • 2,070
  • 13
  • 16
  • but when i do setUnitType(const UnitType& type) the & isn't already passing the reference?? – thiagoh Aug 22 '12 at 08:36
  • 1
    The problem is that `const UnitType& type` is a **reference** and `CombatEvent::generalType` is a **pointer**. You can't assign a reference to pointer. – Alexander Chertov Aug 22 '12 at 08:38
0

You're assigning a reference to a pointer. Correct code:

this->generalType = &type; 

You may have a look at these links

References vs. Pointers

What are the differences between pointer variable and reference variable in C++?

Community
  • 1
  • 1
Andriy
  • 8,486
  • 3
  • 27
  • 51
0

There's a fundamental problem in your use of BWAPI Types.

1. BWAPI::Type is a templated class that contains all the utility functions common among all types. There is no need to use it. There is no way to know if it is a UnitType or UpgradeType, it only contains an integer as the underlying type.

2. There is no need to turn BWAPI Types into pointers or references. The compiler resolves it to an integer, nothing fancy about it. Would you use const int& everywhere in your code?