469,584 Members | 2,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,584 developers. It's quick & easy.

namespace,class problem

hello all

GCC gives me this error:

g++ -c -o x-main_a2.o x-main_a2.cpp
g++ -Wall -pedantic -c xfile_a2.cpp
g++ -Wall -pedantic x-main_a2.o xfile_a2.o -o x
Undefined first referenced
symbol in file
xf::Xfile::stats() x-main_a2.o
ld: fatal: Symbol referencing errors. No output written to x
collect2: ld returned 1 exit status
make: *** [x] Error 1
my programs contain 3 files. here are:

dragon:/home/cstudent/05/052203y/3773/a2> cat xfile_a2.cpp
#include "xfile_a2.h"

namespace xf{
Xfile::Xfile(string fname, int maxLineLength, int maxLines){
}
void compress() throw (runtime_error){

}
void stats() throw (runtime_error){
}
}

--
dragon:/home/cstudent/05/052203y/3773/a2> cat xfile_a2.h
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
using namespace std;

namespace xf{
class Xfile{

public:

Xfile(string = "mydata", int = 10, int = 10); //constructor
void compress() throw (runtime_error);
void stats() throw (runtime_error);

private:

typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
};
}
--
dragon:/home/cstudent/05/052203y/3773/a2> cat x-main_a2.cpp
#include <iostream>
using namespace std;
#include "xfile_a2.h"

int main(){
xf::Xfile x;

cout <<"before compressing"<<endl;

x.stats();

cout <<"after compressing"<<endl;

//x.compress();
//x.stats();

return 0;
}
---
Jul 22 '05 #1
4 1220
Daqian Yang wrote:
hello all

GCC gives me this error:

g++ -c -o x-main_a2.o x-main_a2.cpp
g++ -Wall -pedantic -c xfile_a2.cpp
g++ -Wall -pedantic x-main_a2.o xfile_a2.o -o x
Undefined first referenced
symbol in file
xf::Xfile::stats() x-main_a2.o
ld: fatal: Symbol referencing errors. No output written to x
collect2: ld returned 1 exit status
make: *** [x] Error 1


You got the wrong linking order. Change it to:

g++ -Wall -pedantic x-file_a2.o xmain_a2.o -o x

Btw, this is off-topic here, since comp.lang.c++ is only about the
language itself, not about any compilers/build tools.

Jul 22 '05 #2
On Tue, 27 Jan 2004 13:42:16 -0400, Daqian Yang wrote:

Change this:
namespace xf{
Xfile::Xfile(string fname, int maxLineLength, int maxLines){
}
void compress() throw (runtime_error){

}
void stats() throw (runtime_error){
}
}


to:

xf::Xfile::Xfile(string fname, int maxLineLength, int maxLines){
}

void xf::Xfile::compress() throw (runtime_error){
}

void xf::Xfile::stats() throw (runtime_error){
}

This prevents you from accidentally defining new functions (as you did
above) instead of defining the ones in the class that you want.

HTH

- Jay

Jul 22 '05 #3
thx! now it works perfectly!

it makes me better understanding on namespace and class.
"Jay Nabonne" <ja*@rightagainBYTEME.com> wrote in message
news:pa****************************@rightagainBYTE ME.com...
On Tue, 27 Jan 2004 13:42:16 -0400, Daqian Yang wrote:

Change this:
namespace xf{
Xfile::Xfile(string fname, int maxLineLength, int maxLines){
}
void compress() throw (runtime_error){

}
void stats() throw (runtime_error){
}
}


to:

xf::Xfile::Xfile(string fname, int maxLineLength, int maxLines){
}

void xf::Xfile::compress() throw (runtime_error){
}

void xf::Xfile::stats() throw (runtime_error){
}

This prevents you from accidentally defining new functions (as you did
above) instead of defining the ones in the class that you want.

HTH

- Jay

Jul 22 '05 #4
hello,

sorry about the off-topic.

jay's solution seems right to my code. :)
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...
Daqian Yang wrote:
hello all

GCC gives me this error:

g++ -c -o x-main_a2.o x-main_a2.cpp
g++ -Wall -pedantic -c xfile_a2.cpp
g++ -Wall -pedantic x-main_a2.o xfile_a2.o -o x
Undefined first referenced
symbol in file
xf::Xfile::stats() x-main_a2.o
ld: fatal: Symbol referencing errors. No output written to x
collect2: ld returned 1 exit status
make: *** [x] Error 1


You got the wrong linking order. Change it to:

g++ -Wall -pedantic x-file_a2.o xmain_a2.o -o x

Btw, this is off-topic here, since comp.lang.c++ is only about the
language itself, not about any compilers/build tools.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Kevin Newman | last post: by
5 posts views Thread by Alexis | last post: by
5 posts views Thread by Mike Oliszewski | last post: by
10 posts views Thread by anders | last post: by
4 posts views Thread by Kevin Newman | last post: by
4 posts views Thread by R. Nachtsturm | last post: by
5 posts views Thread by michael.d.pedersen | last post: by
7 posts views Thread by Armin Zingler | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.