String issue

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?
PLABBAsked:
Who is Participating?
 
DanRollinsConnect With a Mentor Commented:
Please see my comment above: http:#17061546 and the link I provided there.  It explains the situation very clearly.
-- Dan
0
 
AlexFMCommented:
test1("text");

How is this code related to MyType class?
0
 
bastibartelCommented:
Hi there,

Do you mean:
    test2("text");

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

Cheers,
Sebastian

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
PLABBAuthor Commented:
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
 
bastibartelCommented:
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
 
PLABBAuthor Commented:
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
 
bastibartelCommented:
( 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
 
bastibartelCommented:
correction,
Try:
test2(System::String("test"));
     ^
0
 
bastibartelCommented:
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
 
PLABBAuthor Commented:
I have made:

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

Still cannot convert parameter from 'const char [5]' to 'MyType' :(
0
 
bastibartelCommented:
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
 
DanRollinsCommented:
Just for fun, try

   test2( S"hi there" );
0
 
bastibartelCommented:
What's it with the S"" - trick -  Mr Rollins ?
0
 
PLABBAuthor Commented:
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
 
DanRollinsCommented:
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
 
PLABBAuthor Commented:
doesn't help either
0
 
DanRollinsCommented:
Does this work?

void main()
{
     MyClass foo("Test");
     func( foo );
}
0
 
PLABBAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.