By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

calling functions in another file

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ben wrote:
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.
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.