Hello. I'm writing for help. I have an issue related to the inheritance. Have 2 classess: "Spatial" and "Node". I want to assign a Spatial* to Node* in a following manner (please note the error message comment):
#pragma once
namespace graphics {
class Scene;
class Node ;
class Spatial {
public:
Spatial();
/** Set the parent of this spatial to node or to null */
virtualvoid setParent(Node* parent) ;
~Spatial();
/** The parent element of the spatial */
Spatial* parent;
protected:
private:
};
}
Spatial.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "StdAfx.h"
#include "Spatial.h"
namespace graphics {
Spatial::Spatial(){}
void Spatial::setParent(Node* parent) {
this->parent = parent; // here the error occurs, see below for explanation
}
Spatial::~Spatial() {}
}
So the error that relates to the marked line is (in VS2010):
1>c:\temp\cppcast\castproblem\spatial.cpp(10): error C2440: '=' : cannot convert from 'graphics::Node *' to 'graphics::Spatial *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
The error is pretty the same in MingW g++. It says that the classes Spatial and Node are unrelated. Is there a way to get the correct result? I hope you can see the intents in code. I want to Node and Spatial know about each other and write pointer of type "Node" to type "Spatial".
At Spatial.cpp line 10, you're trying to assign a Node (child class) to a Spatial pointer. Normally, this would be fine, except that Node has been declared forward and the compiler only knows that Node is a class and does not yet know that Node is a child of Spatial (node.h hasn't been read).
As the error message suggests, the way around this is to use reinterpret_cast.