I’m writing a C++ Package for later use using Code::Blocks.
The project structure looks like this:
cNormal\
cNormal.cdp
src\
main.cpp # for testing purpose
cnormal_defs.h # important compiler definitions
cnormal_storage.h # includes all .h files from "./storage"
storage\
cnarray.h
cnarray.cpp
cnstack.h
cnstack.cpp
bin\
obj\
The cnormal_storage.h:
// cnormal_storage.h
// *****************************************************************
// Includes all necessary headers for the cNormal storage subpackge.
//
#ifndef _cNORMAL_STORAGE_H_
#define _cNORMAL_STORAGE_H_
#include "storage/cnarray.h"
#include "storage/cnstack.h"
#endif // _cNORMAL_STORAGE_H_
To test the classes, i create a main-function in main.cpp.
// main.cpp
// *****************************************************************
// The main-file.
//
#include <iostream>
#include "cnormal_storage.h"
using namespace std;
int main() {
cnArry<int> arr(10);
arr[9] = 999;
arr[0] = 0;
cout << arr[9] << endl;
cout << arr.getLength();
}
But the compiler (gcc) gives me undefined reference to ... errors about cnArray.
Now, the cnarray.cpp includes cnarray.h (as it is the implementation file), so using
#include "storage/cnarray.cpp" works just fine.
It seems like the compiler can’t find the implementation of cnarray.h which is located in cnarray.cpp.
I assume it’s because of the folder-structure, can you tell me how I can fix this ?
Even adding src\storage to the include directives does not fix it. (And I also don’t want to add it to the include-paths as this would be very unhandy for a package.)
I could spot the error now,
cnArray.hdeclared a template class and template classes cannot be implmented in another file than they are declared, because the compiler must know about the implementation when compiling, not when linking.I have found a workaround on the internet to
#includethe implementation in the headerfile, but exclude the implementation file from compiling.This works just fine now.
Cheers