Solved

String issue

Posted on 2006-07-06
19
166 Views
Last Modified: 2010-04-24
VC++ 2005

void test1(System::String^ inVal)
{
}

test1("text"); //=> works

//####

ref class MyType
{
protected:
           System::String^ MyString;
public:
          MyType(System::String^ inValue)
          {
              MyString = inValue
          }
          MyType(char inValue[]);            
          {
              MyString = gcnew System::String(inValue);
          }
};

void test2(MyType inVal)
{
}

test1("text"); //=> error C2664: cannot convert parameter from 'const char [5]' to 'MyType'

Why?
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
  • 7
  • 6
  • 4
  • +1
19 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17049031
test1("text");

How is this code related to MyType class?
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17049062
Hi there,

Do you mean:
    test2("text");

if so, try
     test2(MyType("text"));

Cheers,
Sebastian

0
 

Author Comment

by:PLABB
ID: 17049168
yes that is right it should be:
test2("text"); //=> error C2664: cannot convert parameter from 'const char [5]' to 'MyType'

MyType("test") would work, but there is constructor which should handle the job.

          MyType(System::String^ inValue)
          {
              MyString = inValue
          }
0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
LVL 5

Expert Comment

by:bastibartel
ID: 17049223
Yes but you need to construct an object  of MyType explictely to pass it to test2.
The function test2() expects a MyType, so give it an MyType.

MyType A("hello world");
test2(A);

// or short:
test2(MyType("hello world"));

Alternatively you could provide a conversion operator. But a t this point I wouldn't recommend that - they can be quite confusing and error prone.




0
 

Author Comment

by:PLABB
ID: 17049297
Buit if you have that example with System::String

void test1(System::String^ inVal)
{
}

test1("text");

you don't have to create object. Why?

0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17049406
( I am not sure about .Net basic types - I hope char* still exists there in .Net ;-)
---------------------------------------------------------------------------------------------

System::String  probably has a constructor that accepts const char*.

You see, "test" is not C-like string, but will be accepted by a System::String(const char*txt) constructor.
And hence, construct a System::String to be passed to test1(System::String^)

Try:
test1(System::String("test"));

or provide another constructor
MyType::MyType(const char*);


Cheers,
Sebastian




0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17049411
correction,
Try:
test2(System::String("test"));
     ^
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17049559
I must have been totally distraced: Ignore the two previous posts

System::String  probably has a constructor that accepts const char*.

You see, in C "test" is a 'const char*' , and it will be accepted by a System::String(const char*txt) constructor,
hence construct a System::String to be passed to test1(System::String^)

Try:
test2(System::String("test"));

or provide another constructor
MyType::MyType(const char*);

Cheers,
Sebastian

0
 

Author Comment

by:PLABB
ID: 17050082
I have made:

MyType::MyType(const char* Value)
{      
        MyString = gcnew System::String(Value);      
}

Still cannot convert parameter from 'const char [5]' to 'MyType' :(
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17050213
Hi again,

Does
   test2(MyType("hello"));
or
   test2(System::String("test"));
work ?

----------------------------------------------------------------
The following works in VC6++
It probably does not compile for you - I am just trying to see the difference.


class CMyClass
{
  public:
  CMyClass(const char *Str)
       {  sprintf(m_Str,"%s", Str);   }

   char m_Str[256];
};

void func(CMyClass A)
{
     TRACE(A.m_Str);
}
void main()
{
     func("Test");
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17055155
Just for fun, try

   test2( S"hi there" );
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17055169
What's it with the S"" - trick -  Mr Rollins ?
0
 

Author Comment

by:PLABB
ID: 17056463
following is true for native classes. But if we would declare it as ref class it wouldn't compile

class CMyClass
{
  public:
  CMyClass(const char *Str)
       {  sprintf(m_Str,"%s", Str);   }

   char m_Str[256];
};

void func(CMyClass A)
{
     TRACE(A.m_Str);
}
void main()
{
     func("Test");
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17061546
Sorry, I can't test this right now, but the problem may relate to what is described here:
      C++ Stack Semantics for Reference Types  
      http://msdn2.microsoft.com/en-us/library/ms177191.aspx

When you use semantics like:
      test2( "hi")
you are asking the compiler to create a temporary MyType object to pass to test2.  Since the compiler does not generate a copy constructor or a default assignment operator, it may be choking.  So you may need to add these to your class definition as shown in the example in that link.

-- Dan
0
 

Author Comment

by:PLABB
ID: 17097310
doesn't help either
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17103532
Does this work?

void main()
{
     MyClass foo("Test");
     func( foo );
}
0
 

Author Comment

by:PLABB
ID: 17198563
It writes that I do not have copy consstructor. But I do not need one since I wanted that class to be a ref class.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 17198689
Please see my comment above: http:#17061546 and the link I provided there.  It explains the situation very clearly.
-- Dan
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified 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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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