Reputation: 1281
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:
test.h:
#ifndef TEST_H_
#define TEST_H_
#include <vector>
#include <utility>
#include <string>
class testclass
{
public:
void printcontent(const std::vector<std::string> & = std::vector<std::string>());
void printother(const std::vector<std::pair<float,float> >& = std::vector<std::pair<float,float> >());
};
#endif
test.cpp:
#include "test.h"
#include <iostream>
using namespace std;
void testclass::printcontent(const vector<string> &v)
{
if (v.empty())
cout << "vector was empty!" << endl;
else
for (unsigned i = 0; i < v.size(); i++)
cout << v.at(i) << endl;
}
void testclass::printother(const vector<pair<float,float> >& v)
{
if (v.empty())
cout << "vector was empty!" << endl;
else
for (unsigned i = 0; i < v.size(); i++)
cout << v.at(i).first << ' ' << v.at(i).second << endl;
}
int main()
{
testclass tc;
vector<string> v1;
v1.push_back("a");
v1.push_back("b");
v1.push_back("c");
tc.printcontent(v1);
tc.printcontent();
vector<pair<float,float> > v2;
v2.push_back(pair<float,float>(1,2));
v2.push_back(pair<float,float>(3,4));
v2.push_back(pair<float,float>(5,6));
tc.printother(v2);
tc.printother();
}
If the same function prototypes are not inside a class definition, everything compiles and runs fine.
Am I making a stupid C++ error that my Linux compiler tolerates, or is this a Mac compiler problem?
The full error message:
$ g++ -o test test.cpp
In file included from test.cpp:1:
test.h:12: error: expected ‘,’ or ‘...’ before ‘>’ 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<class _T1, class _T2> 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<std::pair<float, float>, std::allocator<std::pair<float, float> > >&, float)’
test.cpp:18: error: prototype for ‘void testclass::printother(const std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&)’ does not match any in class ‘testclass’
test.h:12: error: candidate is: void testclass::printother(const std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&, float)
test.cpp: In function ‘int main()’:
test.cpp:46: error: call of overloaded ‘printother(std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&)’ is ambiguous
test.h:12: note: candidates are: void testclass::printother(const std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&, float)
test.cpp:18: note: void testclass::printother(const std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&)
An update... I've tested a similar example with a vector of vectors, and don't get the same error:
test2.h:
#ifndef TEST2_H_
#define TEST2_H_
#include <vector>
class testclass
{
public:
void printother(const std::vector<std::vector<float> > & = std::vector<std::vector<float> >());
};
#endif
test2.cpp:
#include "test2.h"
#include <iostream>
using namespace std;
void testclass::printother(const vector<vector<float> >& v)
{
if (v.empty())
cout << "vector was empty!" << endl;
else
for (unsigned i = 0; i < v.size(); i++)
for (unsigned j = 0; j < v.at(i).size(); j++)
if (j < v.at(i).size() - 1)
cout << v.at(i).at(j) << ' ';
else
cout << v.at(i).at(j) << endl;
}
int main()
{
testclass tc;
vector<vector<float> > v2;
v2.push_back(vector<float>());
v2.back().push_back(0);
v2.back().push_back(1);
v2.back().push_back(2);
v2.push_back(vector<float>());
v2.back().push_back(3);
v2.back().push_back(4);
v2.back().push_back(5);
tc.printother(v2);
}
Upvotes: 3
Views: 1696
Reputation: 2054
It's a compiler bug in Apple's version. I think you can work it around by naming your parameter and adding parents around your default value:
void printother(const std::vector<std::pair<float,float> >& ref = (std::vector<std::pair<float,float> >()));
^ ^ ^
Upvotes: 8