Solved

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

Posted on 2006-06-27
3
206 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
  • 2
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now