I have a project going with multiple levels of inheritance. It goes like this
XMLs -> Entity -> Item
and then there are numerous item classes that inherit from Item which then inherit from Entity. Now, I have each class defined as shown
class Entity: public XMLs
{
public:
Entity() {}
virtual ~Entity() {};
//other functions
};
This is the one that is giving me trouble. Whenever I try to create an Item object or any type of object at all really in my main function, it gives me the following error
/usr/include/c++/4.6/ostream: In constructor ‘Entity::Entity()’: /usr/include/c++/4.6/ostream:363:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits]’ is protected
What does this mean? Everything I google about constructors and protected involves the keyword protected where I have public.
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.