开发者

How to use a function in three different .cpp files

开发者 https://www.devze.com 2023-03-13 05:03 出处:网络
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 D

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 inclu开发者_运维技巧ded 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...


You need to create MeshLoader.h, and put something like this in it

#ifndef INCLUDED_MESH_LOADER_H
#define INCLUDED_MESH_LOADER_H

char* FindTexturePath( char* TexturePath ,LPSTR FileNameToCombine);

#endif

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.


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 or static.


put the function prototype in an header file (MeshLoader.h) and include that file everywhere you need to use that function.


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(...) { .. }

0

精彩评论

暂无评论...
验证码 换一张
取 消