C++ ifstream 类错误

发布于 2025-01-02 14:41:50 字数 4669 浏览 2 评论 0原文

我围绕这个问题进行了很多搜索,发现了很多类似的问题,但我还没有找到解决方案。我正在声明一个类:

class File {

    public:
            string fileName;
            std::ifstream & flinstream;
            Password pass;
            //Next block to look at
            unsigned int nb;
            unsigned int sectorsLeft;
File (string name,string passd);
File ( );
};

和一个相应的函数:

File::File (string name,string passd) {
         fileName =  name;
        const char* cstr =  name.c_str();
         pass =  Password(passd);
         flinstream =  std::ifstream(cstr);
        if(!flinstream.good()) {
            string err =  "The file '";
            err.append(name);
            err.append("' could not be opened!");
            callError(err,3);
        }
    }

在编译时,我收到以下错误:

 [0] => out.cpp: In constructor ‘File::File(std::string, std::string)’:
    [1] => out.cpp:130:3: error: uninitialized reference member ‘File::flinstream’
    [2] => In file included from /usr/include/c++/4.5/ios:39:0,
    [3] =>                  from /usr/include/c++/4.5/ostream:40,
    [4] =>                  from /usr/include/c++/4.5/iostream:40,
    [5] =>                  from out.cpp:1:
    [6] => /usr/include/c++/4.5/bits/ios_base.h: In member function ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’:
    [7] => /usr/include/c++/4.5/bits/ios_base.h:788:5: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private
    [8] => /usr/include/c++/4.5/iosfwd:77:11: error: within this context
    [9] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’:
    [10] => /usr/include/c++/4.5/iosfwd:83:11: note: synthesized method ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ first required here
    [11] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’:
    [12] => /usr/include/c++/4.5/iosfwd:111:11: note: synthesized method ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’ first required here
    [13] => /usr/include/c++/4.5/streambuf: In member function ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’:
    [14] => /usr/include/c++/4.5/streambuf:781:7: error: ‘std::basic_streambuf<_CharT, _Traits>::__streambuf_type& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>::__streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
    [15] => /usr/include/c++/4.5/iosfwd:108:11: error: within this context
    [16] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’:
    [17] => /usr/include/c++/4.5/iosfwd:111:11: note: synthesized method ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’ first required here
    [18] => out.cpp: In constructor ‘File::File(std::string, std::string)’:
    [19] => out.cpp:134:36: note: synthesized method ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’ first required here
    [20] => out.cpp: In constructor ‘File::File()’:
    [21] => out.cpp:142:3: error: uninitialized reference member ‘File::flinstream’
    [22] => out.cpp: In member function ‘File& File::operator=(const File&)’:
    [23] => out.cpp:51:12: error: non-static reference member ‘std::ifstream& File::flinstream’, can't use default assignment operator
    [24] => out.cpp: In function ‘int main(int, char**)’:
    [25] => out.cpp:166:57: note: synthesized method ‘File& File::operator=(const File&)’ first required here
)

我收集到 ifstream 对赋值和所有内容相当特殊,但我没有想法如何将其包含在课程中。预先感谢您的帮助!

编辑:我已经尝试了上述类的几种排列,例如使用普通变量:

std::ifstream flinstream;

以及使用建议的 open() 函数:

flinstream.open(cstr);

但是,错误仍然相同。

I've searched around a lot with this question and found a lot of questions that were similar, but I haven't managed to find a solution. I'm declaring a class:

class File {

    public:
            string fileName;
            std::ifstream & flinstream;
            Password pass;
            //Next block to look at
            unsigned int nb;
            unsigned int sectorsLeft;
File (string name,string passd);
File ( );
};

and a corresponding function:

File::File (string name,string passd) {
         fileName =  name;
        const char* cstr =  name.c_str();
         pass =  Password(passd);
         flinstream =  std::ifstream(cstr);
        if(!flinstream.good()) {
            string err =  "The file '";
            err.append(name);
            err.append("' could not be opened!");
            callError(err,3);
        }
    }

at compile-time, I get the following errors:

 [0] => out.cpp: In constructor ‘File::File(std::string, std::string)’:
    [1] => out.cpp:130:3: error: uninitialized reference member ‘File::flinstream’
    [2] => In file included from /usr/include/c++/4.5/ios:39:0,
    [3] =>                  from /usr/include/c++/4.5/ostream:40,
    [4] =>                  from /usr/include/c++/4.5/iostream:40,
    [5] =>                  from out.cpp:1:
    [6] => /usr/include/c++/4.5/bits/ios_base.h: In member function ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’:
    [7] => /usr/include/c++/4.5/bits/ios_base.h:788:5: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private
    [8] => /usr/include/c++/4.5/iosfwd:77:11: error: within this context
    [9] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’:
    [10] => /usr/include/c++/4.5/iosfwd:83:11: note: synthesized method ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ first required here
    [11] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’:
    [12] => /usr/include/c++/4.5/iosfwd:111:11: note: synthesized method ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’ first required here
    [13] => /usr/include/c++/4.5/streambuf: In member function ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’:
    [14] => /usr/include/c++/4.5/streambuf:781:7: error: ‘std::basic_streambuf<_CharT, _Traits>::__streambuf_type& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>::__streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
    [15] => /usr/include/c++/4.5/iosfwd:108:11: error: within this context
    [16] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’:
    [17] => /usr/include/c++/4.5/iosfwd:111:11: note: synthesized method ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’ first required here
    [18] => out.cpp: In constructor ‘File::File(std::string, std::string)’:
    [19] => out.cpp:134:36: note: synthesized method ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’ first required here
    [20] => out.cpp: In constructor ‘File::File()’:
    [21] => out.cpp:142:3: error: uninitialized reference member ‘File::flinstream’
    [22] => out.cpp: In member function ‘File& File::operator=(const File&)’:
    [23] => out.cpp:51:12: error: non-static reference member ‘std::ifstream& File::flinstream’, can't use default assignment operator
    [24] => out.cpp: In function ‘int main(int, char**)’:
    [25] => out.cpp:166:57: note: synthesized method ‘File& File::operator=(const File&)’ first required here
)

I've gathered that ifstream is rather particular with the assignment and all, but I've no idea how to include it in a class. Thanks in advance for your help!

EDIT: I've tried several permutations of the above class, such as using a normal variable:

std::ifstream flinstream;

As well as using the open() function suggested:

flinstream.open(cstr);

However, the error remains the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2025-01-09 14:41:50

对于初学者,除非您确实想要对 ifstream引用,否则我只需在您的类中将 ifstream 声明为

std::ifstream flinstream;

In C++03 (以前版本的 C++),流类的赋值被禁用,因此该行将

flinstream =  std::ifstream(cstr);

无法编译。但是,您可以使用 std::ifstream::open 方法来执行此操作:

flinstream.open(cstr);
/* ... remaining processing ... */

希望这会有所帮助!

For starters, unless you really want a reference to an ifstream, I would just declare the ifstream in your class as

std::ifstream flinstream;

In C++03 (the previous version of C++), assignment is disabled for stream classes, so the line

flinstream =  std::ifstream(cstr);

Will not compile. You can, however, use the std::ifstream::open method to do this:

flinstream.open(cstr);
/* ... remaining processing ... */

Hope this helps!

优雅的叶子 2025-01-09 14:41:50

引用不能处于未初始化状态。您必须初始化初始化列表中的所有引用成员。

flinstream 正在构造函数的主体中初始化。当构造函数主体执行时,flinstream 的值必须具有合法值。参考文献必须始终具有法律价值。

初始值设定项列表应始终优先于构造函数主体。

References cannot be left uninitialized. You must initialize all reference members in the initializer list.

flinstream is being initialized in the body of the constructor. By the time the body of the constructor is executing, the value of flinstream must have a legal value. References must always have a legal value.

The initializer list should always be favored over the constructor body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文