Jump to content
  • Advertisement

Archived

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

00702000744

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
You want to go

#ifndef __MY_HEADER_FILE
#define __MY_HEADER_FILE
//Header stuff goes here
#endif

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

Share this post


Link to post
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 this post


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

in your header:

#ifndef _MY_GLOBAL_HEADER_
#define _MY_GLOBAL_HEADER_

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

#endif

and then in one Cpp file implement the function:

#include "my_global_header.h"

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!