using class library..help pls

hello, i have these two projects one is a class library and the other one is a WinForm.. i add the Class Library in Winform as reference and so on....

in Class Library i have these code...
1
2
3
4
5
6
7
8
9
10
public ref class Class1
	{
		public:
		int add(int a, int b){
                 int result = a + b;
				 return result;
	
                   }

	};



now in WinForm ..

1
2
3
4
5
6
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 DLL::Class1::add(6,2);
				 MessageBoxW(0,add,L"info", MB_OK);
					 
				 
			 }

obviously its not gonna work..i have these errors
1
2
error C2352: 'DLL::Class1::add' : illegal call of non-static member function
error C3861: 'add': identifier not found



im trying to show the result in a MessageBox...can u pls help me how to do it

thanks in advance
Last edited on
Looks like you don't quite understand how to use a class.

A class is a new kind of object. Like an int, or a double, or a char. If you want to use one, first you create an instance of one, then you can use it.

This code does not work:
x = 7; // no such object named x
but this code does work
1
2
int x;
x = 7; // x has been created we can use it. 


In the same way, you must create an instance of the class first, and then you can use that instance1.

You're not using C++; you're using some kind of Microsoft language, I'm guessing, but the syntax you're looking for is probably something like:

1
2
Class1 anInstanceOfTheClass; // Now we have created an instance of type Class1
anInstanceOfTheClass.add(6,2); // Use the member function add in the object we created 



-------------
1) If a class definition contains static methods, you can use those without creating an instance of the class. However, given that you are unsure of how to use classes, best to start with the basics.
Last edited on
i already tried that first and got me an error,,i forgot to add ^ symbol
so i skipped it then i post this thread


this works for me :/

DLL::Class1^ test = gcnew DLL::Class1();

test->add(2,5);

thnx !!
Last edited on
Topic archived. No new replies allowed.