namespace,class problem

D

Daqian Yang

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;
}
---
 
R

Rolf Magnus

Daqian said:
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.
 
J

Jay Nabonne

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
 
D

Daqian Yang

thx! now it works perfectly!

it makes me better understanding on namespace and class.
 
D

Daqian Yang

hello,

sorry about the off-topic.

jay's solution seems right to my code. :)


Rolf Magnus said:
Daqian said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top