0

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

3
  • 1
    I would recommend you reading a book about C++. Any will do. Commented Jun 14, 2011 at 22:32
  • 1
    You got some good answers but I feel like it's always important to know why as well. Check out this overview of compiling and linking. It might be a bit obtuse at first but it really is useful for C programming. Commented Jun 14, 2011 at 22:38
  • Thank you for that documentation it realy looks like usefull for me I'll read it for sure. Commented Jun 15, 2011 at 7:52

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.