calling functions in another file

B

Ben

I am having trouble calling functions in another file.. Lets look at
the scenario below:

File xyz.cc: (I have both xyz.h and xyz.cc, for simplicity I've
avoided xyz.h)
------------
struct xyz {
int x, y, z;
};
xyz readxyz (fstream& inFile) {
xyz g;
g.x = 4;
g.y = 3;
g.z = 6;
return g;
}
// end File: xyz.cc

File abc.cc: (I have both abc.h and abc.cc, for simplicity I've
avoided abc.h)
------------
struct abc {
int a;
xyz b; // b of type xyz (struct)
int c;
};
abc readabc (fstream& inFile) {
abc h;
h.a = 5;
h.b = readxyz(inFile);
h.c = 10;
return h;
}

int main() {
.......
fstream inFile;
inFile.open(...);
.......
.......
abc val = readabc(inFile);
.......
.......
return 0;
}
// end File: abc.cc

when i compile using g++ (gcc version 3.2), i get errors like this:
/tmp/ccqCIxjA.o: In function `readabc(std::basic_ifstream<char,
std::char_traits<char> >&)':
/tmp/ccqCIxjA.o(.text+0xb8): undefined reference to
`readxyz(std::basic_ifstream<char, std::char_traits<char> >&)'

i'm not sure what it means... any suggestions?? i've also tried
putting readabc() and readxyz() inside the struct but i'm getting
similar error...

any help will be highly appreciated!

Thanx
Ben
 
E

E. Robert Tisdale

Ben said:
I am having trouble calling functions in another file..
Lets look at the scenario below:

[Let's not.]
cat xyz.h
#ifndef Guard_xyz_h
#define Guard_xyz_h 1

#include <fstream>

struct xyz {
int x, y, z;
};

xyz readxyz(std::ifstream& inFile);

#endif//Guard_xyz_h
cat xyz.cc
#include "xyz.h"

xyz readxyz(std::ifstream& inFile) {
xyz g;
g.x = 4;
g.y = 3;
g.z = 6;
return g;
}

// end File: xyz.cc
cat abc.h
#ifndef Guard_abc_h
#define Guard_abc_h 1

#include "xyz.h"

struct abc {
int a;
xyz b; // b of type xyz (struct)
int c;
};

abc readabc(std::ifstream& inFile);

#endif//Guard_abc_h
cat abc.cc
#include "abc.h"

abc readabc(std::ifstream& inFile) {
abc h;
h.a = 5;
h.b = readxyz(inFile);
h.c = 10;
return h;
}

// end File: abc.cc
cat main.cc
#include "abc.h"

int main(int argc, char* argv[]) {
// . . .
std::ifstream inFile("filename");
// . . .
abc val = readabc(inFile);
// . . .
return 0;
}
g++ -Wall -ansi -pedantic main.cc abc.cc xyz.cc

It works just fine for me.
 

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

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top