Click to See Complete Forum and Search --> : simple codes, cannot run in Solaris, need help


benchris
December 10th, 2002, 01:16 PM
I have a very simple C++ program running fine at VC++ but having prob in compiling in Solaris using g++ .
Here are the complete codes.
Thanks in advance



#if !defined Utility_h
#define Utility_h

#include <string>

class Utility
{
public:
Utility();
virtual ~Utility();
static std::string dtoStr(double);
static std::string itoStr(int i);
static std::string out();

};

#endif // !defined Utility_h


#include "Utility.h"
#include <string>
#include <stdio.h>

using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Utility::Utility()
{

}

Utility::~Utility()
{
}


string Utility::dtoStr(double d){

char strDouble[15];
//strDouble[14]='\0';
sprintf(strDouble,"%f",d);
//string s(strDouble);
return strDouble;
}

string Utility::itoStr(int i){

char strInt[15];
//strDouble[14]='\0'
sprintf(strInt,"%i",i);;
//itoa(i,strInt,10);
//sprintf(strDouble,"%f",d);
//string s(strDouble);
return strInt;
}



main.cxx

#include <iostream.h>
#include <string>
#include "Utility.h"

using namespace std;

int main(int argc, char **argv)
{
/*
double d=3.14;
char strDouble[15];
sprintf(strDouble,"%f",d);
*/
// cout << (T_STR) (Utility::dtoStr(3.14)).c_str() << endl;
string s=Utility::out();
//string s=Utility::itoStr(1);
//cout <<s<<endl;


return 0;
} // main

Philip Nicoletti
December 10th, 2002, 01:33 PM
What error do you get ?

It compiled OK for me using g++ on Linux.

It will not link correctly ... there is no code
for Utility::out()

benchris
December 10th, 2002, 01:48 PM
Here is the part for out.

string Utility::out(){
return "out test";
}


In fact, you can call other two methods. I was so sesperate so just putting something simple as returnning a string.

In Solaris, Utility.h and Utility.cpp and main.cpp are in the same folder, when call
g++ -o m.x main.cpp

get error

Undefined first referenced
symbol in file
Utility::out(void) /var/tmp/ccyZGt5g.o
ld: fatal: Symbol referencing errors, No output written to m.x
collect2: ld returned 1 exit status

Very frustrated :(

Philip Nicoletti
December 10th, 2002, 01:59 PM
You are not compiling the code in utility.cpp ...

g++ -o m.x main.cpp utility.cpp

benchris
December 10th, 2002, 02:02 PM
after working on WIndows most of the time, I am dumb now :(.
Thank you very much for your help.