Я пытаюсь создать файл в производном классе, но я получаю сообщение об ошибке ниже.
Мой код:
#include <string>
#include <fstream>
class Animal
{
public:
Animal();
~Animal();
protected:
std::string name;
};
class Cat : public Animal
{
public:
Cat(std::string const &filename);
private:
std::ofstream _file;
};
Cat::Cat(std::string const &filename) : _file(filename.c_str(), std::ios::out)
{
this->_file.open(filename);
}
int main()
{
Cat c1 = Cat("cat.txt");
}
, и я получаю следующую ошибку,
In file included from animal.cpp:3:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/fstream:169:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ostream:139:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/streambuf:486:64: error: base class 'std::__1::ios_base' has
private copy constructor
_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:133:32: note: in implicit copy constructor for
'std::__1::basic_ios<char>' first required here
class _LIBCPP_TEMPLATE_VIS basic_ofstream;
^
animal.cpp:15:7: note: in implicit copy constructor for 'std::__1::basic_ofstream<char, std::__1::char_traits<char> >' first required here
class Cat : public Animal
^
animal.cpp:34:14: note: in implicit copy constructor for 'Cat' first required here
Cat c1 = Cat("cat.txt");
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ios:313:5: note: declared private here
ios_base(const ios_base&); // = delete;
^
1 error generated.