C++ access global integer from class header file - c++

I have a header file that declares a class, and I want this class to access an integer that has been declared in the main cpp C++ file (i.e. another file than the class' one). I have been searching on Google and found nothing relevant. How can I do this?

To share a global variable among source files, use the extern keyword.
main.cpp
#include "foo.h"
int global_var=0;
int main()
{
foo();
return 0;
}
foo.h
#ifndef FOO_H
#define FOO_H
extern int global_var;
void foo();
#endif
foo.cpp
#include "foo.h"
int foo()
{
global_var=1;
}

Move the integer in main.cpp into a function, make it static there, have the function return a reference to it and put the function into a header file of its own which the class header file (or implementation file, if appropriate) includes.
integer.h:
#ifndef INTEGER_H // use some better, longer name here
#define INTEGER_H
int &Integer();
#endif
integer.cpp:
#include "integer.h"
int &Integer()
{
static int i = 0;
return i;
}
Access the integer like this:
int x = Integer(); // copy
Integer() = 123; // assign

Related

multiple definition of `grid' __ first defined here C++ [duplicate]

I have two source files that need to access a common variable. What is the best way to do this? e.g.:
source1.cpp:
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
source2.cpp:
int function()
{
if(global==42)
return 42;
return 0;
}
Should the declaration of the variable global be static, extern, or should it be in a header file included by both files, etc?
The global variable should be declared extern in a header file included by both source files, and then defined in only one of those source files:
common.h
extern int global;
source1.cpp
#include "common.h"
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
source2.cpp
#include "common.h"
int function()
{
if(global==42)
return 42;
return 0;
}
You add a "header file", that describes the interface to module source1.cpp:
source1.h
#ifndef SOURCE1_H_
#define SOURCE1_H_
extern int global;
#endif
source2.h
#ifndef SOURCE2_H_
#define SOURCE2_H_
int function();
#endif
and add an #include statement in each file, that uses this variable, and (important) that defines the variable.
source1.cpp
#include "source1.h"
#include "source2.h"
int global;
int main()
{
global=42;
function();
return 0;
}
source2.cpp
#include "source1.h"
#include "source2.h"
int function()
{
if(global==42)
return 42;
return 0;
}
While it is not necessary, I suggest the name source1.h for the file to show that it describes the public interface to the module source1.cpp. In the same way source2.h describes what is public available in source2.cpp.
In one file you declare it as in source1.cpp, in the second you declare it as
extern int global;
Of course you really don't want to be doing this and should probably post a question about what you are trying to achieve so people here can give you other ways of achieving it.

Defining member variable in header without class definition

I've got two files, list.cpp and Header.h. Segments of the files are below. I know that if the header file is for a class, it is setup different. E.g.
class MyClass
{
public:
void foo();
int bar;
};
However, since I'm not really working with a class here (correct me if I'm wrong), am I not able to declare things under public: and private like below?
Also, if I were to place the global variable rescan in the header file as a member variable, below the function definitions, only the main function can see the variable. Why is it not within the scope of the other functions?
list.cpp:
#include <boost/algorithm/string.hpp>
#include <vector>
using namespace std;
vector<int> results;
bool rescan;
int main()
{
vector<vector<string>> list;
int success = readFile(list);
vector<vector<string>> bad = findMe(list);
system("pause");
return 0;
}
vector<vector<string>> findMe(vector<vector<string>> find)
{
rescan = true;
}
Header.h:
#pragma once
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <string>
#include <vector>
std::vector<std::vector<std::string>> findMe(std::vector<std::vector<std::string>>);
#endif
EDIT: I tried this in my header file:
public:
bool rescan;
But I got "syntax error: 'public'
If you want your global to be visible in other translation units (TU) (other files), you have to declare them extern in those other TUs:
Header.h:
// Include guard omitted
extern bool rescan; // Declaration
file.cpp
#include "Header.h"
bool rescan = false; // Definition
// ...
file2.cpp
#include "Header.h" // To see extern bool rescan;
void foo()
{
rescan = true;
}
// ...

redefinition error in a 2d vector [duplicate]

I have two source files that need to access a common variable. What is the best way to do this? e.g.:
source1.cpp:
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
source2.cpp:
int function()
{
if(global==42)
return 42;
return 0;
}
Should the declaration of the variable global be static, extern, or should it be in a header file included by both files, etc?
The global variable should be declared extern in a header file included by both source files, and then defined in only one of those source files:
common.h
extern int global;
source1.cpp
#include "common.h"
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
source2.cpp
#include "common.h"
int function()
{
if(global==42)
return 42;
return 0;
}
You add a "header file", that describes the interface to module source1.cpp:
source1.h
#ifndef SOURCE1_H_
#define SOURCE1_H_
extern int global;
#endif
source2.h
#ifndef SOURCE2_H_
#define SOURCE2_H_
int function();
#endif
and add an #include statement in each file, that uses this variable, and (important) that defines the variable.
source1.cpp
#include "source1.h"
#include "source2.h"
int global;
int main()
{
global=42;
function();
return 0;
}
source2.cpp
#include "source1.h"
#include "source2.h"
int function()
{
if(global==42)
return 42;
return 0;
}
While it is not necessary, I suggest the name source1.h for the file to show that it describes the public interface to the module source1.cpp. In the same way source2.h describes what is public available in source2.cpp.
In one file you declare it as in source1.cpp, in the second you declare it as
extern int global;
Of course you really don't want to be doing this and should probably post a question about what you are trying to achieve so people here can give you other ways of achieving it.

how to make global string declaration [duplicate]

I have two source files that need to access a common variable. What is the best way to do this? e.g.:
source1.cpp:
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
source2.cpp:
int function()
{
if(global==42)
return 42;
return 0;
}
Should the declaration of the variable global be static, extern, or should it be in a header file included by both files, etc?
The global variable should be declared extern in a header file included by both source files, and then defined in only one of those source files:
common.h
extern int global;
source1.cpp
#include "common.h"
int global;
int function();
int main()
{
global=42;
function();
return 0;
}
source2.cpp
#include "common.h"
int function()
{
if(global==42)
return 42;
return 0;
}
You add a "header file", that describes the interface to module source1.cpp:
source1.h
#ifndef SOURCE1_H_
#define SOURCE1_H_
extern int global;
#endif
source2.h
#ifndef SOURCE2_H_
#define SOURCE2_H_
int function();
#endif
and add an #include statement in each file, that uses this variable, and (important) that defines the variable.
source1.cpp
#include "source1.h"
#include "source2.h"
int global;
int main()
{
global=42;
function();
return 0;
}
source2.cpp
#include "source1.h"
#include "source2.h"
int function()
{
if(global==42)
return 42;
return 0;
}
While it is not necessary, I suggest the name source1.h for the file to show that it describes the public interface to the module source1.cpp. In the same way source2.h describes what is public available in source2.cpp.
In one file you declare it as in source1.cpp, in the second you declare it as
extern int global;
Of course you really don't want to be doing this and should probably post a question about what you are trying to achieve so people here can give you other ways of achieving it.

Static variable link error, C++

Consider this code.
//header.h
int x;
//otherSource.cpp
#include "header.h"
//main.cpp
#include "header.h"
...
int main()
{
}
In this case compiler erred with the message. "fatal error LNK1169: one or more multiply defined symbols found"
but when I add static before x, it compiles without errors.
And here is the second case.
//header.h
class A
{
public:
void f(){}
static int a;
};
int A::a = 0;
/otherSource.cpp
#include "header.h"
//main.cpp
#include "header.h"
...
int main()
{
}
In this case compiler again erred with multiple declaration.
Can anybody explain me the behavior we static variables in classes and in global declarations?? Thanks in advance.
The issue with the static member variable is that you have the definition occur in the header file. If you #include the file in multiple source files, you have multiple definitions of the static member variable.
To fix this, the header file should consist only of this:
#ifndef HEADER_H
#define HEADER_H
// In the header file
class A
{
public:
void f(){}
static int a;
};
#endif
The definition of the static variable a should be in one and only one module. The obvious place for this is in your main.cpp.
#include "header.h"
int A::a = 0; // defined here
int main()
{
}
Declare x as extern in header.h to tell the compiler that x will be defined somewhere else:
extern int x;
Then define x once in the source file which you think is most fitting.
For example in otherSource.cpp:
int x = some_initial_value;