【发布时间】:2020-03-19 06:45:17
【问题描述】:
更新:
感谢大家帮助理解这一点!
我尝试运行这个:
#include <iostream>
int* x = new int;
*x = 5;
int main()
{
}
我收到以下错误:
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 ==========
但是,如果我在主函数中将值分配给 x,我不会收到任何错误。
像这样:
#include <iostream>
int* x = new int;
int main()
{
*x = 5;
}
怎么会?
【问题讨论】:
标签: c++ c scope namespaces declaration