Ok, this has been covered already, for example here: static array class variable "multiple definition" C++
But I am missing some details here.
I have got the following classes: Foo.cpp
#include <iostream>
#include "Model.h"
int main(int argc, char** argv){
std::cout << "hello" << std::endl;
return 0;
}
Model.h
#ifndef MODEL_H
#define MODEL_H
#include <string>
#include "md2Loader.h"
class Model{
public:
Model();
Model(const std::string& model_file);
private:
md2_header_t header;
modelData_t model;
};
#endif
Model.cpp
#include "Model.h"
#include "md2Loader.h"
Model::Model(){}
Model::Model(const std::string& model_file){
model = md2Loader::load_model(model_file);
}
and md2Loader.h
#ifndef MD2LOADER_H
#define MD2LOADER_H
struct modelData_t{
int numVertices;
int numTextures;
// etc
};
struct md2_header_t {
std::string version;
};
class md2Loader{
public:
md2Loader(){};
static modelData_t load_model(const std::string& model_file);
};
modelData_t md2Loader::load_model(const std::string& model_file){
modelData_t result;
result.numVertices = 1000;
result.numTextures = 10;
return result;
}
#endif
The linker complains of multiple definitions. But I am not quite sure, why. Do the #ifndef, #define preprocessor directives not help? I kind of get it that md2Loader.h get included to both Model.cpp and Model.h. When I do the implementation in Model.h and leave Model.cpp away it will compile and link just fine. I thought, that the directives for the preprocessor prevent that from happening, but obviously I am missing something there.