Skip to main content
gamedev.net
🔒 Locked

error C2084

Started by Zong 8 replies 4k views
Original Post
Zong
Zong
Hi! Maybe this question should be in the beginners forum but I wasn''t really sure. It probably also been asked and answered many times but the only post I found didn''t help me vrey much Anyhow. I have one file called Test.h which only has void Test(void) {} in it. And then three files main.cpp, functions.cpp, stuff.cpp (names I just came up with right now) These files have #include "Test.h" Now I get three errors (all the same) error C2084: function ''void __cdecl Test(void)'' already has a body so the question if very simple, how do I do to compile? (in a term where I might have a great deal of functions in Test.h) thanks!
Melraidin
Melraidin
Put include guards around test.h:


  
#ifndef TEST.H
#define TEST.H

void Test(void) {}

#endif


This will ensure that the compiler only processes the definition of Test once, no matter how many times it''s included.
Zong
Zong
Thanks for the fast reply!

I tried that but now I get the error
error LNK2005: "void __cdecl Test(void)" (?Test@@YAXXZ) already defined in main.obj

Should I close down visual studio and just play some game instead?
doctorsixstring
doctorsixstring
I believe that you can only define the function your "test.h" file. You must write the code for the function in another file ("test.cpp"?). Otherwise, the compiler will try to add the same function to multiple .obj files, and will not know which object file it should get the function from.
Cyberdrek
Cyberdrek
quote:
Original post by Zong
Thanks for the fast reply!

I tried that but now I get the error
error LNK2005: "void __cdecl Test(void)" (?Test@@YAXXZ) already defined in main.obj

Should I close down visual studio and just play some game instead?


Of course it didn't solve your problem, that wasn't the error... Instead of having void test( void ){}, put void test(void); <- Notice the ";" at the end, when you write declarations, you put ";" at the end, not {}. This will solve your problem for sure... Although, make sure you define the body of test in one of your .cpp files. One last thing, even if the include guards weren't your real problem, leave them in your .h file, they will make sure you don't get too many headaches later on...





"And that's the bottom line cause I said so!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

[edited by - cyberdrek on May 28, 2002 11:57:26 AM]
[Cyberdrek | ]
Zong
Zong
Now I finally understand how it should be done!

thanks!
JonStelly
JonStelly
Assuming you''re on VC 6.0 or VS.NET you can also use the "#pragma once" directive.
Neodraco
Neodraco
quote:
Original post by Zong

Should I close down visual studio and just play some game instead?


Yeah, you can do this, or maybe take a look at:

http://www.gamedev.net/reference/programming/features/orgfiles/

Whatever makes you entertain more...
Zong
Zong
Great link thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.