LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Migration of C++ code from Solairs to Linux ES3 (https://www.linuxquestions.org/questions/programming-9/migration-of-c-code-from-solairs-to-linux-es3-406209/)

najam,shahid 01-21-2006 12:35 PM

Migration of C++ code from Solairs to Linux ES3
 
I am facing problems while migrating application written in C++/Solaris to Linux ES3.

Consider the following snippet:

class String {
publis:
String();
String(String&);
String(const char*);
};

class Logger {
public:
int logMsg(String);
};

int func(){
Logger objLogger;
objLogger.logMsg("test"); // compiler error on this line
return 1;
}

While compiling the above code from gcc, I am getting following error:

Can not find funciton String::String(String)
candidates are: String::String(String&)
String::String(const char*)

Trying to convert arg 1 of objLogget.logMsg("test") to String::String.

This code is compiled successfully on Solaris.

Please help me out.

XavierP 01-21-2006 12:49 PM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

dmail 01-21-2006 11:18 PM

the parameter here is a class String
Code:

  int logMsg(String);
the parameter here is a char* or could be std::string
Code:

  objLogger.logMsg("test"); // compiler error on this line
it looks like you are trying to create a wrapper class for a string and theres really no need if you use an std::string; it has a func which returns a const char* (c style string).

Hivemind 01-22-2006 05:27 AM

While I agree with dmail that rolling your own string class is a waste of time, unless it's an assignment or for learning purposes, this compiles just fine:

Code:

class String
{
public:
  String(const String &) {}
  String(const char *) {}
};

static void logMsg(const String &) {}

int
main()
{
  /* logMsg expects a const String reference and String has a
      constructor that takes a const char * and the literal "foo"
      is of that type. It wouldn't work, however, if logMsg didn't
      take a const reference because you can't bind a reference to
      a temporary. */
  logMsg("foo");

  return 0;
}


dmail 01-22-2006 06:26 AM

Oops that does work, I apologise.

xhi 01-22-2006 10:03 PM

najam,shahid,

maybe if you copy and past the compiler error message, it would clear things up..

i think somthing is getting lost in translation.. as was mentioned the code you posted should work..

najam,shahid 01-22-2006 11:54 PM

Re: Migration of C++ code from Solairs to Linux ES3
 
Hi HiveMind,
Thanks for youyr reply. It really did work.

But the problem is that I have to compile the same code on Solaris and Linux.

As suggested by you,
...
logMsg(const String&);
...

On Solaris, following compiler error is the outcome:

initialization of non-const reference type `class String &'
from rvalue of type `String'
in passing argument 1 of `Logger::Print(String &)'

Is there any way that the same code is used for both platforms.

najam,shahid 01-22-2006 11:57 PM

Hello Xhi,
Here's the exact compiler error:

no matching function for call to `String::String(String)'
candidates are: String::String(char*)
String::String(String&)
initializing argument 1 of `int Logger::Print(String)' from
result of `String::String(char*)'

Hivemind 01-23-2006 04:22 AM

Huh? In your first post you indicated that you were porting from Solaris to Linux and you encountered that problem, now you say it's the other way around. Compile my program, without modification, on both platforms, paste compiler output/version for each. Seems to me you're using a poor implementation of the C++ standard on one of the platforms.

Hivemind 01-23-2006 04:29 AM

I also said that if you are going to bind a temporary to a reference, that reference must be const. Doesn't appear to be in your code.

xhi 01-23-2006 10:18 AM

Code:

class String {
publis:
String();
String(String&);
String(const char*);
};

fix your copy constructor..
String(String&);
to
String(const String&);

clear?


All times are GMT -5. The time now is 06:14 AM.