rai*_*lin 3 c++ stl compiler-errors
我有一个程序不能在Mac上编译(gcc 4.2.1,Apple Inc. build 5658),但似乎在Linux上没有问题(gcc 4.7.2).当我尝试编写一个带有包含STL向量的STL向量的成员函数的类时,我得到了上述错误:
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
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
如果相同的函数原型不在类定义中,则所有内容都会编译并运行正常.
我是否犯了我的Linux编译器容忍的愚蠢的C++错误,或者这是一个Mac编译器问题?
完整的错误消息:
$ 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> > >&)
Run Code Online (Sandbox Code Playgroud)
更新...我用向量向量测试了一个类似的例子,并没有得到相同的错误:
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
Run Code Online (Sandbox Code Playgroud)
测试2.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);
}
Run Code Online (Sandbox Code Playgroud)
这是Apple版本中的编译器错误.我认为你可以通过命名你的参数并在你的默认值周围添加父项来解决它:
void printother(const std::vector<std::pair<float,float> >& ref = (std::vector<std::pair<float,float> >()));
^ ^ ^
Run Code Online (Sandbox Code Playgroud)