Global variable and extern [duplicate] - c++

This question already has answers here:
Why must c++ statements be contained within functions?
(3 answers)
Code outside functions
(3 answers)
Can C++ have code in the global scope?
(1 answer)
Closed 1 year ago.
I have this three files
header.h
extern int global_var;
header.cpp
#include "header.h"
int global_var=50;
main.cpp
#include<iostream>
#include "header.h"
using namespace std;
global_var+=100;
int main(){
cout<<global_var;
}
when I run the prog error occurred :
global_var does not name a type
I know when I put global var inside main function work
but I want to know why this problem occurred

Related

how can I use a function to print the name of args not use define In c++ [duplicate]

This question already has answers here:
generic way to print out variable name in c++
(6 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
#define debug(x) cout<<#x<<": "<<(x)<<endl;
int main() {
int a = 0;
debug(a)
return 0;
}
the output is: a:0
I want to print the name of variable like a
can I do it by a function not use define
i think the answers here can help you :
generic way to print out variable name in c++
and it seems the only way is using macro!

What's the meaning of ::std::vector [duplicate]

This question already has answers here:
What is the meaning of prepended double colon "::"?
(9 answers)
C++ : what is :: for?
(5 answers)
Closed 5 years ago.
I just saw in some DLib code, in a .cpp file a thing like that:
#include <stdio>
namespace {
using namespace std;
void foo() {
::std::vector<int> my_vector;
}
}
Is there a reason we specify ::std:: in front of our vector we imported from std already ?

Undefined reference to sample void function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.
There is main:
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
afis();
return 0;
}
There is an function named function.cpp:
#include <iostream>
using namespace std;
void afis(){
cout <<"yehe";
}
And there is the header :
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void afis();
#endif // FUNCTIONS_H_INCLUDED
While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.
Instead of calling
CXX/clang++/g++ main.cpp -o hello
you need to manually include all relevant code files:
CXX/clang++/g++ main.cpp functions.cpp -o hello

Fatal Error in c++ while using local variables [duplicate]

This question already has answers here:
Where to get iostream.h
(3 answers)
Closed 7 years ago.
As am learning cpp tutorial
#include <iostream.h>
using namespace std;
int main()
{
//variable declaration
int a,b;
int c;
//actual initialization
a=10;b=20;
c=a+b;
cout<<c;
return 0;
}
my error
fatal error: iostream.h
Just change <iostream.h> to <iostream>
Reason is that .h header extensions were used for C includes but aren't used for C++ anymore.
In fact, you can actually use C libraries with .h it's just there isn't one for iostream since its C++ exclusive, hence the fatal error.

C++ file including C header file [duplicate]

This question already has answers here:
error: 'INT32_MAX' was not declared in this scope
(10 answers)
Closed 8 years ago.
I need to include a C header file in my C++ project but g++ throws "not declared in this scope" errors.
I read that i need to use extern "C" keyword to fix it but it didn't seem to work for me.
Here is a dummy example triggering this error.
main.cpp:
#include <iostream>
extern "C"
{
#include "includedFile.h"
}
int main()
{
int a = 2;
int b = 1212;
std::cout<< "Hello World!\n";
return 0;
}
includedFile.h
#include <stdint.h>
enum TypeOfEnum {
ONE,
TWO,
THREE,
FOUR = INT32_MAX,
};
The error thrown is :
$> g++ main.cpp
In file included from main.cpp:4:0:
includedFile.h:7:9: error: ‘INT32_MAX’ was not declared in this scope
FOUR = INT32_MAX,
I saw on this post that I may need #define __STDC_LIMIT_MACROS without any success.
Any help is welcome!
<cstdint> is a C++11 feature. Pass -std=c++11 to your compiler.