2011-10-31 108 views
0

我在Visual C++编写的小程序++ 2010继承功能inaccessable在C++

这是基类的代码:

class BaseInfo { 

private: 
    std::map <std::string, std::string> info; 
    std::vector<std::string> extra_info_key; 
public: 
    uint get_id(); 
    //Other function is hidden 
}; 

uint BaseInfo::get_id() { 
    return (uint)atoi ((info["ID"]).c_str()); 
} 

然后我做一个派生类,它宣称为:

class test:BaseInfo { 
public: 
    void f(); 
}; 

void test::f (test& inf) { 
    cout<<inf.get_id()<<endl; 
} 

但我得到了一个错误:

function "BaseInfo::get_id is inaccessible.

我很困惑,似乎所有的都在C++规则中。

+0

可我建议使用一些更多的空间用于可读性 – sehe

+0

[什么是访问符?我应该继承与私人,保护或公共?](http://stackoverflow.com/questions/5447498/what-are-access-specifiers-should-i-inherit-with-private-protected-or-public/%5d )应该是一个很好的阅读。 –

回答

13

您正在使用私有继承,这就是为什么。 变化:

class test : BaseInfo 

到:

class test : public BaseInfo 

有关公共,保护和私有继承更多的信息,看here

+0

有趣的是,结构默认地私下继承了类。 – DanDan

+2

与成员相同,结构中默认情况下是公开的,在类中 - 私有。 – Griwes