LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2006, 12:35 PM   #1
najam,shahid
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Rep: Reputation: 0
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.
 
Old 01-21-2006, 12:49 PM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 01-21-2006, 11:18 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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).
 
Old 01-22-2006, 05:27 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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;
}
 
Old 01-22-2006, 06:26 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Oops that does work, I apologise.
 
Old 01-22-2006, 10:03 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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..
 
Old 01-22-2006, 11:54 PM   #7
najam,shahid
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 01-22-2006, 11:57 PM   #8
najam,shahid
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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*)'
 
Old 01-23-2006, 04:22 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 01-23-2006, 04:29 AM   #10
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 01-23-2006, 10:18 AM   #11
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Code:
class String {
publis:
String();
String(String&);
String(const char*);
};
fix your copy constructor..
String(String&);
to
String(const String&);

clear?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need the Java code for process migration manujp Linux - Networking 3 01-11-2012 10:46 AM
no sound in sun solairs 10 mirdassou Solaris / OpenSolaris 2 09-01-2005 10:10 AM
istalling Solairs 9 (video card) Robert0380 Solaris / OpenSolaris 8 01-14-2004 10:27 AM
My solairs mchine is pretty slow after using it for 4-5 days anjan_bhushan Solaris / OpenSolaris 1 07-28-2003 12:44 PM
linux migration bulliver Linux - General 3 11-16-2002 12:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration