Sep 11, 2012 at 10:49pm UTC
Hello everyone.
I am having issues with using a function within two c files that is declared in a header file.
Here is basically what I am trying to do.
main.h
1 2
#pragma once
void foo();
another.h
1 2
#pragma once
void bar();
main.cpp
1 2 3 4 5 6
#include "main.h"
#include "another.h"
int main() {
bar();
}
another.cpp
1 2 3 4 5 6
#include "main.h"
#include "another.h"
void bar() {
foo();
}
undefined reference to: bar() in another.h line blah
Would I have to use some form of extern or something?
Thanks for any help
- strongdrink
Last edited on Sep 11, 2012 at 10:52pm UTC
Sep 11, 2012 at 11:07pm UTC
The function bar calls the function foo. The function foo does not exist. The linker cannot find it because it does not exist. Otherwise, this compiles and links fine.
Last edited on Sep 11, 2012 at 11:07pm UTC
Sep 11, 2012 at 11:23pm UTC
Here is my exact error:
/home/elijah/code/chip/src/block/blocks/player.cpp:46:
undefined reference to `void push_back_bytes<unsigned short>(std::deque<unsigned char, std::allocator<unsigned char> >*, unsigned short)'
Player is a class.
push_back_bytes is declared in main.h.
main.h is inluded in the top of player.cpp
push_back_bytes is called by one of the methods of the Player class.
EDIT:
Aha! according to this post
http://v2.cplusplus.com/forum/windows/72494/ -- I can't have a template split among several files.
Last edited on Sep 11, 2012 at 11:38pm UTC