Invalid qualified name use

I am trying to do the following:

#include <iostream>

namespace A
{
    extern int j;
}

int main()
{
    int A::j=5;
    std::cout << A::j;
}

But with me error: invalid use of qualified-name ‘A::j’. Please explain why this error occurred?

+4
source share
2 answers

Please explain why this error occurred?

The language simply does not allow you to define visibility space variables within functions. The definition should be either in namespace A:

namespace A {
    int j = 5;
}

or in the surrounding (global) namespace:

int A::j = 5;

You can, of course, assign a value to a variable inside a function:

int main() {
    A::j = 5;
    // ...
}

but you will also need a definition somewhere, since your program does not have it.

+4
source
#include <iostream>

namespace A
{
    int j;
}

int main()
{
    A::j=5;
    std::cout << A::j;
    return 0;
}

j A extern , . , . , extern A 'int' .

+2

Source: https://habr.com/ru/post/1542883/


All Articles