How can I fix this callback include problem? - c++

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.

Related

function does not take 1 argument - Observer/Pattern

i am new to c++ programming and now faceing this "simple" problem for a while. I am implementing a simple step of a Observer-Pattern with the classes: Observer and Observable.
#ifndef OBSERVER_H
#define OBSERVER_H
#include "observable.h"
class Observer{
public:
virtual ~Observer() = default;
virtual void update(Observable* obs ) = 0;
};
#endif // OBSERVER_H
the Observabel Class Looks like that:
#ifndef OBSERVABLE_H
#define OBSERVABLE_H
#include <vector>
#include "observer.h"
class Observable
{
public:
Observable();
virtual ~Observable() = default;
void attach(Observer &o);
void detach(Observer &o);
void notify();
private:
std::vector<Observer*> observerlist;
};
#endif // OBSERVABLE_H
c++- file
#include "observable.h"
#include <algorithm>
void Observable::attach(Observer &o) { observerlist.push_back(&o); }
void Observable::detach(Observer &o)
{
observerlist.erase(std::remove(observerlist.begin(), observerlist.end(), &o));
}
void Observable::notify()
{
for (Observer* obs : observerlist) {
obs->update(this); // Here the IDE Shows the Error
}
}
Error:
C:\U...\observable.cpp:16: error: C2660: "Observer::update": function does not take 1 Argument
I really hope one of you can help.
greetings

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.

<Windows.h> c++ threads and inheritance

Hello guys I'm working on a Thread class and a CountingThread class inherated from Thread class including Synchronized Counter using library. But while creating this CountingThread class I'm having a problem of "incomplete type is not allowed" So i would be happy if you give some advise if I'm forming this Thread abstract class in a bad structure or say what am I doing wrong. (FYI i have to keep classes and methods because it's an assignment)
#ifndef _THREAD_H_
#define _THREAD_H_
#include <Windows.h>
#include <iosfwd>
class Thread{
private:
HANDLE hThread;
int idThread;
public:
Thread(LPTHREAD_START_ROUTINE fnct){ // here I'm trying to get a function and create thread with it
hThread = CreateThread(NULL, 0,fnct,NULL,0,(LPDWORD)&idThread);
}
virtual void main()=0;
void suspend(){
SuspendThread(hThread);
}
void resume(){
ResumeThread(hThread);
}
void terminate(){
TerminateThread(hThread,0);
}
static void sleep(int sec){
Sleep(sec*1000);
}
};
#endif
CountingThread.h
#ifndef _COUNTINGTHREAD_H_
#define _COUNTINGTHREAD_H_
#include "SynchronizedCounter.h"
#include "Thread.h"
class CountingThread :public Thread{
private:
SynchronizedCounter counter;
public:
CountingThread(counter.increment()){ // here I'm having the error "incomplete type on counter"
} // I want to create thread with the counter.increment function
};
#endif
SynchronizedCounter.h
#ifndef SYNCHRONIZEDCOUNTER_H_
#define SYNCHRONIZEDCOUNTER_H_
#include "Mutex.h"
#include <iosfwd>
class SynchronizedCounter{
private:
int count;
public:
SynchronizedCounter();
SynchronizedCounter(int);
void increment();
int value();
friend std::ostream &operator <<(std::ostream& output, const SynchronizedCounter& counter)
{
output << counter.count << endl;
return output;
}
};
#endif
and synchronizedCounter::increment
void SynchronizedCounter::increment(){
Mutex mut;
mut.lock;
count++;
mut.unlock;
}
Seems to to be a syntax error. You should define an argument here :
So it should be:
class CountingThread :public Thread{
private:
SynchronizedCounter counter;
public:
CountingThread()
{
counter.increment())
//... etc
} // I want to create thread with the counter.increment function
//...
};
Anyway as counter.increment() returns void, you cannot pass it as parameter.

putting some class methods in different .cc file

I have a bit of code of the following format contained in a single .h and .cc file:
myClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
class myClass
{
public:
myClass(); // constructor
~myClass(); // destructor
void classMethod1 ();
void classMethod2 ();
int memberVarable1;
int memberVariable2;
};
#endif
and myClass.cc:
#include "myClass.h"
myClass::myClass(){
// stuff
}
myClass::~myClass(){
// stuff
}
void myClass::classMethod1 (){
// stuff
}
void myClass::classMethod2 (){
// stuff
}
All of this is working fine. However my project is getting quite large and I'm about to add a set of new functionality. Instead of clogging up myClass.h and myClass.cc I want to put some new methods in another .cc file. I don't seem to be able to get this to work though.
myClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "secondFile.h"
class myClass
{
public:
myClass(); // constructor
~myClass(); // destructor
void classMethod1 ();
void classMethod2 ();
int memberVarable1;
int memberVariable2;
};
#endif
and myClass.cc:
#include "myClass.h"
#include "secondFile.h"
myClass::myClass(){
// stuff
}
myClass::~myClass(){
// stuff
}
void myClass::classMethod1 (){
// stuff
}
void myClass::classMethod2 (){
// stuff
}
secondFile.h:
#ifndef SECONDFILE_H
#define SECONDFILE_H
void someNewMethod();
#endif
secondFile.cc
#include "secondFile.h"
void someNewMethod(){
// can't see classMethod1()
}
In every source file, you need to include every header file that declares functions, etc. you want to use.
So in your case, it seems like you want secondFile.cc to contain
#include "myClass.h"
#include "secondFile.h"
void someNewMethod(){
// can't see classMethod1()
}
Btw, what you are doing is quite common to do in practice. Sometimes, I go even further than what you suggest, and implement the various methods of a single class in multiple source files. For large, complicated classes, this helps speed up the development cycle because I only have to recompile a fraction of the class implementation if I only made a small change. Example:
myclass.h
#pragma once
class MyClass
{
...
void complicatedMethod0();
void complicatedMethod1();
...
};
myclass_impl0.cpp
#include "myclass.h"
void MyClass::complicatedMethod0()
{
...
}
myclass_impl1.cpp
#include "myclass.h"
void MyCLass::complicatedMethod1()
{
...
}
If you are intending to add methods to myClass, you can't do that - the class methods have to be contained in one definition.
You can extend myClass, however, by inheriting from it:
secondFile.h:
#ifndef SECONDFILE_H
#define SECONDFILE_H
#include "myClass.h"
class mySecondClass : public myClass
{
public:
void someNewMethod();
}
#endif
secondFile.cc
#include "secondFile.h"
void mySecondClass::someNewMethod(){
this.classMethod1();
}
#include "secondFile.h"
#include "myClass.h"
//if you want the class methods,
//you need to tell the compiler where to look
void someNewMethod(){
// can't see classMethod1()
}
it seems you've forgotten to include "myClass.h".

Resolving circular dependency in the State Pattern with C++

I'm trying to implement a State Pattern in C++, but have problems with the circular dependency. I have read other related material here - unfortunately it didn't help me. I don't have a lot of experience with C++, so bear with me.
The following code is developed on a Ubuntu 10.10 machine in Eclipse Helios CDT:
ConcreteSystem.h
#ifndef CONCRETESYSTEM_H_
#define CONCRETESYSTEM_H_
class SystemState;
class ConcreteSystem {
public:
ConcreteSystem();
void SelfTestFailed();
void Restart();
private:
friend class SystemState;
SystemState *currentState;
void ChangeState(SystemState *state);
};
#endif /* CONCRETESYSTEM_H_ */
ConcreteSystem.cpp
#include "ConcreteSystem.h"
#include "SystemState.h"
ConcreteSystem::ConcreteSystem() {
currentState = SelfTest::GetInstance();
}
void ConcreteSystem::SelfTestFailed() {
currentState->SelfTestFailed(this);
}
void ConcreteSystem::Restart() {
currentState->Restart(this);
}
void ConcreteSystem::ChangeState(SystemState *state){
currentState = state;
}
SystemState.h
#ifndef SYSTEMSTATE_H_
#define SYSTEMSTATE_H_
class ConcreteSystem;
class SystemState {
public:
virtual void Restart(ConcreteSystem *cs);
virtual void SelfTestFailed(ConcreteSystem *cs);
protected:
virtual void ChangeState(ConcreteSystem *cs, SystemState *state);
};
#endif /* SYSTEMSTATE_H_ */
SystemState.cpp
#include "SystemState.h"
#include "ConcreteSystem.h"
void SystemState::Restart(ConcreteSystem *cs) {
}
void SystemState::SelfTestFailed(ConcreteSystem *cs) {
}
void SystemState::ChangeState(ConcreteSystem *cs, SystemState *state) {
cs->ChangeState(state);
}
SelfTest.h
#ifndef SELFTEST_H_
#define SELFTEST_H_
#include "SystemState.h"
class SelfTest : public SystemState {
public:
SelfTest();
void SelfTestFailed(ConcreteSystem* cs);
static SystemState* GetInstance();
private:
static SystemState* instance;
};
#endif /* SELFTEST_H_ */
SelfTest.cpp
#include "SelfTest.h"
#include "Failure.h"
SystemState* SelfTest::instance = 0;
SelfTest::SelfTest() {
}
void SelfTest::SelfTestFailed(ConcreteSystem *cs) {
ChangeState(cs, Failure::GetInstance());
}
SystemState* SelfTest::GetInstance() {
if (instance == 0) {
instance = new SelfTest();
}
return instance;
}
Failure.h
#ifndef FAILURE_H_
#define FAILURE_H_
#include "SystemState.h"
class SelfTest;
class Failure : public SystemState {
public:
Failure();
void Restart(ConcreteSystem* t);
static SystemState* GetInstance();
private:
static SystemState* instance;
};
#endif /* FAILURE_H_ */
Failure.cpp
#include "Failure.h"
#include "SelfTest.h"
SystemState* Failure::instance = 0;
Failure::Failure() {
}
void Failure::Restart(ConcreteSystem* t) {
ChangeState(t, SelfTest::GetInstance());
}
SystemState* Failure::GetInstance() {
if (instance == 0) {
instance = new Failure();
}
return instance;
}
I have problem with the includes, which gives me some weird compiler errors. Anyone with a good solution to this problem?
From the looks of the code you've posted, you'll have classes being redefined. Looking at your Failure.cpp file, you have:
#include "Failure.h"
#include "SelfTest.h"
Which will include both of those files, and each of those files include the SystemState.h file. Since the SystemState.h file is included more than once, it tries to redefine the SystemState class. At the top of each of your header files, you should do something like this:
// SystemState.h
#ifndef SystemState_h
#define SystemState_h
.. class definition ..
#endif // close the if statement from above.
As an aside on the design, I think it's bad form for the states to know about each other - use your ConcreteSystem as a state controller and then base the state on the return value of the last state operation.
Also, if you're relatively inexperienced with C++, I would recommend looking at this as a great source of learning material (in addition to StackOverflow, of course!).