Solved

String issue

Posted on 2006-07-06
19
161 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
  • 7
  • 6
  • 4
  • +1
19 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
test1("text");

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

Expert Comment

by:bastibartel
Comment Utility
Hi there,

Do you mean:
    test2("text");

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

Cheers,
Sebastian

0
 

Author Comment

by:PLABB
Comment Utility
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
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
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
Comment Utility
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
Comment Utility
( 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
Comment Utility
correction,
Try:
test2(System::String("test"));
     ^
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
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
Comment Utility
I have made:

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

Still cannot convert parameter from 'const char [5]' to 'MyType' :(
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
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
Comment Utility
Just for fun, try

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

Expert Comment

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

Author Comment

by:PLABB
Comment Utility
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
Comment Utility
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
Comment Utility
doesn't help either
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Does this work?

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

Author Comment

by:PLABB
Comment Utility
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
Comment Utility
Please see my comment above: http:#17061546 and the link I provided there.  It explains the situation very clearly.
-- Dan
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

12 Experts available now in Live!

Get 1:1 Help Now