Cannot convert to base class.

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):

Spatial.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once

namespace graphics {

    
    class Scene;
    class Node ;
   
    class Spatial {
    public:

        Spatial();
		
        /** Set the parent of this spatial to node or to null */
        virtual void 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() {}

}




Node.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

#include "Spatial.h"

namespace graphics {

    using namespace std;

    class Spatial;
    
    class Node : public Spatial {
    public:
        Node();
      
        virtual ~Node();

    

    };
}



Node.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "StdAfx.h"
#include "Node.h"

namespace graphics {

	Node::Node(){}

	Node::~Node(){}
   
}



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".

Thank you for help in advice.

Nevermind - misread the code in question. My apologies for any confusion.
Last edited on
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.
 
 this->parent = reinterpret_cast<Spatial *>(parent); 


edit: As an alternative, you could try including node.h at line 3 of spatial.cpp.
Last edited on
As the error message suggests, the way around this is to use reinterpret_cast.
Doesn't it risky if Spatial and Node pointers are not pointing to exact same memory (i.e. if there is address arithmetics involved in conversion?)
Thank you @AbstractionAnon including node.h worked . I should probably learn more about the inclusion stuff.
Topic archived. No new replies allowed.