【发布时间】:2018-04-01 04:34:20
【问题描述】:
我对在 c++ 中使用头文件非常陌生,并且在使用一个源文件中定义的另一个源文件中的方法时遇到了问题。我已将源文件与头文件链接起来。
定义.cpp:
#include "header.h"
int c::foo (int bar){
return bar;
}
function_call.cpp:
#include "header.h"
int c::other_function( int num ){
int b = foo(num);
return b;
}
header.h:
#ifndef HEADER_H_
#define HEADER_H_
class c {
public:
int foo(int bar);
}
#endif /*HEADER_H_*/
我的 function_call.cpp 出现编译错误:
error: 'foo' was not declared in this scope
我是不是忽略了什么?
【问题讨论】:
-
我认为您的代码位不是真实的。如果您将代码 sn-ps 传递给编译器,那么第一个错误将是非常干净的
error: ISO C++ forbids declaration of ‘foo’ with no type [-fpermissive]。下一个错误是error: ; missing after class declaration。要达到您引用的错误,代码和包含必须不同。
标签: c++ header-files declaration definition