抱歉找不到我问题的更好标题.基本上我注意到以下编译很好:
#include <vector>
void foo();
int main () {
foo();
return 0;
}
namespace{
struct point {
double x, y;
};
}
void foo(){
std::vector<point> p;
}
Run Code Online (Sandbox Code Playgroud)
而编译器抱怨以下内容:
#include <vector>
void foo();
int main () {
foo();
return 0;
}
void foo(){
struct point {
double x, y;
};
std::vector<point> p;
}
// g++ output:
a.cpp: In function ‘void foo()’:
a.cpp:14: error: template argument for ‘template<class _Tp> class std::allocator’ uses local type ‘foo()::point’
a.cpp:14: error: trying to instantiate ‘template<class _Tp> class std::allocator’
a.cpp:14: error: template argument 2 is invalid
a.cpp:14: error: invalid type in declaration before ‘;’ token
Run Code Online (Sandbox Code Playgroud)
我想知道第二种方法有什么问题?struct point在创建新std::vector<point>对象时是不是完全定义了?