X does not have a type?

...

Other urls found in this thread:

stackoverflow.com/questions/20458720/how-to-access-a-global-variable-within-a-local-scope
twitter.com/SFWRedditVideos

scope

stackoverflow.com/questions/20458720/how-to-access-a-global-variable-within-a-local-scope

Looks like a compiler quirk. Declare x with a value.

it's not a problem with the main function's access. the compiler doesn't even make it that far.

supposed I wanted to define x in another cpp file?

this behavior enrages and confuses me.

short int x

>using namespace std;
>declare one-letter variable in global namespace

Why don't you just instantiate in the same line as the declare faggot.

int x = 10;

it's not about making it work.. it's about understanding why it doesn't work.

I'm pretty sure you need to use extern int x; when dealing with global scope

>it's about understanding why it doesn't work.
I googled around and there wasn't an answer to "'why", just always "you cannot assign a value to a variable in the global scope / outside of a function, but you can assign a value when declaring the variable" (like )

You can't have assignment statements in global scope you fucking retard.

Think about what are you doing as if you were the compiler. It's a good thing that variables at global scope can't be assigned a value.

uhh, what is this retard.

Not him, but this is initialization. Think about how variables are stored in the binary file, everything will make sense.

that's a declaration with initial value retard

Why would you think it'd work? What utility could there possibly be in being able to assign globally? If there's no reason for a functionality to exist why the fuck would you expect it to?

He means initialization

I guess I'm confused about the difference between initialization, definition, assignment, and declaration.

What is the difference between a declaration and assignment vs a declaration with an initialization? They seem like the same thing to me.

If I want to use a variable across multiple cpp files, wouldn't it have to be defined globally?

With extern

>If I want to use a variable across multiple cpp files, wouldn't it have to be defined globally?
You'd use a header file that describes the variable with the extern keyword I guess, it's been too long since I fucked with C++.

>What is the difference between a declaration and assignment vs a declaration with an initialization? They seem like the same thing to me.
You'll understand after you write a copy ctor and copy assigmment operator.

The difference you need to learn first is between statements and expressions.

Expressions like x=10 are evaluated at runtime and makes sense only during program execution. The reason you can write "int i=10" in the global scope is because the expression is attached to a global variable.

in the header file extern int x;
and in a source file int x = 10;

C++ is the most needlessly complicated language.

Go back to python faggot

>declaring twice

>needlessly

Try gameplay programming

If you wanna run code in the global scope you need to use something like HolyC.

You forgot to #include windows.h

#include
using namespace std;

void main
{
int x;
x=10;
cout

Typical atom user retardation

>Actually, unironically, genuinely using C++
Dude wtf!

Global/static (global (i.e. non-local) scope) non-function declarations must have the extern keyword, otherwise the variable is implicitly initialized to 0 (i.e. NULL for pointers). Obviously you cannot define it twice, and you cannot assign outside of a function. So I think by using extern, the line with the equal sign and such will act as *the* definition. At least I think it works like this in C, so probably in C++ as well.

>app
kys

>using the "using" and "namespace" keywords
>not directly controlling scope resolution using the namespace scope resolution operator

>writing redundant shit

>ending up like OP
>not defining concise namespaces

Sorry I'm just a nonfaggot tending my nonshit code, don't mind me

This is obviously not some big software project. That shit is completely irrelevant. Stop being edgy just because you've heard once that 'using namespace' is bad.

int x;

at this point x == 0
x = 10;

at this point you try to assign 10 to x

however

the standard says that order of initializtion of variables in global scope (or side effects in c++) is unspecified.
so what value should the variable have when you enter main()? 0 or 10? it's literally unspecified, so it could have either. that's why the standard forbids arbitrary assignment of global variables at global scope.

read the fucking manual next time.

int x = 10; is a self-contained declaration with an initial value
x=10; is arbitary code that you can't run outside a function

But using 'using namespace' is bad and you should be made to feel bad

Compiler programmer oversight, since everyone assumes you do global variable assignment during declaration, not on the line after.
You probably could fix the compiler, but in this particular case you'd have to wonder what the point of doing that would be, and whether "fixing" it could lead to other problems down the line.

Those are 2 different declarations, user.

nvm I just scrolled up and realized what you were doing