简体   繁体   English

C ++ C2440错误无法从'const BWAPI :: UpgradeType'转换为'const BWAPI :: Type *'

[英]c++ C2440 error cannot convert from 'const BWAPI::UpgradeType' to 'const BWAPI::Type *'

error C2440: '=' : cannot convert from 'const BWAPI::UpgradeType' to 'const BWAPI::Type *' 错误C2440:'=':无法从'const BWAPI :: UpgradeType'转换为'const BWAPI :: Type *'

at this line 在这条线

this->generalType = type;   

what is the problem? 问题是什么? since UnitType extends Type shouldn't be permitted? 由于UnitType扩展类型不应该被允许?

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

您需要填写地址:

this->generalType = &type;

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++? C ++中的指针变量和引用变量之间有什么区别?

There's a fundamental problem in your use of BWAPI Types. 使用BWAPI类型存在一个基本问题。

1. BWAPI::Type is a templated class that contains all the utility functions common among all types. 1. BWAPI :: Type是一个模板化类,其中包含所有类型之间通用的所有实用程序函数。 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. 无法知道它是UnitType还是UpgradeType,它仅包含一个整数作为基础类型。

2. There is no need to turn BWAPI Types into pointers or references. 2.无需将BWAPI类型转换为指针或引用。 The compiler resolves it to an integer, nothing fancy about it. 编译器将其解析为整数,对此一无所知。 Would you use const int& everywhere in your code? 您会在代码的任何地方使用const int&吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C2440:'=':无法从'const BWAPI :: UnitType *'转换为'BWAPI :: Type *' - error C2440: '=' : cannot convert from 'const BWAPI::UnitType *' to 'BWAPI::Type *' 错误C2440:'=':无法从'const char [2]'转换为'char' - error C2440: '=' : cannot convert from 'const char [2]' to 'char' 错误 C2440:“正在初始化”:无法从“const mynamespace::mytype*”转换为“const T*” - error C2440: 'initializing' : cannot convert from 'const mynamespace::mytype*' to 'const T*' C2440:“ =”:无法从“ const char [9]”转换为“ char *” - C2440: '=': cannot convert from 'const char [9]' to 'char*' 返回NULL但得到错误C2440:'return':无法从'int'转换为'const&' - returning NULL but getting error C2440: 'return' : cannot convert from 'int' to 'const &' const 转发参考给出错误 C2440:'initializing': cannot convert from 'const std::string' to 'const std::string &&' - const forwarding reference gives error C2440: 'initializing': cannot convert from 'const std::string' to 'const std::string &&' 错误 C2440:“正在初始化”:无法从“const char *”转换为“TCHAR *” - error C2440: 'initializing': cannot convert from 'const char *' to 'TCHAR *' 错误C2440:“正在初始化”:无法从“ const temp1”转换为“ temp2” - error C2440: 'initializing' : cannot convert from 'const temp1' to 'temp2' 错误C2440:“正在初始化”:在Visual Studio 2008中无法从“ const jchar *”转换为“ LPCWSTR” - error C2440: 'initializing' : cannot convert from 'const jchar *' to 'LPCWSTR' in Visual Studio 2008 错误C2440:'初始化':无法从'const int'转换为'int *' - error C2440: 'initializing' : cannot convert from 'const int' to 'int *'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM