Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I'm new to VC++, and cannot figure out why my MessageBox call here is error'ing out when executed:


	void center(char *s)
	{
	 int len;
	
	 len = (strlen(s));

	 MessageBox::Show(len);
	}



The resulting build would produce:

1>c:\users\test\documents\visual studio 2010\projects\test\test\Form1.h(90): warning C4068: unknown pragma
1>c:\users\test\documents\visual studio 2010\projects\test\test\Form1.h(116): warning C4101: 'found' : unreferenced local variable
1>c:\users\test\documents\visual studio 2010\projects\test\test\Form1.h(152): error C2665: 'System::Windows::Forms::MessageBox::Show' : none of the 21 overloads could convert all the argument types
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: could be 'System::Windows::Forms::DialogResult System::Windows::Forms::MessageBox::Show(System::String ^)'
1>          while trying to match the argument list '(int)'



Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 2-Apr-12 18:01pm    
It looks like C++/CLI and .NET, mixed mode with native Win32. Tag it.
--SA

1 solution

The error message clearly shows the problem: it expects the string, not integer. And it should be System::String, not char*. The solution of the problem is: use MessageBox::Show(len.ToString()); however, it's generally bad to mix native C++ code with .NET C++/CLI without special need for it.

One nice discipline for mixed-mode code I would advice is: strictly classify all files into 1) pure managed C++/CLI code; 2) pure unmanaged C++ code; 3) managed C++/CLI code wrapping unmanaged C++ types in managed "ref" classes or structures; 4) unmanaged C++ code wrapping managed "ref" types in unmanaged classes or structures. In most mixed-mode projects, either class #3 or #4 will be used, not both of them.

—SA
 
Share this answer
 
Comments
stoneferry_tiger 3-Apr-12 10:21am    
Thanks for the response, I have changed the code to this:

void center(System::String)
{
//int len;

//len = (strlen());

//MessageBox::Show(len.ToString());
}

But now it errors out as this:

1>c:\users\test\documents\visual studio 2010\projects\test\test\Form1.h(146): error C3149: 'System::String' : cannot use this type here without a top-level '^'

Could someone advise the correct syntax? Thanks
Sergey Alexandrovich Kryukov 3-Apr-12 11:59am    
I tested it before posting the answer. Perhaps you should understand how .NET references work, instead of trial-and-error. You see, your code is commented out, and second line is wrong. You can show real code, formatted, using "Improve question".
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900