Solved

Passing user type default argument to the function (Conversion Problem?)

Posted on 2006-06-27
3
212 Views
Last Modified: 2010-04-24
I have made wrapper for string type.
I have implemented conversion function:
operator System::String^()
{
    return MyString;  // return System::String
}
and constructor ...
StringWrapper (System::String^ Value)
{
    MyString = Value;
}

When I declare:
StringWrapper SW = "23";   // Runs well

But it does not work in the function
void test(StringWrapper Message = "test")
{
}
I receive following errors:

error C2440: 'default argument' : cannot convert from 'System::String ^' to 'StringWrapper'
error C2548: 'test' : missing default parameter for parameter 1

Why? How to correct that?
0
Comment
Question by:PLABB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16990547
In .NET overloaded functions are used instead of default parameters:

void test(StringWrapper^ Message)
{
}

void test()
{
    test(gcnew StringWrapper("test"));
}

I replaced StringWrapper Message with StringWrapper^ Message, since this is reference type.
0
 

Author Comment

by:PLABB
ID: 16990596
it doesn't help.

I need default parameter to pass to the function => for example
void test (int _test = 3)
{
}
and that works well.


//  talking about VC++2005
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 16990964
From .NET Framework Developer's Guide:
Do use member overloading rather than defining members with default arguments. Default arguments are not CLS-compliant and cannot be used from some languages.
For caller overloaded functions look exactly like function with default arguments.

This definition is compiled:

void test(StringWrapper^ Message = gcnew StringWrapper("test"))
{
}
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question