Why is my global variable causing problems? - c++

I've been reading about global variables for an hour now and I can't get mine to work. I have two vectors of custom classes, Airport and Flight, which are defined in their respective header files. Then I declare the extern vectors in the Globals.h header which is included in main.cpp. If I leave the code as is, I get a "unresolved external symbol" error for both of the vectors:
error LNK2001: unresolved external symbol "class std::vector<class Airport,class std::allocator<class Airport> > airports" (?airports##3V?$vector#VAirport##V?$allocator#VAirport###std###std##A)
error LNK2001: unresolved external symbol "class std::vector<class Flight,class std::allocator<class Flight> > flights" (?flights##3V?$vector#VFlight##V?$allocator#VFlight###std###std##A)
If, instead, I forward declare (like I've been lead to believe is how it should be done) by putting
vector<Airport> airports;
vector<Flight> flights;
in front of main(), then I get these errors:
error C2371: 'airports' : redefinition; different basic types
error C2371: 'flights' : redefinition; different basic types
There's also errors given about allocators in vectors, and how one vector is not equal to the other vector. However, I cannot create a vector by typing vector> airport, which is what is suggested. This is mirrored by vector.
The code in my project is below. Any help would be appreciated.
main.cpp (simplified):
#include "stdafx.h"
#include "Globals.h"
using namespace std;
int main(){
airports = readInAirports();//returns a vector<Airport>
flights = readInFlights();//returns a vector<Flight>
}
Airport.h: (Flight.h is similar)
#ifndef AIRPORT_H
#define AIRPORT_H
#include <vector>
#include <string>
using namespace std;
class Airport{
public:
Airport(string c, string n, int dt, int cc)
:code(c), name(n),
departureTax(dt),connectionTime(cc)
{}
string toLine();
string getCode();
string getName();
int getDepTax();
int getConnTime();
private:
string code, name;
int departureTax, connectionTime;
};
#endif
Globals.h:
#ifndef GLOBALS_H
#define GLOBALS_H
#include <vector>
#include <string>
#include <math.h>
#include "Airport.h"
#include "Flight.h"
extern vector<Airport> airports;
extern vector<Flight> flights;
#endif

extern vector<Airport> airports;
This is a declaration of airports. It says "Somewhere in the program is a global variable called airports of type vector<Airport>".
vector<Airport> airports;
This is a definition of airports. It creates a global variable called airports of type vector<Airport>. You cannot have two global variables with the same name, even if they are in different files.
You should know that #include effectively copy-pastes the whole contents of the included file into this file. So if you put a definition in a header file, and include that header file in two different source files, then you have two definitions.
The solution is to put the declarations in the header file (with extern) and the definitions in one source file.

Because you have't defined them. You only declared them (extern only declares).
You need to add the definitions for both airports and flights in a .cpp file, like so:
vector<Airport> airports;
vector<Flight> flights;
Furthermore, vector<Airport> airports; is not a forward declaration, it's a definition. To forward declare variables you need to use the extern keyword.

Related

C++ global namespace members already defined error

I'm trying to make a namespace and its members available globally however I am running into already defined errors.
Settings.h
#pragma once
#include "boost/property_tree/json_parser.hpp"
#include <string>
using json = boost::property_tree::ptree;
namespace Settings {
extern std::string settingsPath;
extern json settings;
extern void init();
extern void readSettings();
extern void writeSettings();
};
Settings.cpp
#pragma once
#include "Settings.h"
using json = boost::property_tree::ptree;
namespace Settings {
void init() {
}
void readSettings() {
}
void writeSettings() {
}
};
I am forward declaring the Settings namespace and members and using extern. I have no idea what I'm doing wrong. Please could someone point out the error here.
Thanks
Edit: The actual error messages:
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > Settings::settingsPath" (?settingsPath#Settings##3V?$
basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A) already defined in
AmalgamService.obj Amalgam F:\Dropbox\CPP\Visual Studio Projects\Amalgam\Amalgam\
main.obj 1
And repeat for all members of the namespace. The Settings.h is #includeed in main.cpp and AmalgamService.cpp
You seem to be including Settings.cpp in another file. Do not include .cpp files. This results in duplicate definitions. This also means that #pragma once in .cpp files is useless.
You need to keep in mind that #include is just a glorified copy&paste tool. When you #include a file, that file is literally being copy&pasted into the spot of the #include statement. So including a .cpp file means you will get multiple definitions of everything defined in that .cpp file.
Furthermore, you don't have to use extern when declaring functions. Functions are extern by default, unless you say otherwise.
Variables are extern by default too, however you need to use extern as a way to declare them without defining them:
extern int var; // declaration
int var; // definition
Functions don't need that, because you can declare them by omitting their body:
void func(); // declaration
void func() { } // definition
You are allowed to declare things multiple times, which is why you can #include header files (like .h, .hpp) in multiple files. But you are not allowed to define things multiple times, which is why you can't #include non-header source files.

creating global variables causes linker error

I have an MFC application AVT_testapp, and in the header file (AVT_testappDlg.h) I am trying to create a variable outside of all functions, classes, etc. in order to make it global. Whenever I try to do this though (say I try int x = 7), I get the error:
1>AVT_testappDlg.obj : error LNK2005: "int x" (?x##3HA) already defined in
AVT_testapp.obj
1>..\..\bin\x64\Debug\AVT_testapp.exe : fatal error LNK1169: one or more
multiply defined symbols found
Everything I have found on google says "just add header guards". AVT_testappDlg has 6 #include's, and each of them has header guards.
What else could be causing these errors when creating global variables?
EDIT: Here is the beginning of my header file,
#pragma once
#include "../../src/CoreUtils/nierr.h"
#include "..\..\src\CoreUtils\StringHelpers.h" //includes windows.h
#include "afxwin.h"
#include "afxcmn.h"
#include "IFrameObserver.h"
#include "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\GdiPlusHeaders.h"
//#include <fstream>
//#include <windows.h>
int x = 7;
using namespace AVT::VmbAPI;
//////////////////////////////////////////////////////////////////////////
////////// MyObserver class ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class MyObserver : public IFrameObserver
{
private:
MyObserver( MyObserver& );
MyObserver& operator=( const MyObserver& );
public:
VmbUchar_t* imageData;
//...
//...
//...
//...
//that's the end of the relevant stuff
You cannot define variables at namespace level in a header. In general it is best not to have global variables, but if you need to you should provide only a declaration in the header and the definition in a single .cpp:
//header
extern int i;
//cpp
int i;
The problem with your code is not related to header guards. Header guards ensure that a header is parsed only once in each translation unit. Lack of header guards causes compiler errors, where the compiler sees, say for example a class, defined multiple times in the same translation unit after preprocessing. In your case the error is a linker error LNK2005, and it means that the same symbol was defined in multiple translation units (in your case in each translation unit that includes the header with the definition).
If the global variable is not const(*), you cannot put it in a header file and include it in multiple translation units (i.e. .cpp files). Otherwise, you will end up with multiple definitions of the same symbol in your program, violating the ODR (One Definition Rule, see Paragraph 3.2 of the C++11 Standard), and the linker will complain about that.
You should use the extern modifier in your shared header to provide only a declaration of your variable:
extern int var;
Then, in one single .cpp file, you can provide a definition for it:
int var;
(*) const global variables have internal linkage by default, so each translation unit will end up having a private copy of it and no multiple definition will occur.
if you insist on having a global variable at least put it in a namespace to avoid collisions with other modules
namespace globals
{
extern int x;
}
then in the .cpp file define it.
int globals::x = 0;
it also makes it more clear that it is a global variable.

c++ link error, cannot find global.h struct

I've defined a struct in a header file global.h, that i try to use it in a another class, but i get this error : Error 6 error LNK2001: unresolved external symbol "struct tag_KG_Data g_GlobalVar" (?g_GlobalVar##3Utag_KG_Data##A) KGComThread.obj
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <stdio.h>
typedef struct tag_KG_Data
{
int nKGStationID;
int nKGComPort;
}GLOBAL_VAR;
#endif
and in KGComThread.cpp file i use it like this:
#include "global.h"
extern GLOBAL_VAR g_GlobalVar;
I think the compiler can't find the global.h file so it defines a meaningless tag_KG_Data struct, but i can't understand why.
This
extern GLOBAL_VAR g_GlobalVar;
is only a declaration. The variable is not yet defined:
GLOBAL_VAR g_GlobalVar;
You need the previous line in a single implementation file.
Also, since this is C++, you don't need a tag for the struct, you can just write
struct GLOBAL_VAR
{
int nKGStationID;
int nKGComPort;
};

C++ VS2010 Linker error regarding namespace variables

MyNamespace.h:
namespace MyNamespace{
int a
}
MyNamespace.cpp: some function that uses a
main.cpp
#include "MyNamespace.h"
main.obj : error LNK2005: "class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >
FileNamespace::m_rootDirectoryPath"
(?m_rootDirectoryPath#FileNamespace##3V?$basic_string#_WU?$char_traits#_W#std##V?$allocator#_W#2##std##A)
already defined in FileNamespace.obj
1>main.obj : error LNK2005: "struct FileNamespace::FileTree FileNamespace::m_dataFileTree"
(?m_dataFileTree#FileNamespace##3UFileTree#1#A) already defined in
FileNamespace.obj
You are defining a global variable (with external linkage) in multiple translation units, which results in duplicate definition errors (since you are violating the ODR).
What you should do, instead, is to declare it in the header with an extern declaration
namespace MyNamespace{
extern int a;
}
and define it in a single .cpp file (probably in MyNamespace.cpp)
int MyNamespace::a;
This way, the compiler will create only one instance of this variable in a single object module, and the linker will link all the references to it made in other object modules to this single instance.
It may help you to understand the problem noticing that this is the exact equivalent of declaring functions in headers (writing there only the prototype) and defining them in a single .cpp.
Namespaces are no different from other variables; in your header, you are actually defining the "a", not just declaring it.
In your header:
namespace MyNamespace{
extern int a;
}
In one source file:
int MyNamespace::a;

Pesky Link Errors

I am using visual studio 2010 and believe I have a project settings issue. I have a header file that has some declarations in it:
definitions.h
#include <string>
struct myStruct
{
std::string x[4];
std::string y[8];
};
void InitializeStructData();
extern myStruct data[12];
and the cpp file initializes my structure:
definitions.cpp
#include "definitions.h"
#include <string>
mySturct data[12];
void InitializeStructData()
{
data[0].x[0] = "a";
data[0].x[1] = "b";
....
data[0].y[0] = "a";
....
....
data[11].y[7] = "done initializing"';
}
and I have a form that has some buttons and things whose text I populate from the arrays depending on different circumstances:
myForm.cpp
#include "definitions.h"
...
//form initialization
As soon as I have two #include "definitions.h" statements I get link errors:
Error 1 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * Definitions"
Error 2 error LNK1169: one or more multiply defined symbols found
Your question is missing the important part.
You have a std::string* Definitions in a header, that you forgot to use extern with.
Do you have your code (.h file) inside:
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
#endif
to help prevent you from defining it multiple times, if you have it included in multiple places?