Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-27
3
Medium Priority
?
213 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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