C++;:STL排序STL数组的STL向量 下面的C++代码和MaFo文件产生了编译错误(对我来说是不可理解的)。有人能解释一下吗
究竟是什么问题C++;:STL排序STL数组的STL向量 下面的C++代码和MaFo文件产生了编译错误(对我来说是不可理解的)。有人能解释一下吗,c++,c++11,stl,stl-algorithm,C++,C++11,Stl,Stl Algorithm,究竟是什么问题 需要做什么来修复此代码?你能举个例子吗 我在Cygwin的GCC上成功编译了这段代码,所以我甚至不确定我可以信任哪个编译器。我附加了cpp文件(test.cpp)、我的makefile(使用clang,因为GCC错误消息更糟糕)和运行make时的实际错误消息 test.cpp: #include <array> #include <vector> #include <map> #include <algorithm> class
#include <array>
#include <vector>
#include <map>
#include <algorithm>
class Foo
{
public:
Foo(
const std::vector<std::array<int,3>> inputdata
);
private:
std::vector< std::array<int,2> > membervariable;
};
Foo::Foo(
const std::vector<std::array<int,3>> inputdata
)
:
membervariable( 0 )
{
/* 1. create all edges, allocate memory */
membervariable.resize( inputdata.size() * 3 );
for( int t = 0; t < inputdata.size(); t++ )
for( int ei = 0; ei < 3; ei++ )
{
membervariable[ 0 * inputdata.size() + t ] = { inputdata[t][0], inputdata[t][1] };
membervariable[ 1 * inputdata.size() + t ] = { inputdata[t][0], inputdata[t][2] };
membervariable[ 2 * inputdata.size() + t ] = { inputdata[t][1], inputdata[t][2] };
}
std::sort( membervariable.begin(), membervariable.end() );
auto it = std::unique( membervariable.begin(), membervariable.end() );
membervariable.resize( it - membervariable.begin() );
}
环境创建和叮当版本的输出:
$ make
clang++ -O0 -g -std=c++11 -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -pedantic -Wall -Wextra -Wno-unused-variable -Wno-sign-compare -Wno-missing-braces -Wmissing-field-initializers -Werror=implicit test.cpp -c -o test.o
In file included from test.cpp:3:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/map:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_map.h:63:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/tuple:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/array:338:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:86:52: error: too many arguments to function call, expected
single argument '__other', have 2 arguments
noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
~~~~ ^~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:264:23: note: in instantiation of exception specification for
'swap' requested here
noexcept(noexcept(__one.swap(__two)))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algobase.h:148:7: note: in instantiation of exception
specification for 'swap<int, 2>' requested here
swap(*__a, *__b);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:84:11: note: in instantiation of function template
specialization 'std::iter_swap<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *, std::__cxx1998::vector<std::__debug::array<int, 2>,
std::allocator<std::__debug::array<int, 2> > > >, std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int,
2> > > > >' requested here
std::iter_swap(__result, __b);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1916:12: note: in instantiation of function template
specialization 'std::__move_median_to_first<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
requested here
std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1948:11: note: in instantiation of function template
specialization 'std::__unguarded_partition_pivot<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
requested here
std::__unguarded_partition_pivot(__first, __last, __comp);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1963:9: note: in instantiation of function template
specialization 'std::__introsort_loop<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, long,
__gnu_cxx::__ops::_Iter_less_iter>' requested here
std::__introsort_loop(__first, __last,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:4698:12: note: in instantiation of function template
specialization 'std::__sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
requested here
std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
^
test.cpp:40:10: note: in instantiation of function template specialization
'std::sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > > >' requested here
std::sort( membervariable.begin(), membervariable.end() );
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:84:7: note: 'swap' declared here
void
^
1 error generated.
makefile:6: recipe for target 'test.o' failed
make: *** [test.o] Error 1
$ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$
$make
clang++-O0-g-std=c++11-D_GLIBCXX_DEBUG-D_GLIBCXX_DEBUG_PEDANTIC-PEDANTIC-Wall-Wextra-Wno未使用变量-Wno符号比较-Wno缺少大括号-Wmissing字段初始值设定项-Werror=implicit test.cpp-c-o test.o
在test.cpp中包含的文件中:3:
在/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../include/c++/5.4.0/map:61中包含的文件中:
在/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../include/c++/5.4.0/bits/stl_-map.h:63中包含的文件中:
在/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../include/c++/5.4.0/tuple:39中包含的文件中:
在/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../include/c++/5.4.0/array:338中包含的文件中:
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/debug/array:86:52:错误:函数调用的参数太多,应为
单个参数“\uuu other”有两个参数
noexcept(noexcept(swap(std::declval(),std::declval()))
~~~~ ^~~~~~~~~~~~~~~~~~~~
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../include/c++/5.4.0/debug/array:264:23:注意:在
此处请求“交换”
noexcept(noexcept(_-one.swap(_-two)))
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/bits/stl_algobase.h:148:7:注意:在异常的实例化中
此处要求的“交换”规范
掉期(*uuuuu a,*\uuuuu b);
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/bits/stl_algo.h:84:11:注意:在函数模板的实例化中
此处请求专门化“std::iter\U交换”
标准:国际热核实验堆交换(结果,b);
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/bits/stl_algo.h:1916:12:注意:在函数模板的实例化中
专业化“标准::u移动_中间值_至_优先”
在此请求
标准::uuu移动中位数到第一(uu第一,uu第一+1,uu中间,uu最后-1,
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/bits/stl_algo.h:1948:11:注意:在函数模板的实例化中
专门化'std::__无防护_分区_轴'
在此请求
标准::uuu无防护分区u轴(uuu第一,最后,复合);
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/bits/stl_algo.h:1963:9:注意:在函数模板的实例化中
此处请求专门化“std::_introsort_loop”
std::\uuuuu introsort\u循环(\uuuuu first,\uuuuu last,
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../../include/c++/5.4.0/bits/stl_algo.h:4698:12:注意:在函数模板的实例化中
专门化“标准::u排序”
在此请求
std::uuu排序(uuu first,_uuulast,_uuugnu cxx::uuu ops::uuu iter less iter());
^
test.cpp:40:10:注意:在函数模板专门化的实例化中
此处请求“std::sort”
排序(membervariable.begin(),membervariable.end());
^
/usr/bin/./lib/gcc/x86_64-linux-gnu/5.4.0/../../../../../../../include/c++/5.4.0/debug/array:84:7:注意:此处声明了“交换”
无效的
^
生成1个错误。
makefile:6:目标“test.o”的配方失败
make:**[test.o]错误1
$clang++--版本
clang版本3.8.0-2ubuntu4(标签/发行版/最终版)
目标:x86_64-pc-linux-gnu
线程模型:posix
已安装dir:/usr/bin
$
问题中显示的错误消息与GCC的标准库完全实现的标准()中的错误有关。GCC的编译器没有用生成的代码诊断问题(否则该错误早就被发现了),但您使用的clang版本确实如此。IIRC clang后来添加了一个特殊的hack,允许编译此特定用法,因此“修复”是使用较新版本的GCC或较新版本的Clang或两者都使用。您的代码太长了。只显示重要的部分。请也粘贴GCC错误消息。Clang的错误消息与您的代码无关。感谢您提供了这一有用的答案。我尝试使用Clang-6.0和(IIRC)编译代码gcc-7.2,生成错误消息。可以给我一个示例编译器(版本)哪一个编译了代码?我只是在讨论您发布的错误消息所揭示的特定问题。我不能讨论我没有看到的错误消息。尽管如此,一个明显的潜在问题是您没有包含
。我没有看到任何其他错误消息(在上面的示例中)和生成对象文件。
一定是在编写过程中丢失的。我想知道是否有人阅读了这篇文章,可以在自己的系统中将上面的代码片段生成一个对象文件。尽管如此,在最近的一段代码中添加-fno expetions
似乎可以避免编译错误。
$ make
clang++ -O0 -g -std=c++11 -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -pedantic -Wall -Wextra -Wno-unused-variable -Wno-sign-compare -Wno-missing-braces -Wmissing-field-initializers -Werror=implicit test.cpp -c -o test.o
In file included from test.cpp:3:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/map:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_map.h:63:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/tuple:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/array:338:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:86:52: error: too many arguments to function call, expected
single argument '__other', have 2 arguments
noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
~~~~ ^~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:264:23: note: in instantiation of exception specification for
'swap' requested here
noexcept(noexcept(__one.swap(__two)))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algobase.h:148:7: note: in instantiation of exception
specification for 'swap<int, 2>' requested here
swap(*__a, *__b);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:84:11: note: in instantiation of function template
specialization 'std::iter_swap<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *, std::__cxx1998::vector<std::__debug::array<int, 2>,
std::allocator<std::__debug::array<int, 2> > > >, std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int,
2> > > > >' requested here
std::iter_swap(__result, __b);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1916:12: note: in instantiation of function template
specialization 'std::__move_median_to_first<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
requested here
std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1948:11: note: in instantiation of function template
specialization 'std::__unguarded_partition_pivot<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
requested here
std::__unguarded_partition_pivot(__first, __last, __comp);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1963:9: note: in instantiation of function template
specialization 'std::__introsort_loop<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, long,
__gnu_cxx::__ops::_Iter_less_iter>' requested here
std::__introsort_loop(__first, __last,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:4698:12: note: in instantiation of function template
specialization 'std::__sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
requested here
std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
^
test.cpp:40:10: note: in instantiation of function template specialization
'std::sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > > >' requested here
std::sort( membervariable.begin(), membervariable.end() );
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:84:7: note: 'swap' declared here
void
^
1 error generated.
makefile:6: recipe for target 'test.o' failed
make: *** [test.o] Error 1
$ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$