3

我有一个无法在 Mac 上编译的程序(gcc 4.2.1,Apple Inc. build 5658),但在 Linux 上似乎没有问题(gcc 4.7.2)。当我尝试使用包含 STL 对对象的 STL 向量的成员函数编写一个类时,出现上述错误:

测试.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

测试.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();
}

如果相同的函数原型不在类定义中,则一切都可以编译并运行良好。

我是在犯我的 Linux 编译器可以容忍的愚蠢的 C++ 错误,还是这是 Mac 编译器的问题?

完整的错误信息:

$ 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<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> > >&)

更新...我已经使用向量向量测试了一个类似的示例,但没有得到相同的错误:

测试2.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

测试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);

}
4

1 回答 1

8

这是 Apple 版本中的编译器错误。我认为您可以通过命名参数并在默认值周围添加父母来解决此问题:

void printother(const std::vector<std::pair<float,float> >& ref = (std::vector<std::pair<float,float> >()));
                                                            ^     ^                                      ^          
于 2013-05-29T00:15:03.360 回答