gpt4 book ai didi

c++ - g++ 编译器错误 : expected ‘,’ or ‘...’ before ‘>’ token only on Mac

转载 作者:太空狗 更新时间:2023-10-29 20:04:50 24 4
gpt4 key购买 nike

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

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

如果相同的函数原型(prototype)不在类定义中,一切都可以正常编译和运行。

我是否犯了 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> > >&)

一个更新...我用 vector 的 vector 测试了一个类似的例子,没有得到同样的错误:

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

}

最佳答案

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

void printother(const std::vector<std::pair<float,float> >& ref = (std::vector<std::pair<float,float> >()));
^ ^ ^

关于c++ - g++ 编译器错误 : expected ‘,’ or ‘...’ before ‘>’ token only on Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16803410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com