我不明白,为什么不能写:
struct foo {
void bar(); // declaration
void bar(){std::cout << "moo" << std::endl;} // declaration + definition
};
该函数被声明了两次(我以为这样是可以的),但只被定义了一次。然而,我的编译器报错:
decldef.cxx:7:10: error: 'void foo::bar()' cannot be overloaded
为什么不允许这样做?
为什么我的编译器(g++ 4.7.2)会把这个解释为重载?
PS:我知道该如何“正确地”编写它,但我只是想知道为什么上述方法是错误的。
struct
替换为namespace
,那么这段代码就是合法的。 - Bathsheba