-1

错误 C2440:“=”:无法从“const BWAPI::UpgradeType”转换为“const BWAPI::Type *”

在这条线上

this->generalType = type;   

问题是什么?因为 UnitType extends Type 不应该被允许?

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;
 }
4

3 回答 3

4

您需要获取地址:

this->generalType = &type;
于 2012-08-22T08:28:59.673 回答
0

您正在分配对指针的引用。正确代码:

this->generalType = &type; 

你可以看看这些链接

引用与指针

C++中的指针变量和引用变量有什么区别?

于 2012-08-22T08:29:37.923 回答
0

您在使用 BWAPI 类型时存在一个基本问题。

1. BWAPI::Type 是一个模板类,包含所有类型中通用的所有实用函数。没有必要使用它。没有办法知道它是 UnitType 还是 UpgradeType,它只包含一个整数作为基础类型。

2.不需要将 BWAPI 类型转换为指针或引用。编译器将其解析为整数,没什么特别的。你会const int&在你的代码中到处使用吗?

于 2015-09-20T04:35:20.240 回答