Hi, why would this not work? (example)

fileA.h:
// declare the function
void methodA();

fileA.cpp:

// define the function
void methodA(){};

fileX.cpp: 
include "fileA.h"
// call it
methodA(); <--linker error, can't find the symbol

All my headers have the #ifndef guard.
Thanks!

Recommended Answers

All 3 Replies

compile as

c++ fileX.cpp fileA.cpp

Hi, why would this not work? (example)

fileA.h:
// declare the function
void methodA();

fileA.cpp:

// define the function
void methodA(){};

fileX.cpp: 
include "fileA.h"
// call it
methodA(); <--linker error, can't find the symbol

All my headers have the #ifndef guard.
Thanks!

Line 13 - is this inside of a function somewhere?
Line 8 - I don't imagine this matters, but you don't need the semicolon.

Line 6 - Do you want #include "fileA.h" here?

I'm not sure how you're compiling this. If you're having an IDE create a makefile for you, without the #include "fileA.h" line in fileA.cpp, I'm not sure that the correct makefile will be created, so it won't link correctly. Try putting that line in, get rid of the semicolon at the end of line 8, and make sure that line 13 is part of a function. Is line 13 in main ()?

Ok sry, I found it. I had all of the stuff in the header, in a namespace and forgot that (like classes) the implementation of the function OF COURSE also needs to have namespace::function ...how else would it know where it lies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.