#### Archived

This topic is now archived and is closed to further replies.

# How to create a universal function in VC7

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have now a header file that contains a function I intend to use universally in my project. All I am supposed to do is to include this header file in my intend-to-use cpp file. But when I have two cpp files sharing this function, the compiler complains that this function has been repeatedly defined. Can anyone give me a hint how to solve this problem, or can I achieve this uniersal sharing another way around it?

##### Share on other sites
you could try using #ifndef and #endif commands ^^ i thinks in ur header.. eg

#endif
im not sure if this is how but its a start?

##### Share on other sites
You want to go

#endif

-=|Mr.Oreo|=-
Code Monkey, Serpent Engine

##### Share on other sites
The first two replys are wrong.

you want to either mark the function inline, or put it in a .cpp file and only leave the prototype in the header.

##### Share on other sites
You''ll want to do something like this:

extern int MyGlobalFunction( /*some args*/ );

#endif

and then in one Cpp file implement the function:

int MyGlobalFunction( /*some args*/ )
{
//code goes here
}

##### Share on other sites
Here is a very evil solution

universal.cpp -->

static int MyUniversalFunction (int whatever){  return 0;}

and in every file,

#include "universal.cpp"

[edited by - foofightr on August 13, 2003 4:36:42 AM]

##### Share on other sites
foofighter j00 are T3H EVIL!!

The solution to this problem (as already state) is to declare the funciton in a header file (that gets included everywhere) and define it in a cpp file (that never gets included, but still includes the header).

ToohrVyk

##### Share on other sites
You can also place the ''#pragma once'' pre-processor statement in your header before any declarations, etc...

And also, as people have stated -- function declarations in the header, function definitions in the .cpp

I used to like the #ifndef...#define method... but I find myself liking #pragma once because you don''t need to close the #ifndef at the bottom so its one less thing to worry about...

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

##### Share on other sites
Note:

#pragma once - MSVC specific syntax

Kory

##### Share on other sites
Yeah, I don''t see why people use vendor-specific extensions when there exists a portable solution.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631767
• Total Posts
3002239
×