Instance of Forward declared Class - c++

It's the follow up question of this: error C2504 circular inclusion.
Child if forward declared in parent's header.
It is not included, so the complier wont find Child? Then how do i instantiate a new Child object from Parent object.
Parent.h
#pragma once
#include <vector>
using std::vector;
class Child;
class Parent
{
public:
Parent();
void GiveBirth();
~Parent();
vector<Child*> children;
};
Parent.cpp
#include "stdafx.h"
#include "Parent.h"
Parent::Parent()
{
}
void Parent::GiveBirth()
{
Child ch = Child(); //Error: incomplete type is not allowed
}
Parent::~Parent()
{
}
Child.h
#pragma once
#include "Parent.h"
class Child : Parent
{
public:
Child();
~Child();
};
Child.cpp
#include "stdafx.h"
#include "Child.h"
Child::Child()
{
}
Child::~Child()
{
}

Here are some reading list for you.
http://en.wikipedia.org/wiki/Opaque_pointer
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Idioms#Pointer_To_Implementation_.28pImpl.29
Parent.cpp
#include "stdafx.h"
#include "Parent.h"
#include "Child.h"
Parent::Parent()
{
}
void Parent::GiveBirth()
{
//Child ch = Child(); //Error: incomplete type is not allowed
children.push_back(new Child());
}
Parent::~Parent()
{
}

Related

How can I fix this callback include problem?

I am kind of new to C++ (and StackOverflow). I am trying to get something to work, but I have some #include problems.
I want to call a callback I made (from here), but I am struggling to do this.
This is my code so far. When I include child.hpp in the someclass.hpp file (because it needs information about Child for Callback<Child>), it has a looped include and the compiler crashes.
I have read about forward declarations (would be class Child; in the someclass.hpp file), and after trying I figured out this works, but I also read different opinions about this.
I have all .hpp files guarded with #ifndef CLASSNAME #define CLASSNAME ... #endif
Do I need to change my entire design, or what is the best option in my case?
base.hpp
#include "someclass.hpp"
class Base
{
protected:
unique_ptr<SomeClass> someClass;
};
base.cpp
#include "base.hpp"
Base::Base()
{
this->someClass = make_unique<SomeClass>();
}
child.hpp
#include "base.hpp"
class Child : public Base
{
public:
void callbackFunction(std::string data);
unique_ptr<Callback<Child>> callback;
};
child.cpp
#include "child.hpp"
void Child::callbackFunction(std::string data)
{
/*does something*/
}
Child::Child()
{
this->callback = make_unique<Callback<Child>>(this, &Child::callbackFunction);
//I can call this->callback->call(data); here without problems
this->someClass->setCallback(this->callback);
//^^^^^^^^^^^^^^^ == base.someClass
}
someclass.hpp
#include "child.hpp" // < does crash compiler due to loop
//> someclass.hpp uses child.hpp
//> child.hpp uses base.hpp
//> base.hpp uses someclass.hpp
// and thus loop
class SomeClass
{
public:
void someFunction(std::string data);
void setCallback(unique_ptr<Callback<Child>> callback);
unique_ptr<Callback<Child>> callbackInstance;
};
someclass.cpp
//not 100% sure about the type of this parameter
void setCallback(unique_ptr<Callback<Child>> callback)
{
this->callbackInstance = callback;
}
void SomeClass::someFunction(std::string data)
{
//here I want to call this "Child::callbackFunction" which should go like "this->callbackInstance->call(data)" ?
}
also in someclass.hpp
template<class T>
class Callback
{
public:
Callback(T* instance, void (T::*function)(std::string))
{
this->callbackInstance = instance;
this->callback = function;
}
void call(std::string data)
{
(callbackInstance->*callback)(data);
}
private:
T *callbackInstance;
void (T::*callback)(std::string);
};
To solve the mentioned error("expected class-name before '{' token on child.hpp") you should remove the #include "someclass.hpp" from base.hpp and replace it with a forward declaration for class SomeClass as shown below.
base.hpp
#ifndef BASE_H
#define BASE_H
//NO NEED TO INCLUDE someclass.hpp
#include <memory>
class SomeClass;//FORWARD DECLARE SomeClass
class Base
{
std::unique_ptr<SomeClass> someClass;
public:
//add declaration for default constructor
Base();
};
#endif
base.cpp
#include "base.hpp"
#include "someclass.hpp"
//other things here
Base::Base()
{
this->someClass = std::make_unique<SomeClass>();
}
child.hpp
#ifndef CHILD_H
#define CHILD_H
#include "base.hpp"
#include <memory>
#include "someclass.hpp"
class Child : public Base
{
public:
void callbackFunction(std::string data);
std::unique_ptr<Callback<Child>> callback;
//add declaration for default constrcutor
Child();
};
#endif
child.cpp
#include "child.hpp"
void Child::callbackFunction(std::string data){
/*does something*/
}
Child::Child()
{
this->callback = std::make_unique<Callback<Child>>(this, &Child::callbackFunction);
//I can call this->callback->call(data); here without problems
}
someclass.hpp
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include <string>
//REMOVED include child.hpp from here
class SomeClass
{
public:
void someFunction(std::string data);
//I think I need an instance of Callback<Child> here?
};
template<class T>
class Callback
{
public:
Callback(T* instance, void (T::*function)(std::string))
{
this->callbackInstance = instance;
this->callback = function;
}
void call(std::string data)
{
(callbackInstance->*callback)(data);
}
private:
T *callbackInstance;
void (T::*callback)(std::string);
};
#endif
someclass.cpp
#include "someclass.hpp"
void SomeClass::someFunction(std::string data)
{
//here I want to call this "Child::callbackFunction" which should go like "this->callbackInstance->call(data)" ?
}
The above program compiles and executes successfully as can be seen here.
Summary
Some of the changes that i made are listed below:
Removed unnecessary includes
Added declarations for default constructor in child.hpp and base.hpp
Added include guards in all headers.

How to override Function in C++

Currently, I am working on c++ project I want to know how I can send an instance of a child class to function use parent as a parameter and execute a function in a child here is an example: I want Child print function to be called
Parent.h
#ifndef UNTITLED_PARENT_H
#define UNTITLED_PARENT_H
class Parent {
public:
virtual void printData();
};
#endif
Parent.cpp
#include "Parent.h"
#include <iostream>
using namespace std;
void Parent::printData() {
cout<<"Parent"<<endl;
}
Child.h
#ifndef UNTITLED_CHILD_H
#define UNTITLED_CHILD_H
#include "Parent.h"
class Child : public Parent{
public:
void printData();
};
#endif
Child.cpp
#include "Child.h"
#include <iostream>
using namespace std;
void Child::printData() {
cout<<"Child"<<endl;
}
ParentUser.h
#ifndef UNTITLED_PARENTUSER_H
#define UNTITLED_PARENTUSER_H
#include "Parent.h"
class ParentUser {
public:
void printer(Parent p);
};
#endif
ParentUser.cpp
#include "ParentUser.h"
void ParentUser::printer(Parent p) {
p.printData();
}
main.cpp
#include <iostream>
#include "Parent.h"
#include "Child.h"
#include "ParentUser.h"
int main() {
Child child;
ParentUser parentUser;
parentUser.printer(child);
return 0;
}
Your function void printer(Parent p); will create a new object of type Parent using a copy constructor your compiler automagically creates for you. You need to change it to take a reference instead:
void printer(Parent& p);
This will make sure that p is actually a reference to child, not a new Parent created from child using a copy constructor.
What's happening here is also called object slicing, as the copy is a parent type, which does not have any of the members defined in the child class.

error C2504 circular inclusion

i have Parent and Child classes. Child inherits from Parent. I want to store in a vector of Child objects the children of a parent object.
I include the Child header into the Parent header, but i have to include the Parent header into the Child header (since it inherits from Parent).
How do i overcome this circular inclusion?
Parent.h
#pragma once
#include <vector>
#include "Child.h"
using std::vector;
class Parent
{
public:
Parent();
~Parent();
vector<Child> children;
};
Parent.cpp
#include "stdafx.h"
#include "Parent.h"
Parent::Parent()
{
}
Parent::~Parent()
{
}
Child.h
#pragma once
#include "Parent.h"
class Child : Parent
{
public:
Child();
~Child();
};
Child.cpp
#include "stdafx.h"
#include "Child.h"
Child::Child()
{
}
Child::~Child()
{
}
Errors
child.h(4): error C2504: 'Parent' : base class undefined
parent.h(11): error C2065: 'Child' : undeclared identifier
Forward declare Child, and store pointer inside of vector.
Parent.h
#pragma once
#include <vector>
using std::vector;
class Child;
class Parent
{
public:
Parent();
~Parent();
vector<Child*> children;
};

How to access a variable from the parent class

How can I access a variable from a parent class? I thought the below code would do this, but when I try to print out the value of name in Controller.cpp I get the error:
Member access into incomplete type 'TestApp'
TestApp.cpp
#include "cinder/app/AppNative.h"
#include "Controller.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class TestApp : public AppNative
{
public:
void setup();
void update();
string name = "Parent";
Controller controller;
};
void TestApp::setup()
{
controller.setup(this);
}
void TestApp::update()
{
controller.update();
}
CINDER_APP_NATIVE( TestApp, RendererGl )
Controller.h
#pragma once
class TestApp;
class Controller
{
public:
void setup(TestApp* parent);
void update();
TestApp* p;
};
Controller.cpp
#include "Controller.h"
void Controller::setup(TestApp* parent)
{
p = parent;
}
void Controller::update()
{
std::cout << p->name << std::endl;
}
This has nothing to do with parent classes. TestApp is defined in TestApp.cpp, it's not visible to the code in Controller.cpp. You need to move the definition of TestApp to a header file (called TestApp.h say) and then #include "TestApp.h" in Controller.cpp.
You need to put the TestApp class definition in a header, and include that in TestApp.cpp and Controller.cpp.
Class Controller knows nothing about what dara members class TestApp has. It knows only that there is class TestApp that is defined somewhere else.

C++ inheritance and constructors, destructors

//Parent.h
class Parent{
public:
Parent(){}
~Parent(){}
virtual void func1() = 0;
};
//Child.h
#include "Parent.h"
class Child : public Parent{
int x, y;
public:
Child() : Parent(){ //constructor
}
virtual void func1();
};
//Child.cpp
#include "Child.h"
void Child::Parent::func1(){
}
This compiles fine, however, I want to put the implementation of the constructor (and destructor) of Child class in its cpp file, is it possible? How?
I've tried the code below but it throws undefined reference to vtable for Child
Child::Child() : Parent(){ //in the cpp
}
Child(); //in the header file
Child():Parent(); //also tried this one
A couple of things for you to do:
Guard-post your header files to prevent unintended multiple inclusion.
Make your Parent destructor virtual
Initialize your non-auto member variables to determinate values.
Your final layout can look something like this.
Parent.h
#ifndef PARENT_H_
#define PARENT_H_
class Parent
{
public:
Parent() {};
virtual ~Parent() {};
public:
virtual void func1() = 0;
};
#endif // PARENT_H_
Child.h
#ifndef CHILD_H_
#define CHILD_H_
#include "Parent.h"
class Child : public Parent
{
int x,y;
public:
Child();
virtual ~Child();
virtual void func1();
};
#endif
Child.cpp
Child::Child()
: Parent() // optional if default
, x(0), y(0) // always initialize members to determinate values
{
}
Child::~Child()
{
}
void Child::func1()
{
}
$ cat Parent.h
#ifndef GUARD_PARENT_H_
#define GUARD_PARENT_H_
class Parent{
public:
Parent(){}
~Parent(){}
virtual void func1() = 0;
};
#endif /* GUARD_PARENT_H_ */
$ cat Child.h
#ifndef GUARD_CHILD_H_
#define GUARD_CHILD_H_
#include "Parent.h"
class Child : public Parent{
int x, y;
public:
Child();
virtual void func1();
};
#endif /* GUARD_CHILD_H_ */
$ cat Child.cpp
#include "Child.h"
Child::Child() : Parent() {
}
void Child::func1(){
}
$ cat try.cc
#include "Child.h"
int main() {
Child c;
}
$ g++ try.cc Child.cpp
$ ./a.out
$