在另一个 cpp 文件中使用 1 个 cpp 文件中的函数时出现问题
我有 1 个带有 main() 的 cpp 文件。 它依赖于另一个(例如 header.hpp)中的结构和函数。 这些结构体以及函数原型在 header.hpp 中定义。这些函数在 header.cpp 中实现。
当我尝试编译时,我收到一条错误消息:
undefined reference to `see_blah(my_thing *)`
So to go anoverview:
header.hpp:
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
int blah;
};
int see_blah(my_thing*);
#endif
header.cpp:
#include "header.hpp"
int see_blah(my_thing * thingy){
// ...
}
main.cpp:
#include <iostream>
#include "header.hpp"
using namespace std;
int main(void)
{
thinger.blah = 123;
cout << see_blah(&thinger) << endl;
return 0;
}
I don't idea what I do bad, and I can't find any questions 。感谢您的任何答复,非常感谢!
I have 1 cpp file with main().
It relies on structs and functions in another (say, header.hpp).
The structs are defined in header.hpp, along with function prototypes. The functions are implemented in header.cpp.
When I try to compile, I get an error message saying:
undefined reference to `see_blah(my_thing *)`
So to give an overview:
header.hpp:
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
int blah;
};
int see_blah(my_thing*);
#endif
header.cpp:
#include "header.hpp"
int see_blah(my_thing * thingy){
// ...
}
main.cpp:
#include <iostream>
#include "header.hpp"
using namespace std;
int main(void)
{
thinger.blah = 123;
cout << see_blah(&thinger) << endl;
return 0;
}
I have no idea what I'm doing wrong, and I can't find any answers. Thanks for any answers, they are very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该意识到您的结构定义末尾缺少一个分号。这意味着它将两个(应该是独立的)部分折叠在一起,并且您不会因此获得函数原型。
以下编译正常(在修复了一些其他错误之后):
with:
gcc
实际上在您发布的代码中给出了错误,所以我不确定为什么您的编译器没有。上面的命令行也很重要 - 如果您一步进行编译和链接,则需要提供所有必需的 C++ 源文件(否则链接器将无法访问所有内容)。You should be aware that you're missing a semi-colon at the end of your structure definition. This means it's folding the two (supposedly separate) parts together and that you're not getting the function prototype as a result.
The following compiles fine (after fixing a couple of other errors as well):
with:
gcc
actually gave an error with that code you posted so I'm not certain why your compiler didn't. That command line above is also important - if you're compiling and linking in one step, you need to provide all required C++ source files (otherwise the linker won't have access to everything).你的代码没问题。你只是编译错了。尝试:
Your code is fine. You're just compiling it wrong. Try:
您需要:
在您的 *main.cpp 文件中。
You need to:
in your *main.cpp file.
如果您已包含 header.hpp,则可能尚未将其 (
header.cpp
) 与 main.cpp 链接。你使用什么环境(g++或VC++)?编辑:要在 g++ 中链接,您必须编写:
此外,您的结构末尾缺少分号!
If you have included header.hpp, than probably you haven't link it(
header.cpp
) with main.cpp. What environment are you using(g++ or VC++)?Edit:for linking in g++ you must write:
Also you are missing semicolon in the end of your struct!
还应遵循以下内容:
my_thing thinger = { 123 };
thinger.blah = 123;
除了其他发帖者提到的问题外, 。请更新您的示例以便它可以编译。thinger.blah = 123;
should be along the lines of:my_thing thinger = { 123 };
in addition to issues other posters have mentioned. please, update your example so it compiles.
您在结构定义末尾缺少分号并将其与方法混合。
You are missing a semi colon at the end of your structure definition and mixing it with the method.