I am trying to access variables and functions defined in a namespace in a header file. However, I get the error: xor.cpp:(.text+0x35): undefined reference to "the function in header file" collect2: error: ld returned 1 exit status
. It seems to me that the compilation steps are OK after reading this post, and also because I can access variables in this header file, but calling the function returns the error mentioned above. My question is: How can I access those functions in the namespace from my main.cpp ? What am I doing wrong ?
The case with a class is clear to me, but here I don't understand because I am not supposed to create an object, so just calling the namespace in front should be OK (?).
Edit
After changes suggested by Maestro, I have updated the code the following way, but it still doesn't work. The error I get is the same. If I define using NEAT::trait_param_mut_prob = 6.7;
I get the error: xor.cpp:127:36: error: expected primary-expression before ‘=’ token
Main c++
#include "experiments.h"
#include "neat.h"
#include <cstring>
int main(){
const char *the_string = "test.ne";
bool bool_disp = true;
NEAT::trait_param_mut_prob;
trait_param_mut_prob = 6.7;
NEAT::load_neat_params(the_string ,bool_disp);
std::cout << NEAT::trait_param_mut_prob << std::endl;
return 0;
}
neat.h
#ifndef _NERO_NEAT_H_
#define _NERO_NEAT_H_
#include <cstdlib>
#include <cstring>
namespace NEAT {
extern double trait_param_mut_prob;
bool load_neat_params(const char *filename, bool output = false); //defined HERE
}
#endif
neat.cpp
#include "neat.h"
#include <fstream>
#include <cmath>
#include <cstring>
double NEAT::trait_param_mut_prob = 0;
bool NEAT::load_neat_params(const char *filename, bool output) {
//prints some stuff
return false;
};
Makefile
neat.o: neat.cpp neat.h
g++ -c neat.cpp