2016-03-13 21 views
7

我正在阅读Bjarne Stroustrup的“The C++ Programming Language”第4版。在本书的第一部分,我发现using的使用看起来像以下:如何使用模板在C++中使用`(使参数化别名)进行别名?

// Error is in these 2 lines 
template<typename T> 
using Iterator<T> = typename T::iterator; 

*参见[**]为完整的程序和错误消息*

这正是我在105页面发现当我把这个变成一个完整的程序,并试图编译它,g++给了我这个错误masseage:

> g++ -std=c++14 -o fnd find_all.cpp 
find_all.cpp:13:15: error: expected '=' before '<' token 
using Iterator<T> = typename T::iterator; 
     ^
find_all.cpp:13:15: error: expected type-specifier before '<' token 

我不能鳍d在这段代码中的任何问题,(我是新的C++,我无法找到我的小知识问题)(更多容易混淆的,我发现这个上了Bjarne的书)

有人能告诉我为什么该代码是否会产生错误?

注意:但是,如果我用typename C::iterator替换Iterator<C>(见下文),它工作正常,没有错误!

[**]完整的计划和错误消息:

// Error is in these 2 lines 
template<typename T> 
using Iterator<T> = typename T::iterator; 
// ------------------------------------------- 

// For the completeness I'll include my complete program here 
template<typename C, typename V> 
vector<Iterator<C>> find_all(C& c, V v) // find all occurrences of v in c 
{ 
    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"; 

    list<double> ld { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 1.1, 1.1 }; 
    for (auto p : find_all(ld, 1.1)) 
     if (*p == 1.1) 
      cerr << "list bug!\n"; 

    vector<string> strv { "blue", "yellow", "red", "white", "orange", "blue" }; 
    for (auto p : find_all(strv, "blue")) 
     if (*p == "blue") 
      cerr << "string vector bug!\n"; 

} 

int main(void) 
{ 
    test(); 

    return 0; 
} 

错误消息:

> g++ -std=c++14 -o fnd find_all.cpp 
find_all.cpp:13:15: error: expected '=' before '<' token 
using Iterator<T> = typename T::iterator; 
     ^
find_all.cpp:13:15: error: expected type-specifier before '<' token 
find_all.cpp:16:8: error: 'Iterator' was not declared in this scope 
vector<Iterator<C>> find_all(C& c, V v) 
    ^
find_all.cpp:16:17: error: template argument 1 is invalid 
vector<Iterator<C>> find_all(C& c, V v) 
      ^
find_all.cpp:16:17: error: template argument 2 is invalid 
find_all.cpp:16:18: error: expected unqualified-id before '>' token 
vector<Iterator<C>> find_all(C& c, V v) 
      ^
find_all.cpp: In function 'void test()': 
find_all.cpp:30:31: error: 'find_all' was not declared in this scope 
    for (auto p : find_all(m, 'a')) 
         ^
find_all.cpp:35:32: error: 'find_all' was not declared in this scope 
    for (auto p : find_all(ld, 1.1)) 
          ^
find_all.cpp:40:37: error: 'find_all' was not declared in this scope 
    for (auto p : find_all(strv, "blue")) 

回答

12

首先<T>必须省略

template<typename T> 
using Iterator = typename T::iterator; 
+1

非常感谢你,现在工作得很好。这本书似乎是一个错字,谢谢 – 0xEDD1E

6

当你定义一个类模板或功能模板,你SE:

template <typename T> struct Foo { }; 

template <typename T> T bar() { return T{}; } 

你不Foo<T>bar<T>定义模板时使用。

同样,使用模板时,定义一个别名,你需要使用:

template <typename T> 
using Iterator = typename T::iterator; 
      ^^ Don't include <T>