I am trying to use QTConcurrent class to launch some tasks asynchronously but I am getting some errors:
This is my code:
class A {
public:
void method1();
};
class B {
std::unique_ptr<A> ptr;
public:
void method2() {
QtConcurrent::run(&this->ptr, &A::method1);
}
}
I get compilation error.
Could someone tell me what the correct syntax is?
Thanks in advance and regards
I finally was able to find working version:
class A {
public:
void method1();
};
class B {
std::unique_ptr<A> ptr;
public:
void method2() {
QtConcurrent::run(this->ptr.get(), &A::method1);
}
}
Related
Here I have some interesting code. Does anyone know how to get subtype from myClass without auxiliary parameter in template?
Note: Maybe myClass template too.
class myClass
{
public:
struct tPacaje
{
int data;
};
};
template <class T>
class executorClass
{
public:
void todo(T::tPacaje ob)
{
...
}
};
You are trying to use a type so need to tell the compiler you are using a typename:
template <class T>
class executorClass
{
public:
void todo(typename T::tPacaje ob)
{
//^-------
//...
}
};
I've got following classes:
public ref class Form1 : public System::Windows::Forms::Form
{
//[...]
protected:
System::Void label1_Click(System::Object^ sender, System::EventArgs^ e);
};
public ref class Functions : public Form1
{
protected:
void Example() {}
};
public ref class Handlers : public Functions
{
private:
System::Void label1_Click(System::Object^ sender, System::EventArgs^ e)
{
Example();
}
};
As you can see I want to extern my method into additional class.
The error is:
1>Milionerzy.obj : error LNK2020: unresolved token (06000004) Milionerzy.Form1::label1_Click
What is wrong?
You should probably remove label1_Click from Form1. There is no point in handling label1 click event at all, since you are thinking about making it pure virtual. Just handle it when you are able to.
If you want polymorphism in the handler declare another pure virtual function like this:
public ref class Form1 abstract: public System::Windows::Forms::Form
{
//[...]
protected:
virtual void OnLabel1Click()=0;
};
public ref class Functions : public Form1
{
protected:
void Example()
{
}
virtual void OnLabel1Click() override
{
Example();
}
};
public ref class Handlers : public Functions
{
private:
System::Void label1_Click(System::Object^ sender, System::EventArgs^ e)
{
OnLabel1Click();
}
};
I know that namespace are preferred in C++ if you want to define static functions, but what to do when you have functions which access other functions but the other functions should be private and not be accessible by others. How to declare/define it ? Wouldn't it be much simple to use classes ? Or do I have to still use namespaces ?
Like this:
class Test1
{
public:
static void Start()
{
if(1) // Some checks...
{
ProcessStart();
}
}
private:
static void ProcessStart()
{
if(!Initialized)
{
//Initialize
}
// Do other stuff
}
static bool Initialized;
};
bool Test1::Initialized = false;
You don't need to expose private details in the public API:
foo.hpp:
#ifndef H_FOO
#define H_FOO
namespace Foo
{
void Start();
}
#endif
foo.cpp:
#include "foo.hpp"
namespace Foo
{
namespace
{
void ProcessStart()
{
// ...
}
}
void Start()
{
ProcessStart();
}
}
Only the header file ends up in your public /include directory, so nobody will ever know that there are private implementation functions (except perhaps your debugger).
This is a class library clr/c++ project.
Class A is unmanaged c++, class B managed c++.
I would like to create an object of B from a C# application and call the "void Sign" with that object and catch the StatusEvent in C#.
How to call B::Showsts from A::call_A in order to achieve this? Please keep in mind that call_A is called from a delegate of the B class object.
Thank you in advance!
public class A{
public:
int call_A();
};
public ref class B{
private:
A* a1;
public:
void Sign(String^ ufile);
void Showsts(string sts);
delegate void GetResult(String^);
event GetResult^ StatusEvent;
SpyrusLib(void){
a1=new A();
}
protected: ~SpyrusLib(){
delete a1;
}
private:
String^ str;
delegate int MySignDelegate(String^);
int MySign(String^ file);
void Callbacksign(IAsyncResult ^ar);
};
void B::Sign(String^ ufile){
MySignDelegate^ signDel = gcnew MySignDelegate( this, &B::MySign );
AsyncCallback^ cb = gcnew AsyncCallback( this, &B::Callbacksign);
signDel->BeginInvoke( ufile , cb, signDel );
}
int B::MySign(String^ file){
stdstr=msclr::interop::marshal_as<std::string>(file);
a1->call_A(stdstr);
}
void B::Showsts(string sts){
str = gcnew String(sts.c_str());
StatusEvent(str);
}
int A::call_A(string stat){
?-Showsts(stat);
}
I'm not sure it's the best solution but I solved it adding the following things to the classes:
typedef void (__stdcall * Unmanagedstatus)(string sts);
using namespace std;
public class A{
private:
Unmanagedstatus sendmsg;
public:
int call_A();
spyrus(Unmanagedstatus unm)
{
sendmsg=unm;
}
};
public ref class B
{
private:
delegate void Managedstatus(string);
Managedstatus^ managed;
IntPtr unmanaged;
A* a1;
public:
SpyrusLib(void)
{
managed = gcnew Managedstatus(this, &B::Showsts);
unmanaged = Marshal::GetFunctionPointerForDelegate(managed);
a1=new A((Unmanagedstatus)(void*)unmanaged);
}
}
int A::call_A(string stat){
sendmsg(stat); // this will call B::Showsts and the events raised
//from Showsts are also working in the C# app
}
I have an interface, and I was trying an example on dynamic polymorphism as follows:
#include <iostream>
using namespace std;
class foo{
public:
virtual void set();
virtual void printValue();
};
class fooInt : public foo{
private:
int i;
public:
int get(){
return i;
}
void set(int val){ //override the set
i = val;
}
void printValue(){
cout << i << endl;
}
};
int main(){
foo *dt; //Create a base class pointer
dt = new fooInt; //Assign a sub class reference
dt->set(9);
}
However when I compile this, I get no matching function for call to ‘foo::set(int)’. Where am I going wrong? I tried to read this article, and I still couldn't figure out the mistake.
class foo has no method set(int). It has a method set(), but no method set(int).
If you intend to override an inherited method, the superclass method and your method must have the same signature:
class foo {
...
// If you really want an abstract class, the `= 0`
// ensures no instances can be created (makes it "pure virtual")
virtual void set(int) = 0;
...
}
This is because your definition of
virtual void set();
Should be
virtual void set(int val);
The corrected program is given here
#include <iostream>
using namespace std;
class foo {
public:
virtual void set(int val)=0;////////here you have void set() function with no argument but you tried to override void set(int val) which take one argument.
virtual void printValue()=0;
};
class fooInt : public foo{
private:
int i;
public:
fooInt()
{
cout<<"constructor called\n";
}
int get(){
return i;
}
void set(int val){ //override the set
i = val;
}
void printValue(){
cout << i << endl;
}
};
int main(){
foo *dt; //Create a base class pointer
dt=new fooInt;
dt->set(9);
dt->printValue();
}
Fault of the previous program were
1.You tried to override set() {no argument} with set(int val){one argument}.
2.When a class contain a pure virtual function,it must be implemented by its derived classes.
3. No object can be created of a class which contain a pure virtual function.But ref can be created.
Thanks