0

UPDATE:
Thanks to everyone for the help to understand this!

I try to run this:

#include <iostream>

int* x = new int;
*x = 5;

int main()
{

}


and i get the following errors:

1>------ Build started: Project: learnCpp, Configuration: Debug Win32 ------
1>learnCpp.cpp
1>C:\Users\Danie\source\repos\learnCpp\learnCpp.cpp(4,6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\Danie\source\repos\learnCpp\learnCpp.cpp(4,2): error C2374: 'x': redefinition; multiple initialization
1>C:\Users\Danie\source\repos\learnCpp\learnCpp.cpp(3): message : see declaration of 'x'
1>C:\Users\Danie\source\repos\learnCpp\learnCpp.cpp(4,7): error C2440: 'initializing': cannot convert from 'int' to 'int *'
1>C:\Users\Danie\source\repos\learnCpp\learnCpp.cpp(4,4): message : Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>Done building project "learnCpp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


however I don't get any errors if I assign the value to x inside the main function.
Like so:

#include <iostream>

int* x = new int;


int main()
{
    *x = 5;
}

How come?

dannen
  • 1
  • 1
  • because you can only have non-declaration statements inside functions: [Is it possible to use if statement outside a function (like main)?](https://stackoverflow.com/q/38536076/995714), [C++ using “cout” outside of main function?](https://stackoverflow.com/q/47231899/995714), [Running C++ code outside of functions scope](https://stackoverflow.com/q/25864206/995714), [Code outside functions](https://stackoverflow.com/q/11313450/995714) – phuclv Nov 23 '19 at 11:21
  • Does this answer your question? [Why statements cannot appear at namespace scope?](https://stackoverflow.com/questions/8470065/why-statements-cannot-appear-at-namespace-scope) – phuclv Nov 23 '19 at 11:21

3 Answers3

2

In the file scope in C you may place only declarations. You may not execute statements in the file scope. The same is valid for C++ where you may place in a namespace only declarations.

Note: In C declarations are not statements while in C++ declarations are statements. Nevertheless except the declaration statement other statements may not be present in a namespace in C++. It is interesting also to note that in C there is a null statement but there is no an empty declaration. While in C++ there may be an empty declaration.

So this program

#include <iostream>

int* x = new int;
*x = 5;

int main()
{

}

is invalid.

These error messages of the compiler

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2374: 'x': redefinition; multiple initialization
message : see declaration of 'x'

mean that the compiler tries to interpret the assignment statement as a declaration.

But this program

#include <iostream>

int* x = new int;

int main()
{
    *x = 5;
}

is correct. In this program the assignment statement is present in the outer block scope of the function main.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

What you are trying to do is not possible in global scope.

If you need to do this way try to initialize as below :

// good practice 
namespace
{
    int* x = new int(5);
}

int main()
{
    // You can later modify in function scope
    *x = 10;
}
sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55
0

You cannot have statements other than declarations, outside of any scope (for simplicity consider a scope whatever is inside brackets). So, compiler is trying to interpret

*x = 5;

as a declaration, so as a pointer to a given type, but does not find the type pointed to, so generates an error.

fabiop
  • 179
  • 10