C++ g++;编译器错误:应为‘’;或‘’;在‘之前&燃气轮机’;仅在Mac上使用令牌
我有一个程序不会在Mac(GCC4.2.1,Apple Inc.build 5658)上编译,但在Linux(GCC4.7.2)上似乎没有问题。当我试图编写一个包含成员函数的类,该类采用包含STL对对象的STL向量时,会出现上述错误: 测试h:C++ g++;编译器错误:应为‘’;或‘’;在‘之前&燃气轮机’;仅在Mac上使用令牌,c++,stl,compiler-errors,C++,Stl,Compiler Errors,我有一个程序不会在Mac(GCC4.2.1,Apple Inc.build 5658)上编译,但在Linux(GCC4.7.2)上似乎没有问题。当我试图编写一个包含成员函数的类,该类采用包含STL对对象的STL向量时,会出现上述错误: 测试h: #ifndef TEST_H_ #define TEST_H_ #include <vector> #include <utility> #include <string> class testclass { pu
#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
\ifndef测试_
#定义测试_
#包括
#包括
#包括
类testclass
{
公众:
void printcontent(const std::vector&=std::vector());
void printother(const std::vector&=std::vector());
};
#恩迪夫
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();
}
#包括“test.h”
#包括
使用名称空间std;
void testclass::printcontent(const vector&v)
{
if(v.empty())
cout这是Apple版本中的一个编译器错误。我认为您可以通过命名参数并在默认值周围添加父项来解决它:
void printother(const std::vector<std::pair<float,float> >& ref = (std::vector<std::pair<float,float> >()));
^ ^ ^
void printother(const std::vector&ref=(std::vector());
^ ^ ^
行号是什么?你能发布完整的错误吗?你确定没有更改任何内容吗?我希望在使用>
而不是>
作为结束尖括号时会出现错误。顺便说一句,在向浮点添加整数类型时,应该会收到警告确认Xcode的g++(4.2)和Clang++都无法编译。使用MacPorts版本的g++(4.7)可以很好地编译代码。Clang++发出的第一个错误是:/test.h:12:98:error:expected')'…>&=std::vector());您只需要括号。命名是不必要的。哇。这完全奏效了。我99%确定我只是在做一些愚蠢的事情。这似乎不会影响所有空向量默认参数,只影响对(这是关于双“>”?如果我有一个std::vector>,会有同样的问题吗?)这是一个解析错误。C++是很难解析的。我不惊讶,更多的模板参数,最有可能你可以解析错误。现在,很难知道什么会触发它而不看GCC代码。最好的方法是尝试编译代码,看看:)确实。这不是一个问题2D STD::向量…见上面.
#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
#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);
}
void printother(const std::vector<std::pair<float,float> >& ref = (std::vector<std::pair<float,float> >()));
^ ^ ^