Unterhalb der vollständige Code zu meinem Projekt ist (kein Code weggelassen wurde):Warum sagt der Compiler "Static_cast from, auf die nicht verwandt sind"?
#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;
}
An der Linie markiert, gibt es einen Compiler-Fehler, die sagt:
main.cpp: 62: 19: static_cast von ‚DatarBlock *‘ auf ‚Datar *‘, die nicht durch Vererbung verwandt, nicht
erlaubt
Warum funktioniert der Compiler Bericht es als einen Fehler entdeckt?
EDIT: Ich bin mit Xcode 7.2 (LLVM 7.0), C++ Standard Library = libC++ (LLVM Standard-Bibliothek mit C++ 11-Unterstützung), C++ Sprache Dialect = 11
[Kann nicht reproduziert werden] (http://rextester.com/TPGX82687). Welchen Compiler benutzen Sie? – songyuanyao
@songyuanyao Ich verwende Xcode 7.2 (LLVM 7.0), C++ - Standardbibliothek = libC++ (LLVM-Standardbibliothek mit C++ 11-Unterstützung), C++ - Sprachdialekt = GNU ++ 11. –
Warum postest du dieselbe Frage, die du gerade gelöscht hast? http://stackoverflow.com/questions/36785524/why-does-the-compiler-think-that-three-classes-are-not-related-by-inheritanc/ –