Jump to content

  • Log In with Google      Sign In   
  • Create Account


#pragma once not working?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 lordimmortal2   Members   -  Reputation: 127

Like
0Likes
Like

Posted 26 January 2012 - 01:39 AM

I'm sure it's something really silly but it appears that neither #pragma once nor #ifndef (and the accompanying commands) does not make the file only get defined once with object instances.

Case in point:
#pragma once

class LogicContainer
{
private:
public:
	LogicContainer();
	~LogicContainer();

	void Logic();
	void Loop();
	void GenerateTiles();
	void CleanGame();

	int GetRandNum();
};

LogicContainer logic;

Generates:

1>Main.obj : error LNK2005: "class LogicContainer logic" (?logic@@3VLogicContainer@@A) already defined in LogicContainer.obj


This file is included in two different files (LogicContainer.cpp for class function defining and in main for class function calling). Shouldn't the "#pragma once" stop "LogicContainer logic;" from being defined twice? I would like for this object to be defined within this file if possible.
Prove me wrong so I can know what's right.

Sponsor:

#2 bradbobak   Members   -  Reputation: 1017

Like
4Likes
Like

Posted 26 January 2012 - 01:44 AM

header guards (or pragma once) just make sure that the header file is read one time per tu (translation unit). so if a.cpp includes it, and b.cpp includes it, and you compile a.cpp and b.cpp separately, there will be 2 instances of 'LogicContainer logic'. Simplest solution is to put 'extern LogicContainer logic' in the header file, and define 'LogicContainer logic' in some .cpp that will be linked in.

#3 Marvel Magnum   Members   -  Reputation: 324

Like
0Likes
Like

Posted 26 January 2012 - 12:40 PM

Heh! I remember facing this same issue when I started out with multiple file projects. Posted Image

This file is included in two different files (LogicContainer.cpp for class function defining and in main for class function calling). Shouldn't the "#pragma once" stop "LogicContainer logic;" from being defined twice? I would like for this object to be defined within this file if possible.


And I thought exactly the same.

#4 lordimmortal2   Members   -  Reputation: 127

Like
0Likes
Like

Posted 26 January 2012 - 04:24 PM

header guards (or pragma once) just make sure that the header file is read one time per tu (translation unit). so if a.cpp includes it, and b.cpp includes it, and you compile a.cpp and b.cpp separately, there will be 2 instances of 'LogicContainer logic'. Simplest solution is to put 'extern LogicContainer logic' in the header file, and define 'LogicContainer logic' in some .cpp that will be linked in.


Thanks a lot, that worked. Really should've knew that in the first place but I guess that's what I get for being away from C++ for so long =/. Thanks again! =)
Prove me wrong so I can know what's right.

#5 rip-off   Moderators   -  Reputation: 7651

Like
1Likes
Like

Posted 26 January 2012 - 05:32 PM

To expand on this, for future searchers:

Compilation of C++ is conceptually a multi-stage process. During the preprocessing step header files are literally copied and pasted into the source files to form translation units. The compiler compiles each translation unit independently into an object file. The linker takes the object files, and any libraries, to create an executable.

Note that header guards only exist and work in the first stage. Thus they cannot prevent linker errors.

This is also the reason why the linker is unhelpful with its error message, it only sees two variables with the same name in different object files. It doesn't know they came from a single location in the original source.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS