3

I am a novice at C++ coding and this is my first post on Stack Overflow.

I am coding a text based game in Visual C++ and I have been getting the C2084 error in this file:

//game_system_functions.cpp
#include "basicdefines.h"

namespace
{
    using namespace std;

    void clear_console()
    {
        if (system("CLS")) system("clear");
    }
}

I find this odd, because it is only happening in This File. All the other .cpp files have no problem with functions and they are all in a similar format.

I have checked and double checked all my code, and there is no other function called clear_console. I have tried renaming the function to a bunch of random letters, and I still get the same error. Other functions in the same .cpp file get a similar error. This issue has been bothering me for the past week, and I can't solve it.

I have read other posts about error C2084 and they aren't having this problem. I would appreciate some help regarding this error.

Thank you.

P.S. I apologize about any formatting problems, as I said earlier in the post, this is my first time posting on stack overflow and sorry if the title is a little undescriptive.

Gabriel L.
  • 4,678
  • 5
  • 25
  • 34
FuNK01
  • 35
  • 1
  • 1
  • 4
  • Do you ever include this file? – chris Jan 12 '14 at 21:24
  • Probably he didn't considering it is a cpp file. – BWG Jan 12 '14 at 21:25
  • Compiles fine for me. Besides that, how does one use a function in an anonymous namespace? – Sebastian Hoffmann Jan 12 '14 at 21:25
  • Yes I have, I am intending it to hold all the string and console manipulation functions for the entire game, so almost every file in the program has it included – FuNK01 Jan 12 '14 at 21:26
  • I use an anonymous namespace in all the other .cpp files except the one with the main() function, they all work perfectly – FuNK01 Jan 12 '14 at 21:29
  • @FuNK01, Make a header with a declaration of it and include that. Don't include .cpp files. Unless I'm being dumb, you probably will want to remove the anonymous namespace, too, so the function can be linked to properly. – chris Jan 12 '14 at 21:29
  • @Paranaix Anonymous namespace: http://stackoverflow.com/questions/4514008/anonymous-namespaces-are-they-really-that-great – Brandon Jan 12 '14 at 21:30
  • @chris I just tried that and it gives me the exact same error in the header file – FuNK01 Jan 12 '14 at 21:32
  • 2
    The code you presented is fine (though useless; there are no calls to the function and there can't be any in other units due to the anonymous namespace). If you want this solved, post a [minimal complete valid example](http://stackoverflow.com/help/mcve) as per this site readme. – Jan Hudec Jan 12 '14 at 21:32
  • 1
    lol "readme" ... C:\StackOverflow\Readme.wri – Lightness Races in Orbit Jan 12 '14 at 21:49

2 Answers2

8

OP:

I am intending it to hold all the string and console manipulation functions for the entire game, so almost every file in the program has it included

And this is the problem. Its a .cpp file not a .h. It actually contains definitions of symbols. If you now include this file, its verly likely that some other files, you also include, include this file aswell.

What happens is that in the preprocessed unit code like this occur:

void clear_console()
{
    if (system("CLS")) system("clear");
}

void clear_console()
{
    if (system("CLS")) system("clear");
}

The error message makes sense now, doesnt it? To solve this you will have to use a header guard

Or better, fix your file structure: A .cpp should never be included. Instead create a header file with e.g this declaration void clear_console();. In a .cpp you then implement the function like you did already, but you only include the header (.h) file.

Also notice that this isnt possible with anonymous namespace, but they dont make any sense at all here. So just use a regular / named namespace or get rid of it.

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
  • That makes total sense, I'll try that now – FuNK01 Jan 12 '14 at 21:37
  • That works, thank you so much! As I said, I am pretty novice at C++, so that seems like an obvious mistake. I have fixed my code now, and it works. Thank you! – FuNK01 Jan 12 '14 at 21:41
2

You do have a separate .h file that corresponds to this .cpp file, don't you?

If not, and you try and #include "game_system_functions.cpp" in other files, you will have problems because your .cpp file has no header guards.

If you're including a file into others, you should have header guards something like:-

#ifndef GAMESYSTEMFUNCTIONS_H
#define GAMESYSTEMFUNCTIONS_H
...
... function declarations, etc..
...
#endif
Roddy
  • 66,617
  • 42
  • 165
  • 277
  • True include guards are good, but they won't make a difference for just function declarations. It's mainly for class definitions, but a good habit to get into nonetheless. – chris Jan 12 '14 at 21:35
  • @chris They WILL make a difference for function *definitions* which is what we have here. The linker will then have the job of deciding which definition to use, but I think they're usually capable of doing that. And that would be a separate problem. – Roddy Jan 12 '14 at 21:39
  • @Roddy, It's just that your code block has "declarations", so I was going from that. For definitions (including class definitions), it most definitely will. I didn't really notice you were discussing in relation to the .cpp file, either, so sorry for that. – chris Jan 12 '14 at 21:41
  • @Paranaix, `void foo(); void foo(); void foo();` is perfectly fine, is it not? – chris Jan 12 '14 at 21:43
  • @chris. Ah. Let me add "etc..,"! My point was that a: he needed header guards, and b: he should have function declarations rather than definitions in the header. – Roddy Jan 12 '14 at 21:43
  • @chris Yes they are, because they are DECLARATIONS and not IMPLEMENTATIONS. And though you wrote declarations, the OP has definitons here. – Sebastian Hoffmann Jan 12 '14 at 21:44
  • @chris Probably the primary purpose of header guards is to break circular and repeated includes. They're *vital* in C, which doesn't have class definitions at all. – Roddy Jan 12 '14 at 21:45
  • @Paranaix, Yes, and I said include guards don't make a difference for function declarations. With the last comment, they do make a difference for existing vs. not, though, even if it is only function declarations. – chris Jan 12 '14 at 21:45
  • @Roddy, Good point, that's certainly a good use. Thanks for reminding me of that one, and it would cause different behaviour no matter what's in there. – chris Jan 12 '14 at 21:46