c++/CLI wrapper for pointer member - c++ singleton instance - c++

I am completing my c++/CLI wrapper for the following native c++ class:
#ifndef __TV3DENGINE_H__
#define __TV3DENGINE_H__
#pragma once
#include "TV3DMoteur.h"
#include "Input.h"
#include "Area.h"
#include <vcclr.h>
class Engine3D
{
public:
CLTV3DMoteur* clTV3D;
CLInput* clInput;
CLArea* clArea;
CLGlobalVar * clGlobalVar;
Engine3D();
~Engine3D();
void Setup(HWND TVScreenHWND, string PathString);
void UpdateLoop();
void Cleanup();
bool AppStillIdle();
CLTV3DMoteur* GetTV3D();
CLInput* GetInput();
CLArea* GetArea();
CLGlobalVar * GetGlobalVar();
};
#endif
The actual constructor for Engine3D is :
Engine3D::Engine3D()
{
clTV3D = CLTV3DMoteur::getInstance();
clInput = CLInput::getInstance();
clArea = CLArea::getInstance();
clGlobalVar = CLGlobalVar::getInstance();
}
Here is the actual wrapper:
#ifndef __WRAPPER_H__
#define __WRAPPER_H__
#pragma once
#include "TV3DEngine.h"
#include <msclr\marshal_cppstd.h>
public ref class Engine3DWrapper {
Engine3D* m_nativeClass;
public:
Engine3DWrapper() { m_nativeClass = new Engine3D(); }
~Engine3DWrapper() { delete m_nativeClass; }
void Setup(System::IntPtr tvscreen, System::String^ AppPath) {
System::String^ managedPath = AppPath;
m_nativeClass->Setup((HWND)tvscreen.ToInt32(), msclr::interop::marshal_as<std::string>(managedPath));
}
void UpdateLoop() {
m_nativeClass->UpdateLoop();
}
void Cleanup() {
m_nativeClass->Cleanup();
}
bool AppStillIdle() {
return(m_nativeClass->AppStillIdle());
}
protected:
!Engine3DWrapper() { delete m_nativeClass; }
};
#endif
My question is how can I modifiy my Wrapper so I can have access to, exemple, Engine3DWrapper->clGlobalVar->BLABLABLA() where BLABLABLA would be all the different methods defined in the CLGlobalVar c++singleton?
I tried via this technique :
property String ^Name
{
String ^get()
{
return gcnew String(_stu->getName());
}
}
but that seems not possible since I need not to return a defined type.
thanks for your help.

Problem solved.
Here is the corrected Wrapper following Rufflewind suggestion:
#ifndef __WRAPPER_H__
#define __WRAPPER_H__
#pragma once
#include "TV3DEngine.h"
#include <msclr\marshal_cppstd.h>
public ref class Engine3DWrapper {
Engine3D* m_nativeClass;
public:
Engine3DWrapper(System::IntPtr tvscreen, System::String^ AppPath)
{
m_nativeClass = new Engine3D((HWND)tvscreen.ToInt32(), msclr::interop::marshal_as<std::string>(AppPath));
m_TV3D = m_nativeClass->GetTV3D();
m_Input = m_nativeClass->GetInput();
m_Area = m_nativeClass->GetArea();
m_GlobalVar = m_nativeClass->GetGlobalVar();
}
~Engine3DWrapper() {
delete m_nativeClass;
}
void UpdateLoop() {
m_nativeClass->UpdateLoop();
}
void Cleanup() {
m_nativeClass->Cleanup();
}
bool AppStillIdle() {
return(m_nativeClass->AppStillIdle());
}
CLTV3DMoteur* m_TV3D;
CLInput* m_Input;
CLArea* m_Area;
CLGlobalVar* m_GlobalVar;
protected:
!Engine3DWrapper() { delete m_nativeClass; }
};
#endif
using simple Get Method in the native class:
CLGlobalVar *Engine3D::GetGlobalVar()
{
clGlobalVar = CLGlobalVar::getInstance();
return(clGlobalVar);
}
Thanks for you help!

Related

Undefined reference to a virtual function [duplicate]

This question already has answers here:
Virtual/pure virtual explained
(12 answers)
Closed 1 year ago.
booking.h
#ifndef _BOOKING_H_
#define _BOOKING_H_
#include <string>
class Event {
private:
std::string event_option;
public:
Event(std::string event_option);
virtual ~Event();
void list_specific_event_details();
virtual void list_details();
};
class Films : public Event {
private:
std::string movie_option;
public:
Films(std::string movie_option);
void list_details();
};
class Live_Music : public Event {
private:
std::string live_music_option;
public:
Live_Music(std::string live_music_option);
void list_details();
};
class Standup_Comedy : public Event {
private:
std::string comedy_option;
public:
Standup_Comedy(std::string comedy_option);
void list_details();
};
#endif
booking.cpp
#include "booking.h"
#include <string>
Event::Event(std::string event_option)
{
this->event_option = event_option;
}
Event::~Event()
{
}
void Event::list_specific_event_details()
{
return list_details();
}
Films::Films(std::string movie_option) : Event("Films")
{
this->movie_option = movie_option;
}
void Films::list_details()
{
//some code ...
return;
}
Live_Music::Live_Music(std::string live_music_option) : Event("Live_Music")
{
this->live_music_option = live_music_option;
}
void Live_Music::list_details()
{
//some code...
return;
}
Standup_Comedy::Standup_Comedy(std::string comedy_option) : Event("Standup_Comedy")
{
this->comedy_option = comedy_option;
}
void Standup_Comedy::list_details()
{
//some code...
return;
}
main.cpp
#include "booking.h"
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector <Event *> choice;
choice.push_back(new Films("f"));
choice.push_back(new Live_Music("l"));
choice.push_back(new Standup_Comedy("s"));
for(unsigned i = 0; i < choice.size(); i++)
{
choice[i] -> list_specific_event_details();
}
for (Event * e: choice) delete e;
choice.clear();
}
I have written the above block of codes and got an error when I tried to compile it.
The error is as follows:
usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccuWXzBM.o:booking.cpp:(.rdata$_ZTV5Event[_ZTV5Event]+0x20): undefined reference to `Event::list_details()'
collect2: error: ld returned 1 exit status
Why am I getting this error, and how can I solve it?
You have not defined your method Event::list_details(). If you only want to declare the function but do not want to implement it, that is what is called an Interface, you can make it a pure virtual method with:
class Event
{
...
virtual void list_details() = 0;
...
};

c++ [gamestate] function does not take in correct argument error

Hello i have problem of compile my code
i follow http://gamedevgeek.com/tutorials/managing-game-states-in-c/ tutorial
but it fail to compile and i don't know why.
the error msg from visual studio
here is my code
the CGameEngine modify code
#include <vector>
#include "GameState.h"
#include "GameEngine.h"
class GameState;
class GameStateManager
{
public:
GameStateManager(GameEngine* engine, MSG * msg);
~GameStateManager();
void Cleanup();
void ChangeState(GameState* state);
void Update();
bool Running() { return m_running; }
void Quit();
private:
std::vector<GameState *> states;
bool m_running;
GameEngine * m_engine;
MSG *m_msg;
};
#include "GameStateManager.h"
GameStateManager::GameStateManager(GameEngine* engine, MSG * msg)
:m_engine{ engine }, m_msg{ msg }, m_running{ true }
{
}
GameStateManager::~GameStateManager()
{
}
void GameStateManager::Cleanup()
{
while (!states.empty()) {
states.back()->Exit();
states.pop_back();
}
}
void GameStateManager::Quit()
{
m_running = false;
m_msg->message = WM_QUIT;
}
void GameStateManager::ChangeState(GameState* state)
{
if (!states.empty()) {
states.back()->Exit();
states.pop_back();
}
states.push_back(state);
states.back()->Enter(m_engine, m_msg);
}
void GameStateManager::Update()
{
states.back()->Update(this);
}
the CGameState modify code
#include "GameStateManager.h"
class GameState
{
public:
GameState() {}
virtual ~GameState() {}
virtual void Enter(GameEngine * , MSG * ) = 0;
virtual void Update(GameStateManager* game) =0;
virtual void Exit() = 0;
};
one of the state class
#include "MainMenu.h"
class Logo :public GameState
{
public:
Logo();
~Logo();
static Logo* Instance()
{
return &m_Logo;
}
void Enter(GameEngine * engine, MSG * msg);
void Update(GameStateManager* game);
void Exit();
private:
static Logo m_Logo;
};
#include "Logo.h"
Logo::Logo()
{
}
Logo::~Logo()
{
}
void Logo::Enter(GameEngine * engine, MSG * msg)
{
m_GameEngine_Info = engine;
m_msg = msg;
}
void Logo::Update(GameStateManager* game)
{
}
void Logo::Exit()
{
}
i get no compile error when editing the code, but when i try compile it get this error.
You have circular includes. Use include guards and replace
#include "GameStateManager.h"
with
class GameStateManager;
in GameState.h. Move this include into GameState.cpp.
Do similar with #include "GameEngine.h" and #include "GameState.h" in GameStateManager.h and GameStateManager.cpp.

C++ DLL Class is crashing whilst creating a private member

I have a Problem and i am completely stuck.
First i show you the code:
Config for Building(NarviConfig.h):
#pragma once;
namespace Narvi
{
#ifdef NARVI_EXPORTS
#define _NarviExport __declspec(dllexport)
#else
#define _NarviExport __declspec(dllimport)
#endif
}
//-------------------------------------------
I have 2 Classes in a DLL like this:
The first One(NarviRoot.h):
Header:
#pragma once
#include "NarviConfig.h"
#include "NarviWindow.h"
namespace Narvi
{
class _NarviExport NarviRoot
{
public:
NarviRoot();
~NarviRoot();
public:
bool Init(void);
private:
NarviWindow* mWindow;
};
}
Source:-----------------------------
#include "NarviRoot.h"
namespace Narvi
{
NarviRoot::NarviRoot()
{
}
NarviRoot::~NarviRoot()
{
delete mWindow;
}
bool NarviRoot::Init(void)
{
mWindow = new NarviWindow();
return true;
}
}
//-----------------------------------------------------------------
The window(NarviWindow.h):
Header
#pragma once
#include "NarviConfig.h"
namespace Narvi
{
class _NarviExport NarviWindow
{
public:
NarviWindow();
~NarviWindow();
};
}
Source:--------------------------------
#include "NarviWindow.h"
namespace Narvi
{
NarviWindow::NarviWindow()
{
}
NarviWindow::~NarviWindow()
{
}
}
And this is my Testapp
mRoot = new Narvi::NarviRoot();
mRoot->Init();
It seems to work, but the program crashes when calling
mWindow = new NarviWindow();
Inside of
bool NarviRoot::Init(void)
{
mWindow = new NarviWindow();
return true;
}
and i can't figure out why.
If i write:
NarviWindow* mWindow = new NarviWindow();
it doesn't crash.
The constructor of NarviWindow is doing nothing, but it seems he can't access the private member mWindow.
I am pretty sure it is a simple error i made, but i can't see the wood for the trees.
Thank you! :)

Simple Polymorphism cast not working

I have a class SourceComponent, and its derived class, PeriodicSourceComponent.
Implementations are:
class SourceComponent : public Component
{
protected:
friend class UserInterface;
friend void readInput();
public:
virtual int returnType();
virtual int propagateLogic();
virtual void sourcePropagation(float);
virtual void accept(VisitorSources&);
SourceComponent();
};
and
#include "source_component.h"
class PeriodicSourceComponent : public SourceComponent
{
private:
int frequency;
friend void main();
friend void readInput();
friend class UserInterface;
public:
void sourcePropagation(float);
int returnType();
PeriodicSourceComponent();
};
When I try in some different class/method to do:
SourceComponent* s = new PeriodicSourceComponent;
it won't let me, sayin "a value of type periodicblabla cant be assigned to value of type sourceblabla". Why?
Edit:
Ok, in my main it looks lke this:
#include "source_component.h"
#include "periodic_source_component.h"
void main()
{
SourceComponent* s = new PeriodicSourceComponent;
}
And implementations of both classes:
source.cpp:
#include "source_component.h"
SourceComponent::SourceComponent()
{
outputState = -1;
}
int SourceComponent::propagateLogic()
{
return 1;
}
int SourceComponent::returnType()
{
return 5;
}
and periodic.cpp
#include "periodic_source_component.h"
PeriodicSourceComponent::PeriodicSourceComponent()
{
outputState = 0;
}
int PeriodicSourceComponent::returnType()
{
return 3;
}
void PeriodicSourceComponent::sourcePropagation(float time)
{
float t = time, period;
period = 1000000/frequency;
if(t > period)
{
while(t >= period)
t -= period;
}
if(t <= (period/2))
outputState = 0;
else
outputState = 1;
}
and its not working... (outputState is a member of class Component, base class of SourceComponent)
and the error message: A value of type "PeriodicSourceComponent*" cannot be assigned to a value of type "SourceComponent*".
Important Edit
When I try to compile, the actual compiler error is at PeriodicSourceComponent declaration, it says: "Base class undefined".
and also, I have two other derived classes from SourceComponent, but I don't see how they could interfere with this one..
EDIT 4
Ok so I figured out what causes the error, you were right of course, it something else I didn't post. I have class VisitorSources, heres definition:
#ifndef __VISITOR_SOURCES_H__
#define __VISITOR_SOURCES_H__
#include "component.h"
#include "impulse_source_component.h"
#include "arbitrary_source_component.h"
#include "periodic_source_component.h"
class VisitorSources
{
protected:
VisitorSources();
public:
virtual void visitImpulseSource(ImpulseSourceComponent*);
virtual void visitArbitrarySource(ArbitrarySourceComponent*);
virtual void visitPeriodicSource(PeriodicSourceComponent*);
void visitSource(int, float);
};
#endif
And its implementation is not yet written:
#include "visitor_sources.h"
void visitSource(int type, float time)
{
}
void VisitorSources::visitArbitrarySource(ArbitrarySourceComponent* a)
{
}
When I comment out the entire Visitor class and implementation, the above-mentioned errors are gone for some reason. I have no idea why...
The only error that remains is that when I try to use s->frequency, it says that frequency is not a member of SourceComponent, which is true, but it is a member of PeriodicSourceComponent, which is why I used the cast in the first place..
Finally, here's the Component class, the main class for all almost all other classes in the project :P
#ifndef __COMPONENT_H__
#define __COMPONENT_H__
#include <iostream>
#include <vector>
class Component
{
friend class UserInterface;
friend void readAndSimulate();
protected:
Component();
int numOfInputs;
int numOfOutputs;
std::vector<Component*> inputs;
std::vector<Component*> outputs;
friend void main();
float lengthOfSimulation;
int typeIfSource;
public:
int outputState;
virtual int propagateLogic() = 0;
virtual int returnType();
int beginPropagation();
virtual void sourcePropagation(float);
~Component();
};
#endif
And implementation:
#include "component.h"
#include <conio.h>
Component::Component()
{
}
int Component::beginPropagation()
{
std::vector<Component*>::const_iterator iter = outputs.begin();
for(;iter<outputs.end();++iter)
{
if ((*iter)->outputState == -2)
{
(*iter)->outputState = outputState;
return (*iter)->outputState;
}
}
std::vector<Component*>::const_iterator it = outputs.begin();
int finishedCycle, x;
while(1)
{
finishedCycle = 1;
for(; it < outputs.end(); ++it)
{
x = (*it)->propagateLogic();
if(!x)
finishedCycle = 0;
}
if(finishedCycle) break;
it = outputs.begin();
}
it = outputs.begin();
for(;it<outputs.end();++it)
(*it)->beginPropagation();
}
int Component::returnType()
{
return 0;
}
void Component::sourcePropagation(float)
{
}
Component::~Component()
{
std::vector<Component*>::const_iterator it = inputs.begin();
for(; it < inputs.end(); ++it)
{
if((*it) != NULL)
{
delete *it;
Component* p = *it;
p = NULL;
}
}
it = outputs.begin();
for(; it < inputs.end(); ++it)
{
if((*it) != NULL)
{
delete *it;
Component* p = *it;
p = NULL;
}
}
}
Are you using include guards in all your header files?
Not having them can cause the kinds of problems you're seeing.
Three guesses:
You copied and pasted code between header files and forgot to change the #include guards.
You use precompiled headers and included something before the #include "stdafx.h".
If you use precompiled headers, try deleting the .pch file.

Function refuses to work in Boost test function

I cant understand why in the class constructor I can call this function but when called in the test function, it errors out with
E:\Projects\NasuTek-Plugin-Engine\tests\CheckAddonEngine.cpp:64: error: conversion from 'std::auto_ptr<FakeSettableFeaturePlugin>' to 'std::auto_ptr<FakeFeature>' is ambiguous
C++ File
#include <ObjectEngine.h>
#include <memory>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
class FakeFeature : public PObject {
public:
inline virtual const char *returnSomthingCool() { return "Somthing Cool"; }
};
class FakeFeaturePlugin : public FakeFeature {
public:
inline const char *returnSomthingCool() { return "Somthing Cool From a Plugin Object"; }
inline std::string getName() { return "Fake Feature Plugin Object"; }
};
class FakeSettableFeaturePlugin : public FakeFeature {
public:
inline const char *returnSomthingCool() { return _value; }
inline char *setSomthingCool(const char *value) { _value = const_cast<char *>(value); }
inline std::string getName() { return "Fake Feature Settable Plugin Object"; }
private:
char *_value;
};
typedef ObjectEngine<FakeFeature> FakeFeatureEngine;
class FakeApplication {
public:
inline FakeApplication(){
_engine = new FakeFeatureEngine();
std::auto_ptr<FakeFeature> pluginToAdd (new FakeFeaturePlugin);
_engine->addObject(pluginToAdd); // Essencially what im doing where it errors, both classes inherit FakeFeature
}
inline ~FakeApplication() {
delete _engine;
}
FakeFeatureEngine *_engine;
};
BOOST_AUTO_TEST_CASE(getengineobject) {
FakeApplication *application = new FakeApplication();
FakeFeature &feature = application->_engine->getObject("Fake Feature Plugin Object");
BOOST_CHECK_EQUAL(feature.getName(), "Fake Feature Plugin Object");
BOOST_CHECK_EQUAL(feature.returnSomthingCool(), "Somthing Cool From a Plugin Object");
delete application;
}
BOOST_AUTO_TEST_CASE(addobjecttoengine) {
FakeApplication *application = new FakeApplication();
std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);
plugin.get()->setSomthingCool("Bro I Set this to this value ^_^");
application->_engine->addObject(plugin); // This is the line that fails
FakeFeature &feature = application->_engine->getObject("Fake Feature Settable Plugin Object");
BOOST_CHECK_EQUAL(feature.getName(), "Fake Feature Settable Plugin Object");
BOOST_CHECK_EQUAL(feature.returnSomthingCool(), "Bro I Set this to this value ^_^");
delete application;
}
.h File
/**
#file ObjectEngine.h
#brief Header File with Plugin Engine Templates to make adding a plugin interface to your app easier
*/
#ifndef NASUTEKPLUGINENGINE_H
#define NASUTEKPLUGINENGINE_H
#include <boost/ptr_container/ptr_list.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <stdio.h>
#include <string>
class PObject {
public:
inline PObject() {}
virtual std::string getName() = 0;
};
template<typename TObjectType>
class ObjectEngine {
public:
ObjectEngine() {}
~ObjectEngine() {
_objects.clear();
}
void addObject(std::auto_ptr<TObjectType> obj) {
if(boost::is_base_of<PObject, TObjectType>::value) {
_objects.push_back(obj.release());
}
}
TObjectType &getObject(std::string objectName) {
typename boost::ptr_list<TObjectType>::iterator i;
for(i = _objects.begin(); i != _objects.end(); i++) {
if((*i).getName() == objectName) {
return *i;
}
}
throw "Object does not exist.";
}
bool objectExist(std::string objectName) {
typename boost::ptr_list<TObjectType *>::iterator i;
for(i = _objects.begin(); i != _objects.end(); i++) {
if((*i).getName() == objectName) {
return true;
}
}
return false;
}
private:
boost::ptr_list<TObjectType> _objects;
};
#endif // NASUTEKPLUGINENGINE_H
What I'm trying to do is create a plugin engine for my projects while making it reusable, and the C++ file is making sure its working right.
This doesn't look like a problem with Boost Test, per se, but it looks like the line in addobjecttoengine...
std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);
...should be...
std::auto_ptr<FakeFeature> plugin(new FakeSettableFeaturePlugin);
...the same as in FakeApplication's constructor. Then there's no conversion to perform at CheckAddonEngine.cpp:64.