Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing '+' operator for std::vector
    primarykey
    data
    text
    <p>I'm trying to add support for addition for std::vector. Here's the code I have so far.. The part that doesn't work is the part where I attempt to print the result. I am aware of valarray but I can't get it to work the way that I want (mostly I haven't found an easy way to convert vectors into valarrays).</p> <p>This is the error:</p> <pre><code>../src/VectorOperations.cpp:26:6: error: need 'typename' before 'std::vector&lt;T&gt;::iterator' because 'std::vector&lt;T&gt;' is a dependent scope </code></pre> <hr> <pre><code>class VectorOperations { public: //Vector Operations std::vector&lt;double&gt; logv(std::vector&lt;double&gt; first); std::vector&lt;double&gt; raiseTo(std::vector&lt;double&gt; first, int power); std::vector&lt;double&gt; xthRoot(std::vector&lt;double&gt; first, int xth); double sumv(std::vector&lt;int&gt; first); std::vector&lt;double&gt; operator + ( const std::vector&lt;double&gt; &amp; ) const; std::vector&lt;double&gt; operator - ( const std::vector&lt;double&gt; &amp; ) const; std::vector&lt;double&gt; operator * ( const std::vector&lt;double&gt; &amp; ) const; std::vector&lt;double&gt; operator / ( const std::vector&lt;double&gt; &amp; ) const; }; template &lt;typename T&gt; std::vector&lt;T&gt; operator+(const std::vector&lt;T&gt;&amp; a, const std::vector&lt;T&gt;&amp; b) { assert(a.size() == b.size()); std::vector&lt;T&gt; result; result.reserve(a.size()); std::transform(a.begin(), a.end(), b.begin(), std::back_inserter(result), std::plus&lt;T&gt;()); std::cout&lt;&lt;"Results from addition follow: \n"; //HERE'S THE PROBLEM: I WANT TO PRINT OUT BUT I GET ERRORS for(std::vector&lt;T&gt;::iterator it = a.begin(); it != a.end(); ++it) { /* std::cout &lt;&lt; *it; ... */ } return result; } </code></pre>
    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.
 

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