Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String issue

Posted on 2006-07-06
19
Medium Priority
?
168 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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

610 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