您可能忘記了包括iostream
,這使得ostream
可見。您還需要將其更改爲std::ostream
,因爲C++標準庫名稱位於命名空間std
內。
Do not write using namespace std;
in a header-file, ever!
這是確定將它放入實現文件,如果你想,或者,如果你寫了一個朋友的例子。因爲包含該頭文件的任何文件都會將所有標準庫看作全局名稱,這是一個巨大的混亂並且聞起來很多。它突然增加了與其他全球名稱或其他using
'編號名稱發生名稱衝突的機會 - 我將完全避免使用指令(請參閱Herb Sutter的Using me)。因此,更改代碼到這個
#include <iostream>
// let ScaryDream be the interface
class HereBeDragons : public ScaryDream {
...
// mentioning virtual in the derived class again is not
// strictly necessary, but is a good thing to do (documentary)
virtual std::ostream & print(std::ostream & out) const;
...
};
而且在實現文件( 「的.cpp」)
#include "HereBeDragons.h"
// if you want, you could add "using namespace std;" here
std::ostream & HereBeDragons::print(std::ostream & out) const {
return out << "flying animals" << std::endl;
}
+1爲描述性的類名:) – Eric 2009-07-01 15:34:18