Skip Unused Virtual Function - c++

I have main executable and two functions that are dereferenced to DLL.
class CPluginInterface
{
public:
virtual void A(void) = 0;
virtual void B(void) = 0;
};
I created DLL like this
//Main.h
#include "Function.h"
class CForward : public CPluginInterface
{
public:
//void A(void);
void B(void);
};
//Main.cpp
#include "Main.h"
/*void CForward::A(void)
{
//A Function is commented because it is not used
}*/
void CForward::B(void)
{
//Do something here
}
extern "C"
{
// Plugin factory function
//void __declspec(dllexport) __cdecl A(void) { }
void __declspec(dllexport) __cdecl B(void) { }
}
However the program is crashed because A(void) doesn't exist when the main executable dereferencing it. How to skip A(void)?
If I create the DLL like this, it works fine.
//Main.h
#include "Function.h"
class CForward : public CPluginInterface
{
public:
void A(void);
void B(void);
};
//Main.cpp
#include "Main.h"
void CForward::A(void)
{
//Do something here
}
void CForward::B(void)
{
//Do something here
}
extern "C"
{
// Plugin factory function
void __declspec(dllexport) __cdecl A(void) { }
void __declspec(dllexport) __cdecl B(void) { }
}
NB: I create Plugin Interface.

The =0 suffix on your interface's virtual functions indicate that they are pure virtual and that you are required to override them when inheriting from the base class. In your first example CForward is an abstract class because you have not overridden A, thus you cannot create an instance of CForward.
https://stackoverflow.com/a/2652223

Related

Why is this simple application using polymorphism and virtual functions not printing correctly?

I have an employee class and a server class and am trying to figure out why the printing of the server data in the main function isn't taking the server print() function and using it.
Employee.h:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
using namespace std;
class Employee
{
public:
virtual void job();
void print();
};
#endif
Employee.cpp:
#include "Employee.h"
void Employee::job() {
cout << "Employee status yet to be determined.\n" << endl;
}
void Employee::print() {
cout << "New employee\n" << endl;
}
Server.h:
#ifndef SERVER_H
#define SERVER_H
#include "Employee.h";
#include <iostream>
using namespace std;
class Server : public Employee
{
public:
void job();
void print();
};
#endif
Server.cpp:
#include "Server.h"
void Server::job() {
cout << "Serve tables\n";
print();
}
void Server::print() {
"I am a server!\n";
}
main:
#include "Employee.h"
#include "Server.h"
#include <iostream>
void output(Employee* employee) {
employee->job();
}
int main()
{
Employee* a = new Server;
Employee* b = new Employee;
output(a);
a->print();
output(b);
b->print();
return 0;
}
Just trying to wrap my head around using virtual functions and polymorphism.
The print function is not declared virtual, so there is no polymorphism in that call. What will happen is that each of your calls will call Employee::print because both a and b are of type Employee*.
Change your class definition to this:
class Employee
{
public:
virtual void job();
virtual void print();
};
You should also define a virtual destructor if you plan to delete objects through the base pointer.
class Employee
{
public:
virtual ~Employee() {}
virtual void job();
virtual void print();
};

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

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
$

i need help implementing my event manager and engine system (SDL && C++)

i need help with my attempt at writing a reusable game engine. it's not the best engine, but i definitely think it will be reusable once i am done. i am not asking for code or to be spoonfed, but i am asking for some advice :-).
my current layout:
i have an engine class, a game class, and an event manager class. the engine class extends the event manager class, and the game class extends the engine class. here is my current code for these 3 classes (ignore the Graphics class--it is just a reusable class i use to avoid rewriting fullscreen, initialize, and resize screen functions).
ENGINE.HPP
#ifndef _ENGINE_HPP
#define _ENGINE_HPP
#pragma once
#include <SDL/SDL.h>
#include "event_manager.hpp"
enum
{
ENGINE_SUCCESS = 0,
ENGINE_INITIALIZATION_ERROR
};
class Engine : public EventManager
{
public:
Engine();
virtual ~Engine();
int exec();
void handle_event(SDL_Event *);
void update_engine();
virtual bool init();
virtual void render();
virtual void update();
virtual void clean();
bool running_;
};
ENGINE.CPP
#include "./engine.hpp"
Engine::Engine()
{
running_ = false;
}
Engine::~Engine()
{
}
int Engine::exec()
{
if (!init())
{
clean();
return ENGINE_INITIALIZATION_ERROR;
}
SDL_Event event;
while (running_)
{
while (SDL_PollEvent(&event))
handle_event(&event);
update();
render();
}
clean();
return ENGINE_SUCCESS;
}
void update_engine()
{
}
void handle_event(SDL_Event *event)
{
EventManager::handle_event(event);
}
bool init() {return true;}
void render() {}
void update() {}
void clean() {}
EVENT_MANAGER.HPP
#ifndef _EVENT_MANAGER_HPP
#define _EVENT_MANAGER_HPP
#pragma once
#include <SDL/SDL.h>
class EventManager
{
public:
EventManager();
virtual ~EventManager();
virtual void handle_event(SDL_Event *);
// events here
virtual void event_exit();
};
#endif
EVENT_MANAGER.CPP
#include "./event_manager.hpp"
EventManager::EventManager()
{
}
EventManager::~EventManager()
{
}
void EventManager::handle_event(SDL_Event *event)
{
switch (event->type)
{
case SDL_QUIT:
event_exit();
break;
}
}
void on_exit() {}
GAME.HPP
#ifndef _GAME_HPP
#define _GAME_HPP
#include "./engine.hpp"
#include "./entity.hpp"
#include "./graphics.hpp"
class Game : public Engine
{
public:
Game();
bool init();
void render();
void update();
void clean();
private:
Graphics g;
};
#endif
GAME.CPP
#include "./game.hpp"
int main(int argc, char **argv)
{
Engine engine;
return engine.exec();
}
Game::Game() {}
bool Game::init()
{
if (!g.init(800, 600, 32, g.screen_flags()))
{
return false;
}
SDL_WM_SetCaption("Title", NULL);
return true;
}
void Game::update()
{
Engine::update_engine();
}
void Game::clean()
{
SDL_FreeSurface(g.screen_);
SDL_Quit();
}
void Game::render()
{
SDL_Flip(g.screen_);
}
i am getting this error:
engine.cpp: In function ‘void handle_event(SDL_Event*)’:
engine.cpp:40: error: cannot call member function ‘virtual void
EventManager::handle_event(SDL_Event*)’ without object
why is this happening? shouldn't i be able to do EventManager:: if the I did
class Engine : public EventManager
???
that is the only error i get, i am sure it is something simple. now i need a little bit of advice.
instead of handling events like
void Engine::event_exit()
in the engine, i'd rather do it in the game class.
class Game : public Engine
void Game::event_exit()
if that doesn't make sense, notice how i made Engine extend EventManager, and my Game class extends Engine
class Engine : public EventManager
class Game : public Engine
would it work if i called the snippet above these ^ two snippets? i can't test it because i get that error.
Happens to the best of us, but I think it's just a matter of forgetting to specify namespaces. When you implement the functions in engine.cpp, you forgot to prepend Engine::, so the code should be:
void Engine::update_engine()
{
}
void Engine::handle_event(SDL_Event *event)
{
EventManager::handle_event(event);
}
It's a classic case of C++ error messages not really telling you the root of the error.
A short explanation, just in case:
The compiler tried to compile the function void handle_event(SDL_Event *event), and saw a call to a method EventManager::handle_event(event);. Since the compiler thought the function was not part of the Engine class, it would expect you to call the method of a particular instance of the EventManager class, i.e.
someEventManager->handle_event(event);
As soon as you specify that the implementation you wrote is that of a method, belonging to the class Engine, the compiler essentially deduces:
void Engine::handle_event(SDL_Event *event)
{
this->EventManager::handle_event(event);
}
And therefore is happy.

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!).