如何在三个不同的 .cpp 文件中使用函数
我有三个 .cpp 文件,分别名为 MeshLoader.cpp、DynamicXMesh.cpp 和 StaticXMesh.cpp
我有MeshLoader.cpp 文件中名为 FindTexturePath 的函数,我想在 DynamicXMesh.cpp 中调用并使用它,并且StaticXMesh.cpp 文件。
我已在启动 XMesh 文件中包含 MeshLoader.cpp(#include "MeshLoader.cpp") 文件,当然会收到一个错误,指出函数已定义...
另外我尝试使用 pragma Once 和 ifndef ...:
//This is "MeshLoader.cpp"
pragma once
#ifndef MLOAD
#define MLOAD
char* FindTexturePath( char* TexturePath ,LPSTR FileNameToCombine){
...
...
...
}
#endif
/////
//This is StaticXMesh.cpp
#include "MeshLoader.cpp"
...
...
...
this->StatXMeshTexturePath = FindTexturePath(StatXMeshTexturePath,d3dxMaterials[i].pTextureFilename);
...
...
///// 对 DynamicXMesh.cpp 的同样调用
我希望我自己解释得足够清楚...感谢您花时间...
I have three .cpp files these are named MeshLoader.cpp, DynamicXMesh.cpp and StaticXMesh.cpp
I have a function in the MeshLoader.cpp file named FindTexturePath and I want to call and use it in the DynamicXMesh.cpp and StaticXMesh.cpp files.
I have included MeshLoader.cpp(#include "MeshLoader.cpp") file in boot XMesh files and of course get an error that says function is already defined...
Also I tryed to use pragma once and ifndef ...:
//This is "MeshLoader.cpp"
pragma once
#ifndef MLOAD
#define MLOAD
char* FindTexturePath( char* TexturePath ,LPSTR FileNameToCombine){
...
...
...
}
#endif
/////
//This is StaticXMesh.cpp
#include "MeshLoader.cpp"
...
...
...
this->StatXMeshTexturePath = FindTexturePath(StatXMeshTexturePath,d3dxMaterials[i].pTextureFilename);
...
...
/////
And same call for DynamicXMesh.cpp
I hope I explained myself clear enough... Thank you for givin your time...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要创建 MeshLoader.h,并将类似的内容放入其中,
并将其包含在其他 cpp 文件中。每个cpp文件只需要声明FindTexturePath即可编译。因此,每当您需要将 cpp 中的函数公开给其他 cpp 文件时,请创建一个包含函数声明的 .h 文件。
You need to create MeshLoader.h, and put something like this in it
And include that in your other cpp files. Each of the cpp files just need the declaration of FindTexturePath to compile. So whenever you need to make a function in a cpp public to other cpp files, create a .h file that has the function declarations in.
首选方法是将函数声明放在 .h 文件中,然后让链接器将所有 .cpp 文件合并为一个可执行文件。
如果您坚持以非标准方式执行此操作,则可以通过使函数
内联
或静态
来使您所拥有的功能正常工作。The preferred method is to put the function declaration in a .h file, and let the linker combine all the .cpp files into one executable.
If you insist on doing it in a nonstandard way, you can make what you have work by making the function
inline
orstatic
.将函数原型放入头文件 (MeshLoader.h) 中,并在需要使用该函数的任何地方包含该文件。
put the function prototype in an header file (MeshLoader.h) and include that file everywhere you need to use that function.
正如其他用户所说,您希望将声明放入头文件(
.h
或.hpp
)中。有时您可能希望在头文件中也有一个定义。此时,您进行静态函数定义:
static char* FindTexturePath(...) { .. }
As other users have stated, you want to place declarations into the header (
.h
or.hpp
) file.At times you may wish to have a definition in the header file as well. At this point, you make a static function definition:
static char* FindTexturePath(...) { .. }