在Bjarne Stroustrup的C 书中,这个示例有什么问题
what is the wrong with this example in the c++ book of Bjarne Stroustrup
我尝试了Bjarne Stroustrup的C 编程语言第四版中的示例:
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
template<typename T>
using Iterator<T> = typename T::iterator;
template<typename C, typename V>
vector<Iterator<C>> find_all(C& c, V v)
{
vector<Iterator<C>> res;
for (auto p = c.begin(); p!=c.end(); ++p)
if (*p==v)
res.push_back(p);
return res;
}
void test()
{
string m {"Mary had a little lamb"};
for (auto p : find_all(m,'a'))
if (*p!='a')
cerr << "string bug!n";
// p is a str ing::iterator
list<double> ld {1.1, 2.2, 3.3, 1.1};
for (auto p : find_all(ld,1.1))
if (*p!=1.1)
cerr << "list bug!n";
vector<string> vs { "red", "blue", "green", "green", "orange", "green" };
for (auto p : find_all(vs,"green"))
if (*p!="green")
cerr << "vector bug!n";
for (auto p : find_all(vs,"green"))
*p = "ver t";
}
int main()
{
test();
return 0;
}
§4.5.1
我将代码编译为:g test.cpp -o test -g -std = c 11上的ubuntu,但我在下面得到了错误:
iterator_of_iterator_version_2.cpp:9:15: error: expected ‘=’ before ‘<’ token
using Iterator<T> = typename T::iterator;
^
iterator_of_iterator_version_2.cpp:9:15: error: expected type-specifier before ‘<’ token
iterator_of_iterator_version_2.cpp:12:8: error: ‘Iterator’ was not declared in this scope
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp:12:17: error: template argument 1 is invalid
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp:12:17: error: template argument 2 is invalid
iterator_of_iterator_version_2.cpp:12:18: error: expected unqualified-id before ‘>’ token
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp: In function ‘void test()’:
iterator_of_iterator_version_2.cpp:24:30: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(m,'a'))
^
iterator_of_iterator_version_2.cpp:29:31: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(ld,1.1))
^
iterator_of_iterator_version_2.cpp:33:35: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(vs,"green"))
^
iterator_of_iterator_version_2.cpp:36:35: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(vs,"green"))
^
那是什么问题?
似乎在此代码中找不到语法错误,因为我只是对本书的示例进行了复制。
从
更改起点template<typename T>
using Iterator<T> = typename T::iterator;
to
template<typename T>
using Iterator = typename T::iterator;
在我的Ubuntu 16.04上使用相同的编译器设置
为什么这样?我对此并不是100%的信心,其他人可能会验证它。
写作
using Iterator<T>
是无效的,因为这里没有意义。我们希望迭代器成为模板的Typedef,它将其参数类型询问其通用迭代器类型。 Iterator<T>
将专门化模板。例如。我们知道特定类型的更好:
template<>
using Iterator<MyClass> = MyClassIterator;
至少与普通模板类配合使用,我认为使用也一样。
更改此
template<typename T>
using Iterator<T> = typename T::iterator;
到这个
template<typename T>
using Iterator = typename T::iterator;
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- c++无值sort()的问题是什么?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 什么是钻石问题?是一系列问题还是特定问题?
- 格式说明符C++有什么问题
- 我应该在 main 函数中写什么来测试我的问题?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 方法问题 - 什么会改变值,什么不会改变?什么是无效的?