Visual C++ 2010 Error: LNK2020 Unresolved token - visual-studio-2010

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();
}
};

Related

QtConcurrent::run() calling another class method

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);
}
}

Register to C# event from C++/CLI

I'm writing C++ code that calls C# code. The C# may need to invoke methods back in the C++ code. If both parts were C# I think I would use following mechanism. Please note I pass EventHandler from ShouldBCpp to Csharp instead of registering in ShouldBCpp since ShouldBCpp does not know what csharp points to (& can't change CsharpBase).
public abstract class CsharpBase
{
public abstract void SomeMethodDoingActionInB();
}
public class Csharp : CsharpBase
{
public Csharp(EventHandler f)
{
MySpecialHook += f;
}
public event EventHandler MySpecialHook;
public override void SomeMethodDoingActionInB()
{
if (MySpecialHook != null)
MySpecialHook(this, null);
}
}
public class ShouldBCpp
{
public CsharpBase csharp;
public ShouldBCpp()
{
csharp = new Csharp(NotificationFromClassB); // actually using Activator::CreateInstance
}
public void NotificationFromClassB(object sender, EventArgs e)
{
}
public void Go()
{
csharp.SomeMethodDoingActionInB();
}
}
class Program
{
static void Main(string[] args)
{
ShouldBCpp shouldBCpp = new ShouldBCpp();
shouldBCpp.Go();
}
}
Question is how to write ShouldBCpp in C++/CLI. Bonus points for using delegate :)
Thank you
A simple translation to C++/CLI would look like this:
public ref class IsCppCLI
{
public:
CsharpBase^ csharp;
IsCppCLI()
{
csharp = gcnew Csharp(gcnew EventHandler(this, &IsCppCLI::NotificationFromClassB));
// You didn't show your Activator code,
// but I believe it would translate to C++/CLI as this:
csharp = dynamic_cast<CsharpBase^>(
Activator::CreateInstance(
Csharp::typeid,
gcnew array<Object^> {
gcnew EventHandler(this, &IsCppCLI::NotificationFromClassB)}));
}
void NotificationFromClassB(Object^ sender, EventArgs^ e)
{
}
void Go()
{
csharp->SomeMethodDoingActionInB();
}
}

Calling a managed class function from an unmanaged class object

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
}

Overriding the method and subscribing to the event is the same?

class A
{
public event EventHanler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
if (MyEvent!=null)
MyEvent(this, e);
}
public void DoEvent()
{
//................
MyEvent(this, new EventArgs());
}
}
class B: A
{
private A a = new A();
public B ()
{
a.MyEvent += MyMethod;
}
public void MyMethod(object sender, EventArgs e)
{
Console.WriteLine("Event handler");
}
}
class C : A
{
private A a = new A();
protected override void OnMyEvent(EventArgs e)
{
base.OnMyEvent(e);
Console.WriteLine("OnMyEvent overriding");
}
}
I subscribe to the event and override the method OnMyEvent() in the classes B and C. Pay attension calling the method base.OnMyEvent(e) is in the beginning of the method C.OnMyEvent(...).
As far as I'm concerned there are no differences here. In other words if I call base.OnMyEvent(e) in the beginning of the overriding method, it would mean the same as I just subscribe to the event?
Are there actually no differences?
There is a difference:
Invoking C.OnMyEvent() raises MyEvent. (Conversely, raising MyEvent will not invoke C.OnMyEvent().)
B.MyMethod handles MyEvent. Thus, raising MyEvent will invoke B.MyMethod. (Conversely, invoking B.MyMethod will not raise MyEvent.)

raising events in chaining classes

say I have three classes: class1, control1 and form1; form1 instantiate contorl. and control1 instantiate class1, the later produces some event that I need to 'bypass' to form1, to achieve that I have made an intermediate function as shown below:
public delegate void TestHandler(String^ str);
public ref Class class1
{
event TestHandler^ TestHappen;
void someFunction()
{
TestHappen("test string");
}
};
public ref Class control1
{
event TestHandler^ TestHappen;
class1^ class1Obj;
control1()
{
class1Obj= gcnew class1();
class1Obj->TestHappen+= gcnew TestHandler(this,&control1::onTest);
}
void onTest(String^ str)
{
TestHappen(str);
}
};
public ref Class form1
{
control1^ control1Obj;
form1()
{
control1Obj= gcenw control1();
control1Obj->TestHappen+= gcnew TestHandler(this,&form1::onTest);
}
void onTest(String^ str)
{
//do something with the string...
}
};
I don't want to use class1 in form1, are there a way to remove the intermediate onTest() function.
Yes, if you use a custom event, you can write its add-handler and remove-handler functions so that they add and remove the delegate directly from another object's event.
For example:
public ref class control1 // in "ref class", class is lowercase!
{
class1 class1Obj; // stack-semantics syntax, locks class1Obj lifetime to be same as the containing control1 instance
public:
event TestHandler^ TestHappen {
void add(TestHandler^ handler) { class1Obj.TestHappen += handler; }
void remove(TestHandler^ handler) { class1Obj.TestHappen -= handler; }
}
};

Resources