Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding while (!is_eof)
    primarykey
    data
    text
    <p>I'm working with a (C++) library, where the object needs to be initialized with a stream. The sample code provided with the library uses this code:</p> <pre><code>// Declare the input stream HfstInputStream *in = NULL; try { // This sets up the special stream object for the later object (see below) in = new HfstInputStream("pathToFile.hfsto"); } // catch errors... // {omitted} // Initialize the object while (not in-&gt;is_eof()) { if (in-&gt;is_bad()) { std::cerr &lt;&lt; "ERROR: Stream cannot be read." &lt;&lt; std::endl; exit(1); } // Here we initialize the object using the stream HfstTransducer t(*in); } </code></pre> <p>My problem is that this object cannot be used outside of the while loop because of scoping. and I have to declare it with the stream (as far as I can tell), so I can't declare and then initialize it with the stream inside the loop.</p> <p>My questions are (1) am I wrong? Can I actually declare it outside the loop somehow? (2) is there another (better) way to do this that avoids the loop altogether. For example, if I were to use try/catch and catch the exceptions.</p> <p>I'm very new to C++ and looking to find out best practices, so please let me know what's what. Thanks.</p> <p>Also, to be clear, I'm looking to make a class that would use a persistant version of this object so I don't have to constantly create/destroy these objects every time I need to use them.</p> <p>PS: <a href="http://hfst.sourceforge.net/hfst3/classhfst_1_1HfstTransducer.html#a80b941c9d120fbec4596002c6a979096" rel="nofollow">here's a link to the documentation for the object if it is relevant</a></p> <p>EDIT: If I try to declare the variable outside the loop and then initialize it I get an error</p> <pre><code>HfstTransducer t; while (not in-&gt;is_eof()) { t(*in); } // ERROR: main.cpp:47:0 main.cpp:47: error: no match for call to '(hfst::HfstTransducer) (hfst::HfstInputStream&amp;)' </code></pre> <p>Am I trying to initialize it incorrectly?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload