是否可以声明一个整数而不定义它?C++:如何在不定义的情况下声明非函数?
在C++中,可以分离函数的定义和声明。
// foo.cpp
int foo(int);
int foo(int a) {
return 45;
}
但与非功能,它并不显得
// bar.cpp
int bar;
int bar = 10;
bar.cpp
产生这种
$ clang++ -c bar.cpp
bar.cpp:2:5: error: redefinition of 'a'
int a = 10;
^
bar.cpp:1:5: note: previous definition is here
int a;
^
1 error generated.
娅类型标注的第二条语句产生不同的错误。
// bar2.cpp
int bar;
bar = 10;
产生
$ clang++ -c bar2.cpp
bar2.cpp:3:1: error: C++ requires a type specifier for all declarations
bar = 10;
^
1 error generated.
我花费额外的5秒测试,以确保它的工作原理,和别人击败了我! –