Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…

710 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