下面是完整的代码到我的项目(没有代码已被省略):为什么编译器会说“Static_cast与哪个不相关”?
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
typedef enum{
MAJOR_BUTTON_IGNORED_FIRST = 0,
MAJOR_BUTTON_BLOCKS,
MAJOR_BUTTON_IGNORED_LAST,
}MAJOR_BUTTON_TYPE;
typedef enum{
BLOCK_IGNORED_FIRST = 0,
BLOCK_ORDINARY_SMASHABLE,
BLOCK_IGNORED_LAST,
}THING_TYPE;
class Datar{
private:
public:
MAJOR_BUTTON_TYPE majorType;
std::string majorButtonString;
std::vector<THING_TYPE>thingTypes;
std::unordered_map<THING_TYPE, std::string>thing2String;
Datar(){
}
virtual ~Datar(){};
virtual void initDatar(){};
};
class DatarBlock : public Datar{
private:
public:
virtual void initDatar(){
majorType = MAJOR_BUTTON_BLOCKS;
majorButtonString = "MajorButtonNames::blocks";
}
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
DatarBlock* db = new DatarBlock();
Datar* blah = static_cast<Datar*>(db); //Compiler error here ****
return 0;
}
在标线,还有它说一个编译器错误:
的main.cpp:62: 19:从的static_cast“DatarBlock *”到“达塔尔*”,这 没有继承关系,是不允许
为什么编译器报告为错误?
编辑:我使用的Xcode 7.2(LLVM 7.0),C++标准库= libc的++(LLVM标准库用C++ 11支撑),C++语言方言= GNU ++ 11
[无法重现](http://rextester.com/TPGX82687)。你使用什么编译器? – songyuanyao
@songyuanyao我使用Xcode 7.2(LLVM 7.0),C++标准库= libC++(支持C++ 11的LLVM标准库),C++语言Dialect = GNU ++ 11。 –
你为什么发布你刚才删除的同一个问题? http://stackoverflow.com/questions/36785524/why-does-the-compiler-think-that-these-two-classes-are-not-related-by-inheritanc/ –