Note that there are some explanatory texts on larger screens.

plurals
  1. POprivate and public operator overloading
    primarykey
    data
    text
    <p>I tried to implement the method of Horner and faced a problem:</p> <pre><code>root@host:~# cat | g++ -x c++ -std=gnu++11 - &amp;&amp; ./a.out #include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;iterator&gt; #include &lt;vector&gt; #include &lt;numeric&gt; #include &lt;algorithm&gt; #include &lt;random&gt; #include &lt;chrono&gt; #include &lt;cstdlib&gt; template&lt; typename F &gt; class horner { public : typedef std::vector&lt; F &gt; V; horner(F const &amp; x_, V const &amp; c_) : x(x_) , c(c_) , y(0.0L) { ; } operator F const () const { return std::accumulate(c.rbegin(), c.rend(), *this).y; } private : friend horner std::accumulate&lt; typename V::const_reverse_iterator, horner &gt;(typename V::const_reverse_iterator, typename V::const_reverse_iterator, horner); void operator = (F const &amp; rhs) { y = rhs; } operator F () { y *= x; return y; } F const &amp; x; V const &amp; c; F y; }; #define N_COEFF_PARABOLA 3 int main() { typedef double F; typedef typename horner&lt; F &gt;::V V; V c; unsigned seed(std::chrono::system_clock::now().time_since_epoch().count()); std::cout &lt;&lt; "seed = 0x" &lt;&lt; std::uppercase &lt;&lt; std::hex &lt;&lt; std::setfill('0') &lt;&lt; std::setw(sizeof(seed) * 2) &lt;&lt; seed &lt;&lt; std::endl; std::mt19937 generator(seed); std::generate_n(std::back_inserter(c), N_COEFF_PARABOLA, generator); std::cout &lt;&lt; "coefficients: "; std::copy(c.begin(), c.end(), std::ostream_iterator&lt; F &gt;(std::cout, " ")); std::cout &lt;&lt; ';' &lt;&lt; std::endl; F const x(generator()); F const y(horner&lt; F &gt;(x, c)); std::cout &lt;&lt; "y(" &lt;&lt; x &lt;&lt; ") = " &lt;&lt; y &lt;&lt; std::endl; // naive F xx(1.0L); F yy(0.0L); for (typename V::size_type i(0); i &lt; c.size(); ++i) { yy += c[i] * xx; xx *= x; } std::cout &lt;&lt; "y'(" &lt;&lt; x &lt;&lt; ") = " &lt;&lt; yy &lt;&lt; std::endl; return EXIT_SUCCESS; } // press ^D &lt;stdin&gt;: In function ‘int main()’: &lt;stdin&gt;:39:5: error: ‘horner&lt;F&gt;::operator F() [with F = double]’ is private &lt;stdin&gt;:71:32: error: within this context root@host:~# </code></pre> <p>In my view the problem should not arise, since <code>main()</code> only sees <code>operator F const &amp; () const</code> version of the type conversion operator. But it is.</p> <p>What is the reason of the error?</p>
    singulars
    1. This table or related slice is empty.
    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