[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

X does not have a type?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 45
Thread images: 5

>>
>>61254064
scope

https://stackoverflow.com/questions/20458720/how-to-access-a-global-variable-within-a-local-scope
>>
Looks like a compiler quirk. Declare x with a value.
>>
>>61254079
it's not a problem with the main function's access. the compiler doesn't even make it that far.

>>61254081
supposed I wanted to define x in another cpp file?
>>
this behavior enrages and confuses me.
>>
>>61254185
short int x
>>
File: DmXfgUA.jpg (37KB, 493x495px) Image search: [Google]
DmXfgUA.jpg
37KB, 493x495px
>using namespace std;
>declare one-letter variable in global namespace
>>
>>61254064
Why don't you just instantiate in the same line as the declare faggot.

int x = 10;
>>
>>61254272
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
>>
>>61254335
>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 >>61254272)
>>
You can't have assignment statements in global scope you fucking retard.
>>
>>61254064
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.
>>
File: retard.png (203KB, 2560x1440px) Image search: [Google]
retard.png
203KB, 2560x1440px
>>61254491

uhh, what is this retard.
>>
>>61254519
Not him, but this is initialization. Think about how variables are stored in the binary file, everything will make sense.
>>
>>61254519
that's a declaration with initial value retard
>>
>>61254335
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?
>>
>>61254590
He means initialization
>>
>>61254552
>>61254590

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.

>>61254618
If I want to use a variable across multiple cpp files, wouldn't it have to be defined globally?
>>
>>61254654
With extern
>>
>>61254654
>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++.
>>
>>61254654
>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.
>>
>>61254654
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.
>>
>>61254126
in the header file
 extern int x; 

and in a source file
 int x = 10; 
>>
>>61254079
C++ is the most needlessly complicated language.
>>
>>61255611
Go back to python faggot
>>
>>61255302
>declaring twice
>>
>>61255611
>needlessly

Try gameplay programming
>>
>>61254064
If you wanna run code in the global scope you need to use something like HolyC.
>>
>>61254064
You forgot to #include windows.h
>>
#include<iostream>
using namespace std;

void main
{
int x;
x=10;
cout<<x<<endl;
} //you may need return 0 or system("pause") if the app is not executed
>>
Typical atom user retardation
>>
>>61254064
>>61254185
>>61254519
>Actually, unironically, genuinely using C++
Dude wtf!
>>
>>61254654
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.
>>
>>61255988
>app
kys
>>
File: 1497881942388.gif (2MB, 500x374px) Image search: [Google]
1497881942388.gif
2MB, 500x374px
>using the "using" and "namespace" keywords
>not directly controlling scope resolution using the namespace scope resolution operator
>>
>>61256702
>writing redundant shit
>>
>>61256719
>ending up like OP
>not defining concise namespaces

Sorry I'm just a nonfaggot tending my nonshit code, don't mind me
>>
>>61254267
>>61256702
>>61256768
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.
>>
>>61254064
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.
>>
>>61254064
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
>>
>>61257352
But using 'using namespace' is bad and you should be made to feel bad
>>
>>61254064
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.
>>
>>61255779
Those are 2 different declarations, anon.
>>
>>61258549
nvm I just scrolled up and realized what you were doing
Thread posts: 45
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.