S
S
SkyCrusher2020-04-25 15:52:22
C++ / C#
SkyCrusher, 2020-04-25 15:52:22

C++ type value cannot be used to initialize a type entity?

I want to refer to a derived class using a pointer to the base class. Throws an error: "A value of a type cannot be used to initialize an entity of a type." What is wrong and how to fix it?

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <list>

using namespace std;

class Base {
public:

  Base* parent;
  list<Base*> childrens;
  string name;
  bool status;
  bool isRoot;

  Base(string paramName, Base* paramParent) {
    name = paramName;
    parent = paramParent;
  }

  Base(string paramName) {
    name = paramName;
    isRoot = true;
  }

  void AddChildren(Base* children) {
    childrens.push_back(children);
  }

  void SetStatus(bool paramStatus) {
    status = true;
  }
};

class root : Base {
public:
  root(string paramName) : Base(paramName) {
  }
};

int main() {
  root *Root = new root("root");
  Base *b = &Root;
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-04-25
@SkyCrusher

Base *b = reinterpret_cast<Base*>(root)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question