【发布时间】:2020-05-15 07:58:38
【问题描述】:
我正在尝试访问在头文件的命名空间中定义的变量和函数。但是,我收到错误消息:xor.cpp:(.text+0x35): undefined reference to "the function in header file" collect2: error: ld returned 1 exit status。看了这个post,在我看来编译步骤没问题,也因为我可以访问这个头文件中的变量,但是调用函数返回上面提到的错误。我的问题是:如何从我的 main.cpp 访问命名空间中的这些函数?我做错了什么?
类的情况我很清楚,但这里我不明白,因为我不应该创建一个对象,所以只调用前面的命名空间应该可以(?)。
编辑
在 Maestro 建议的更改之后,我已按照以下方式更新了代码,但它仍然无法正常工作。我得到的错误是一样的。如果我定义 using NEAT::trait_param_mut_prob = 6.7; 我得到错误:xor.cpp:127:36: error: expected primary-expression before ‘=’ token
主要 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;
};
生成文件
neat.o: neat.cpp neat.h
g++ -c neat.cpp
【问题讨论】:
-
标题中的
#endif在哪里? -
这并没有解决问题,但是以下划线开头后跟大写字母 (
_NERO_NEAT_H_) 的名称和包含两个连续下划线的名称保留供实现使用。不要在你的代码中使用它们。 -
@Maestro 确实我忘了复制帖子中的#endif,但它在代码中
-
@PeteBecker 谢谢你的评论。我在这里实际上要做的是修改我下载的包,所以那些 NERO_NEAT_H 没有问题
-
@Joachim — 下载文件并不能免除它的规则。该标识符保留供实现使用。
标签: c++ function makefile namespaces header-files