Solved

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

Posted on 2006-06-27
3
207 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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