【发布时间】:2018-07-30 08:06:45
【问题描述】:
我无法指定使用来自 stl 的旋转,而不是来自 boost。我该怎么做?
我的整个源码可以看下面,是插入排序方法的简单代码
#include <algorithm> // std::rotate
#include <vector>
// Function to sort the array
struct _ItemCompare {...} ItemCompare;
template<class T>
class Sorters {
public:
void insertionSort(std::vector<T> &vec, unsigned int size) {
for (auto it = vec.begin(); it != vec.begin() + size; it++) {
auto const insertion_point = std::upper_bound(vec.begin(), it, *it, ItemCompare);
std::rotate(insertion_point, it, it + 1);
}
}
};
可以看到错误轨迹跟踪如下代码:
In file included from src/balanced_intercalation_multipath.cpp:10:0,
from src/balanced_intercalation_multipath.h:29,
from main.cpp:7:
src/sorters.h: In member function ‘void Sorters<T>::insertionSort(std::vector<T>&, unsigned int)’:
src/sorters.h:29:19: error: ‘it’ does not name a type
for (auto it = vec.begin(); it != vec.begin() + size; it++) {
^
src/sorters.h:29:37: error: expected ‘;’ before ‘it’
for (auto it = vec.begin(); it != vec.begin() + size; it++) {
^
src/sorters.h:29:37: error: ‘it’ was not declared in this scope
In file included from src/balanced_intercalation_multipath.h:29:0,
from main.cpp:7:
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from main.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h:335:18: note: synthesized method ‘Item& Item::operator=(const Item&)’ first required here
*__result = *__first;
^
src/sorters.h: In member function ‘void Sorters<T>::insertionSort(std::vector<T>&, unsigned int)’:
src/sorters.h:29:19: error: ‘it’ does not name a type
for (auto it = vec.begin(); it != vec.begin() + size; it++) {
^
src/sorters.h:29:37: error: expected ‘;’ before ‘it’
for (auto it = vec.begin(); it != vec.begin() + size; it++) {
^
src/sorters.h:29:37: error: ‘it’ was not declared in this scope
EDIT1:把我的整个代码和错误 我用
编译-lboost_serialization -std=c++11
【问题讨论】:
-
文档:(en.cppreference.com/w/cpp/algorithm/rotate)。请发布一个完整示例供读者尝试。
-
您可能包含了错误的头文件。附言包括整个错误消息。
-
这只是错误信息的一部分;还有一部分使用了“错误”这个词,它告诉你编译器在抱怨什么。这部分会告诉您在哪里发现错误时正在查找。
-
对不起@PeteBecker,我无法理解错误,现在我把错误放了
-
您现在有一个不同的问题(即使您仍在尝试解决相同的问题)。看起来您的编译器没有将关键字
auto视为 C++11。您的编译器太旧,或者您没有将-std=c++11开关发送给编译器:也许您拼写错误(破折号的数量很重要)或者您将它提供给了链接器而不是编译器。或者可能将其添加到 CFLAGS 但编译器命令行不使用它们。