【问题标题】:A value of type X cannot be used to initialize an entity of type XX 类型的值不能用于初始化 X 类型的实体
【发布时间】:2018-07-16 06:38:06
【问题描述】:
struct Value {
    struct Array;
    struct Dict;
    struct Primitive;

    Value() {}
    virtual ~Value() {}

    virtual Array * asArray() { assert(false); }
    virtual Dict * asDict() { assert(false); }
    virtual Primitive * asPrimitive() { assert(false); }

    int asInt();
    double asDouble();
    std::string asDate();
    std::string asString();
};

struct Value::Array : public Value {
    std::vector<Value> m_data;

    Array() {}
    ~Array() {}

    Value::Array * asArray() { 
        Value::Array * result = this; 
        return result;
    }
};

Value::Array * result = this; 的声明给了我这个错误...

E0144 a value of type "Value::Array *" cannot be used to initialize an entity of type "Value::Array *".

【问题讨论】:

  • 啊哈。我以为你在编辑之前说我愚蠢。这是否意味着我可以忽略它并且我的代码仍然可以编译?
  • 我盯着这个,但我不知道 C++ 代码有什么问题。关于编译器,这些年来我不时看到这样的事情。 (我个人最喜欢的是:“bool is not compatible with bool”)看来连编译器构造函数都会出错... :-)
  • 是的,这段代码应该可以正常工作。问题是智能感知使用自己的花哨的解析器,因此智能感知错误通常与编译器错误无关。
  • 代码没有问题。我们应该始终能够区分编译器/链接器错误和 IDE 相关错误。

标签: c++ visual-c++


【解决方案1】:

这个问题可以在更简单的代码中看到

enum SPELLSTATUS { NEW, FAIL, OK } spellStatus = SPELLSTATUS::NEW;

enum SPELLSTATUS { NEW, FAIL, OK } spellStatus = NEW;

E0144   a value of type "SPELLSTATUS" cannot be used to initialize an entity of type "SPELLSTATUS"  NcEdit  D:\Systems\Editor2019\Spell.cpp 30  

VS2019. 

编译并运行良好。

【讨论】:

  • 请编辑您的答案,以代码格式输入代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2018-07-23
  • 2022-01-25
  • 2016-05-17
相关资源
最近更新 更多