-1

我有一个具有多级继承的项目。它是这样的

XMLs -> Entity -> Item然后有许多从 Item 继承的项目类,然后从 Entity 继承。现在,我定义了每个类,如图所示

    class Entity: public XMLs
    {
     public: 
            Entity() {}
            virtual ~Entity() {};
         //other functions
    };

这是给我带来麻烦的一个。每当我尝试在我的主函数中创建一个 Item 对象或任何类型的对象时,它都会给我以下错误

/usr/include/c++/4.6/ostream: 在构造函数'Entity::Entity()'中:/usr/include/c++/4.6/ostream:363:7: 错误:'std::basic_ostream<_CharT, _Traits>: :basic_ostream() [with _CharT = char, _Traits = std::char_traits]' 受保护

这是什么意思?我在谷歌上搜索的关于构造函数和受保护的所有内容都涉及我公开的关键字 protected。

4

1 回答 1

5

Read the message again It doesn't say your constructor is protected, it says std::basic_ostream's constructor is protected. Your class (or a parent thereof) has a std::basic_ostream (or maybe std::ostream) member, which cannot be default-constructed. You must construct it with an argument. This page shows that it must be cosntructed from a basic_streambuf<Elem, Tr>*.

Now I'm going to extrapolate: You probably don't actually want a std::ostream member in your class, you probably want a specific-derived type, or you want a reference, or (most likely) a an unknown or changable derived type. But since the nieve way to address the first two cases makes your class non-copiable, the final solution is virtually always the same: Use a std::unique_ptr<std::ostream> instead if your class owns the stream, or a std::ostream* if someone else owns it.

Finally: The full text for errors is in the "output" window of Visual Studio, not in the "Error" Window, which just shows summaries. The full text of that error would have many more details about the error, including (most likely) the name and line number of your class' default constructor.

于 2013-02-11T22:50:38.527 回答