Note that there are some explanatory texts on larger screens.

plurals
  1. POg++ compiler error: expected ‘,’ or ‘...’ before ‘>’ token only on Mac
    primarykey
    data
    text
    <p>I've got a program that won't compile on a Mac (gcc 4.2.1, Apple Inc. build 5658) but seems to have no trouble on a Linux (gcc 4.7.2). I get the above error when I try to write a class with member functions that take STL vectors containing STL pair objects:</p> <p>test.h:</p> <pre><code>#ifndef TEST_H_ #define TEST_H_ #include &lt;vector&gt; #include &lt;utility&gt; #include &lt;string&gt; class testclass { public: void printcontent(const std::vector&lt;std::string&gt; &amp; = std::vector&lt;std::string&gt;()); void printother(const std::vector&lt;std::pair&lt;float,float&gt; &gt;&amp; = std::vector&lt;std::pair&lt;float,float&gt; &gt;()); }; #endif </code></pre> <p>test.cpp:</p> <pre><code>#include "test.h" #include &lt;iostream&gt; using namespace std; void testclass::printcontent(const vector&lt;string&gt; &amp;v) { if (v.empty()) cout &lt;&lt; "vector was empty!" &lt;&lt; endl; else for (unsigned i = 0; i &lt; v.size(); i++) cout &lt;&lt; v.at(i) &lt;&lt; endl; } void testclass::printother(const vector&lt;pair&lt;float,float&gt; &gt;&amp; v) { if (v.empty()) cout &lt;&lt; "vector was empty!" &lt;&lt; endl; else for (unsigned i = 0; i &lt; v.size(); i++) cout &lt;&lt; v.at(i).first &lt;&lt; ' ' &lt;&lt; v.at(i).second &lt;&lt; endl; } int main() { testclass tc; vector&lt;string&gt; v1; v1.push_back("a"); v1.push_back("b"); v1.push_back("c"); tc.printcontent(v1); tc.printcontent(); vector&lt;pair&lt;float,float&gt; &gt; v2; v2.push_back(pair&lt;float,float&gt;(1,2)); v2.push_back(pair&lt;float,float&gt;(3,4)); v2.push_back(pair&lt;float,float&gt;(5,6)); tc.printother(v2); tc.printother(); } </code></pre> <p>If the same function prototypes are not inside a class definition, everything compiles and runs fine.</p> <p>Am I making a stupid C++ error that my Linux compiler tolerates, or is this a Mac compiler problem?</p> <p>The full error message:</p> <pre><code>$ g++ -o test test.cpp In file included from test.cpp:1: test.h:12: error: expected ‘,’ or ‘...’ before ‘&gt;’ token test.h:12: error: wrong number of template arguments (1, should be 2) /usr/include/c++/4.2.1/bits/stl_pair.h:68: error: provided for ‘template&lt;class _T1, class _T2&gt; struct std::pair’ test.h:12: error: template argument 1 is invalid test.h:12: error: template argument 2 is invalid test.h:12: error: default argument missing for parameter 2 of ‘void testclass::printother(const std::vector&lt;std::pair&lt;float, float&gt;, std::allocator&lt;std::pair&lt;float, float&gt; &gt; &gt;&amp;, float)’ test.cpp:18: error: prototype for ‘void testclass::printother(const std::vector&lt;std::pair&lt;float, float&gt;, std::allocator&lt;std::pair&lt;float, float&gt; &gt; &gt;&amp;)’ does not match any in class ‘testclass’ test.h:12: error: candidate is: void testclass::printother(const std::vector&lt;std::pair&lt;float, float&gt;, std::allocator&lt;std::pair&lt;float, float&gt; &gt; &gt;&amp;, float) test.cpp: In function ‘int main()’: test.cpp:46: error: call of overloaded ‘printother(std::vector&lt;std::pair&lt;float, float&gt;, std::allocator&lt;std::pair&lt;float, float&gt; &gt; &gt;&amp;)’ is ambiguous test.h:12: note: candidates are: void testclass::printother(const std::vector&lt;std::pair&lt;float, float&gt;, std::allocator&lt;std::pair&lt;float, float&gt; &gt; &gt;&amp;, float) test.cpp:18: note: void testclass::printother(const std::vector&lt;std::pair&lt;float, float&gt;, std::allocator&lt;std::pair&lt;float, float&gt; &gt; &gt;&amp;) </code></pre> <p>An update... I've tested a similar example with a vector of vectors, and don't get the same error:</p> <p>test2.h:</p> <pre><code>#ifndef TEST2_H_ #define TEST2_H_ #include &lt;vector&gt; class testclass { public: void printother(const std::vector&lt;std::vector&lt;float&gt; &gt; &amp; = std::vector&lt;std::vector&lt;float&gt; &gt;()); }; #endif </code></pre> <p>test2.cpp:</p> <pre><code>#include "test2.h" #include &lt;iostream&gt; using namespace std; void testclass::printother(const vector&lt;vector&lt;float&gt; &gt;&amp; v) { if (v.empty()) cout &lt;&lt; "vector was empty!" &lt;&lt; endl; else for (unsigned i = 0; i &lt; v.size(); i++) for (unsigned j = 0; j &lt; v.at(i).size(); j++) if (j &lt; v.at(i).size() - 1) cout &lt;&lt; v.at(i).at(j) &lt;&lt; ' '; else cout &lt;&lt; v.at(i).at(j) &lt;&lt; endl; } int main() { testclass tc; vector&lt;vector&lt;float&gt; &gt; v2; v2.push_back(vector&lt;float&gt;()); v2.back().push_back(0); v2.back().push_back(1); v2.back().push_back(2); v2.push_back(vector&lt;float&gt;()); v2.back().push_back(3); v2.back().push_back(4); v2.back().push_back(5); tc.printother(v2); } </code></pre>
    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