error C2664 cannot convert parameter 1 from 'std::string (__thiscall ClassName::* )(std::string)' to 'std::string (__cdecl *)(std::string) - visual-c++

I'm making a unmanaged application to handle an event fired in c# here.
FYI:: I want to handle a custom event when my Name property in C# class is changed.
I have gone through the following links:
Explanation about passing pointer to member function as parameter
Something similar to my problem.But couldn't understand the solution
Now,
In NativeApp.cpp,I have a member function which is passed as a function pointer as parameter in a method present in the c++/CLI wrapper
//NativeApp.cpp
std::string Class1::FunctionToBePointed(std::string msg)
{
return msg;
}
void Class1::NativeMethod()
{
UnmanagedWrapperClass* unmanagedWrapperClass=new UnmanagedWrapperClass();
unmanagedWrapperClass->WrapperMethod(&Class1::FunctionToBePointed,"Hello")
}
In Wrapper.h,
//Wrapper.h
class __declspec(dllexport) UnmanagedWrapperClass
{
boost::signals2::signal<void(std::string)>signalEvent;
void WrapperMethod(std::string (*GetCallBack)(std::string),std::string value);
}
When I call the WrapperMethod from NativeApp.cpp,
I subscribe my EventHandlerWrapper to a c# event
connect the function pointer to my boost signal signalEvent.
Set the Name property of the CSharp Class
When the Name Property is set, c# event is fired, EventHandlerWrapper method in Wrapper.cpp is executed.Looks like this::
void EventHandlerWrapper(string value)
{
if(signalEvent.connected())
{
signalEvent(value);
}
For some reasons I can't make my FunctionToBePointed(std::string) method as a non-member function.
P.S:: All ears for any other design approach.

In your real use-case can you simply make FunctionToBePointed a static method?
static std::string Class1::FunctionToBePointed(std::string msg)
{
return msg;
}
If yes your code should work.
The reason is that instance methods are implicitly called with an hidden this pointer, this is the thiscall calling convention, whereas static methods simply use the cdecl convention because they don't work on any instance.
EDIT:
A sample with Boost::bind:
The MyClass C# class:
using System;
using System.ComponentModel;
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate{};
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name != value)
{
name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
The C++/CLI wrapper:
Wrapper.h:
class WrapperPrivateStuff;
class __declspec(dllexport) UnmanagedWrapperClass
{
private: WrapperPrivateStuff* _private;
public: void changeIt(std::string newName);
public: void WrapperMethod(boost::function<std::string(std::string)> GetCallBack);
public: UnmanagedWrapperClass();
};
Wrapper.cpp:
#using "MyClass.dll"
#include <boost/signals2.hpp>
#include <boost/bind.hpp>
#include "Wrapper.h"
#include <msclr\auto_gcroot.h>
#include <msclr\marshal_cppstd.h>
#include <msclr\event.h>
class WrapperPrivateStuff
{
public: boost::signals2::signal<void(std::string)>signalEvent;
public: msclr::auto_gcroot<MyClass^> a;
public: void EventHandlerWrapper(System::Object^, System::ComponentModel::PropertyChangedEventArgs^ args)
{
this->signalEvent(msclr::interop::marshal_as<std::string>(a->Name));
}
public: WrapperPrivateStuff()
{
a = gcnew MyClass();
a->PropertyChanged += MAKE_DELEGATE(System::ComponentModel::PropertyChangedEventHandler, EventHandlerWrapper);
}
BEGIN_DELEGATE_MAP(WrapperPrivateStuff)
EVENT_DELEGATE_ENTRY(EventHandlerWrapper, System::Object^, System::ComponentModel::PropertyChangedEventArgs^)
END_DELEGATE_MAP()
};
void UnmanagedWrapperClass::changeIt(std::string newName)
{
this->_private->a->Name = msclr::interop::marshal_as<System::String^>(newName);
}
UnmanagedWrapperClass::UnmanagedWrapperClass()
{
this->_private = new WrapperPrivateStuff();
}
void UnmanagedWrapperClass::WrapperMethod(boost::function<std::string(std::string)> GetCallBack)
{
_private->signalEvent.connect(GetCallBack);
}
And the native application, test.cpp:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include "Wrapper.h"
class Class1
{
private: std::string name;
public: Class1(std::string name)
: name(name)
{
}
public: std::string FunctionToBePointed(std::string msg)
{
std::cout << "Hey it's " << name << "! Got: " << msg << std::endl;
return msg;
}
};
int main(void)
{
UnmanagedWrapperClass wrapper;
Class1 class1("Ed");
wrapper.WrapperMethod(boost::bind(&Class1::FunctionToBePointed, &class1, _1));
wrapper.changeIt("azerty");
return 0;
}
Result:
>test.exe
Hey it's Ed! Got: azerty
I have a more generic solution but it is really ugly. :(
Let me know if this fix your issue...

Related

cannot instantiate abstract class & no overloaded function could convert all argument types (c2259 & c2665)

I'm working in visual studio and I keep getting these error.
E0322 object of abstract class type "GameState" is not allowed
Error C2259 'GameState': cannot instantiate abstract class
Error C2665 'GameState::GameState': no overloaded function could convert all the argument types
I understand that I need to override the functions from the state class in the gamestate class and I thought that I did that but apparently not. Can anyone see where I am going wrong?
code:
state.h:
class State {
private:
sf::RenderWindow* window;
std::vector<sf::Texture> textures;
public:
State(sf::RenderWindow* window);
virtual ~State(); //this means that all child classes must define these
virtual void endState() = 0;
virtual void update(const float& dt) = 0;
virtual void render(sf::RenderTarget* target = nullptr) = 0; };
state.cpp
#include "State.h"
State::State(sf::RenderWindow* window)
{
this->State::~State(){}
gamestate.h
#include "State.h"
class GameState :
public State
{
private:
public:
GameState(sf::RenderWindow\* window);
virtual \~GameState();
void endState();
void update(const float& dt);
void render(sf::RenderTarget\* target);
};
Gamestate.cpp
#include "GameState.h"
GameState::GameState(sf::RenderWindow\* window)
: State(window){}
GameState::\~GameState(){}
void GameState::endState(){}
void GameState::update(const float& dt){}
void GameState::render(sf::RenderTarget* target = nullptr){}
game.h
#include "GameState.h"
class Game
{
//variables
sf::RenderWindow* window;
sf::Event sfEvent;
sf::Clock dtClock; //frame tracker
float dt; //deltatime
std::stack<State*> states;
//initialization
void initWindow();
void initStates();
public:
Game();
virtual \~Game();
void updateDeltaTime();
void updateSFMLEvents();
void update();
void render();
void run();
};
game.cpp
//I will be leaving out all the unnecessary code to save space
#include "Game.h"
void Game::initStates() {
this->states.push(new GameState(this->window)); //this is where the problem is
}
hovering over GameState says that State::endState()/render()/update() has no override
please help!!!

Call a Form method from a different thread (Invoke)

I've got a WinForm running on my main thread and a while(true) loop running on a separate thread. Each loop of that while(true) creates a new System::String^ and I want to paste that String into a TextBox on my UI.
My file structure includes GUI.h, GUI.cpp, and Other.cpp.
GUI.h contains all the automatically created code for the main (and only) Form. It also has some Get, Set, and ButtonClick methods.
//GUI.h
#pragma once
#include <string>
#include <vector>
#include <cliext\vector>
#include <conio.h>
#include <list>
#include <iostream>
extern void BufferRecieveLoop();
namespace GUI_Example_Receive {
static bool loopFlag = true;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
/// <summary>
/// Summary for GUI
/// </summary>
public ref class GUI : public System::Windows::Forms::Form
{
public:
GUI(void)
{
InitializeComponent();
}
std::vector<std::string> CollectText();
void ContinueNormally(); // Object^);
void DisableAllTextboxes();
void EnableAllTextboxes();
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~GUI()
{
if (components)
{
delete components;
}
}
private:
//Labels
//GroupBoxes
//Buttons
//SaveFile
public:
//TextBoxes
System::Windows::Forms::TextBox^ consoleTextBox;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
//automatically made, lightly edited
}
#pragma endregion
public:
void SetConsoleTextBoxText(System::String^ input)
{
this->consoleTextBox->Text = input;
this->consoleTextBox->Refresh();
}
void ClearConsoleTextBoxText()
{
this->consoleTextBox->Clear();
}
delegate void MyDelegate(System::String ^ str);
void ClearAndSetConsoleTextBoxText(System::String ^ input)
{
/***************************************************
if (InvokeRequired)
{
this->BeginInvoke(gcnew MyDelegate(this, &ClearAndSetConsoleTextBoxText), { input });
}
***************************************************/
ClearConsoleTextBoxText();
SetConsoleTextBoxText(input);
}
System::Void startButton_Click(System::Object^ sender, System::EventArgs^ e)
{
loopFlag = true; //able to loop through ContinueNormally()
ContinueNormally(); //method in GUI.cpp
}
};
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/4da834f0-d8f8-4abb-a655-ef9e99d51eb2/how-to-create-a-global-object-of-a-ref-class-type?forum=vcgeneral
ref struct Globals {
static GUI ^gui; //using Globals::gui everywhere to access the one Form
};
}
Gui.cpp contains code to Run() the form, start a thread, and loop forever.
//GUI.cpp
void BufferRecieveLoop()
{
while (true)
{
size_t bytes_read = multicast.Receive(buffer, Example::MTU_SIZE);
incoming.Process(buffer, bytes_read, endian); //method in Other.cpp
}
}
void GUI::ContinueNormally()
{
System::Threading::Thread ^loopThread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(BufferRecieveLoop));
loopThread->Start();
loopThread->Join();
}
static void Start()
{
Globals::gui = gcnew GUI;
System::Windows::Forms::Application::Run(Globals::gui);
}
int __cdecl main(int argc, char* argv[])
{
System::Windows::Forms::Application::EnableVisualStyles();
System::Windows::Forms::Application::SetCompatibleTextRenderingDefault(false);
Start();
return 0;
}
Other.cpp creates a String^ and calls a method within GUI.h to change the text in a textbox.
//Other.cpp
void Process(const DIS::Pdu& packet)
{
System::String^ sysStr2 = "stuff";
GUI_Example_Receive::Globals::gui->ClearAndSetConsoleTextBoxText(sysStr2);
//GUI_Example_Receive::Globals::gui->BeginInvoke(gcnew MyStringDelegate(GUI_Example_Receive::Globals::gui, &GUI_Example_Receive::GUI::ClearAndSetConsoleTextBoxText), { sysStr2 });
}
I don't know where to properly Invoke my methods. Nor do I know how to Invoke my methods. A lot of what I've found is C# and hasn't worked for me.
Do I invoke from Other.cpp or inside the method being called in GUI.h?
In case anyone else has issues with this in the future and, like me, doesn't find many c++ code examples, I'll post my solution.
In my GUI.h file, I've got a method to SetConsoleTextBoxText(). This is usable only by the thread which owns consoleTextBox. Therefore, any other thread which trys to call that method will need to Invoke() the method (which is to relinquish control back to the owning thread).
//GUI.h
delegate void MyDelegate(System::String ^ text);
void SetConsoleTextBoxText(System::String^ input)
{
if (this->consoleTextBox->InvokeRequired) //is a thread other than the owner trying to access?
{
MyDelegate^ myD = gcnew MyDelegate(this, &GUI::SetConsoleTextBoxText);
//GUI is the ref class. Replace with wherever your function is located.
this->Invoke(myD, gcnew array<Object^> { input }); //Invoke the method recursively
}
else
{
//Normal function of this method. This will be hit after a recursive call or from the owning thread
this->consoleTextBox->Text = input;
this->consoleTextBox->Refresh();
}
}

How to connect signal from qconcurrent thread to gui thread sharing one string

I'm trying to update gui label with an other thread information (QString).
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public Q_SLOTS:
void sl_appendInfo(QString p_text);
private:
Ui::MainWindow *ui;
QFuture<void> m_thread;
QFuture<void> m_engine;
engine* m_object;
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
m_object = new engine();
qRegisterMetaType<QString>();
bool success = connect(this->m_object, SIGNAL(engine::sig_appendInfo(QString)), this, SLOT(sl_appendInfo(QString)), Qt::QueuedConnection);
if(!success)
{
qDebug("success failed");
}
m_engine = QtConcurrent::run(this->m_object, &engine::eventLoop);
}
//slot declaration in mainwindow.cpp
void MainWindow::sl_appendInfo(QString p_text)
{
ui->label->setText(p_text.toLocal8Bit().constData());
}
class engine : public QObject
{
Q_OBJECT
public:
engine();
~engine();
void eventLoop();
Q_SIGNALS:
void sig_exitengine(void);
void sig_appendInfo(QString p_text);
};
void engine::eventLoop()
{
int state = false;
while(true)
{
state = getNextEvent(m_event);
if (state == true)
{
sig_appendInfo("information for gui: we handled a new event !");
state=false;
}
QThread::msleep(1000);
}
}
Now I use this link : My signal / slot connection does not work to build my own code but it didn't work, the connection failed... Can I have some help please?
Thank you
Your connect syntax is wrong. You shouldn't include the class name in the SIGNAL macro. If you use the old syntax, it should be:
bool success = connect(m_object, SIGNAL(sig_appendInfo(QString)), this, SLOT(sl_appendInfo(QString)), Qt::QueuedConnection);
Or if you want to use the new syntax:
bool success = connect(m_object, &engine::sig_appendInfo, this, &MainWindow::sl_appendInfo, Qt::QueuedConnection);

how to write connect statement of lineEdit in different class

How can I make signal and slot of lineEdit which is declare in another class ?
LineEdit is declared in Peakdetechtion class and i want to make signal and slot in peaksettingform so how can I do this?
the QLineEdit either has to be accessible from the outside (public or get) or you have to forward the signal you are interested in.
accessible version (incomplete and very dirty)
class Peakdetechtion { // horrible name
public:
QLineEdit* getLineEdit() { return m_lineEdit; } // don't do it
private:
QLineEdit* m_lineEdit;
};
class Peaksettingform : public QObject { //horrible name
Q_OBJECT
public:
Peaksettingform(Peakdetechtion *p, QObject *parent = 0)
: QObject(parent) {
// you can do this from outside and replace 'this' with a pointer to a Peaksettingform object
connect(p->getLineEdit(), SIGNAL(textChanged(const QString &)), this, SLOT(handleText(const QString &)));
}
public slots:
void handleText(const QString &);
};
signal forwarding
class Peakdetechtion : public QObject { // horrible name
Q_OBJECT
public:
Peakdetechtion() {
m_lineEdit = new QLineEdit(); // should have a parent but i am lazy
connect(m_lineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(leTextChanged(const QString&)));
}
signals:
void leTextChanged(const QString &);
private:
QLineEdit* m_lineEdit;
};
class Peaksettingform : public QObject { //horrible name
Q_OBJECT
public:
Peaksettingform(Peakdetechtion *p, QObject *parent = 0)
: QObject(parent) {
// you can do this from outside and replace 'this' with a pointer to a Peaksettingform object
connect(p, SIGNAL(leTextChanged(const QString &)), this, SLOT(handleText(const QString &)));
}
public slots:
void handleText(const QString &);
};

why signal/slot not working with multiple threads?

class A : public QObject{
Q_OBJECT
signals:
void a_sig();
public:
A(){ }
public slots:
void begin(){
QObject::connect(&_timer, SIGNAL(timeout()), this, SIGNAL(a_sig()));
_timer.start(1000);
}
private:
QTimer _timer;
};
class B : public QObject{
Q_OBJECT
public:
B(){ value = 0; }
public slots:
void b_slot(){
++value;
QFile file("out.txt");
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
out << value << "\n";
file.close();
}
private:
int value;
};
int main(int argc, char **argv){
QCoreApplication app(argc, argv);
A a;
B b;
QThread aThread;
QThread bThread;
QObject::connect(&aThread, SIGNAL(started()), &a, SLOT(begin()));
QObject::connect(&a, SIGNAL(a_sig()), &b, SLOT(b_slot()));
a.moveToThread(&aThread);
b.moveToThread(&bThread);
aThread.start();
bThread.start();
return app.exec();
}
I'm trying to understand why b_slot() isn't getting called. Can anyone explain what's happening, and why b_slot() isn't getting called?
The problem is the ownership of the _timer member of the A class.
Since you're not explicitly initializing it, it is initialized without a parent object. So the a.moveToThread(&aThread) isn't moving the timer to aThread, and things get confused after that.
Change A's constructor to:
A() : _timer(this) {}
and your b_slot() will get called.
The problem is that while object a is moved to aThread, _timer object still belongs to the original main thread. Try initializing _timer inside begin method like that:
void begin() {
_timer = new QTimer;
QObject::connect(_timer, SIGNAL(timeout()), this, SIGNAL(a_sig()));
_timer->start(1000);
}
private:
QTimer *_timer;

Resources