2012-09-30 30 views
0

我有一个GolfCourse类头gCourse.hh,我想为>>运算符实现运算符重载。我如何在文件gcourse.cc的标题之外进行此操作?也就是说,我需要指出哪些“词”本身,“GolfCourse ::”是不够的,像功能...?在C++中实现运算符在类头之外的超载

gcourse.hh: 
class GolfCourse { 

public: 
--- 
friend std::istream& operator>> (std::istream& in, GolfCourse& course); 

gcourse.cc: 
---implement operator>> here --- 

回答

2

GolfCourse::不正确,因为operator >>不是GolfCourse成员。这是一个免费的功能。你只需要写:

std::istream& operator>> (std::istream& in, GolfCourse& course) 
{ 
    //... 
    return in; 
} 

friend声明在类定义时,才需要,如果你打算从GolfCourse访问privateprotected成员。当然,您可以在类定义中提供实现:

class GolfCourse { 
public: 
    friend std::istream& operator>> (std::istream& in, GolfCourse& course) 
    { 
     //... 
     return in; 
    } 
}; 
+0

好吧,现在我可以看到,我只是太执着于功能...谢谢! – rize

+0

是的,我需要访问GolfCourse的私人成员,所以需要朋友。 – rize