小编siv*_*rdy的帖子

在 C++ 中使用命名空间时出现“未命名类型”错误

在下面的代码中编写语句A::x=5给出了错误:

命名空间“A”中的“x”未命名类型

我们不能为x变量全局赋值吗?

#include <iostream>

int x = 10;  

namespace A
{
    int x = 20; 
}

A::x=5;

int main()
{
    int x = 30; 
    std::cout << "x = " << x << std::endl;
    std::cout << "A::x = " << A::x << std::endl;
    std::cout << "::x = " << ::x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ definition statements

5
推荐指数
1
解决办法
67
查看次数

标签 统计

c++ ×1

definition ×1

statements ×1