从vector继承构造函数

时间:2015-03-30 09:33:23

标签: c++ vector constructor using-directives

在Stroustrup,C ++编程语言第4版中,他在第79页写了以下代码:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <allocators>
using std::vector;
using namespace std;
template<class T>
class Vector : vector<T> 
{
private:
    T elem[50];
    int size;
public:
    ***using vector<T>::vector; // inherit constructors***
    T& operator[](size_type i) { check(i); return this−>elem(i); }
    const T& operator=(size_type i) const {
        check(i); 
        return this−>elem(i);
    }
    void check(size_type i) { if (this−>size()<i) throw Bad_index(i); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Vector <int> V{ 1, 2, 3, 4 };
    return 0;
}

编译程序时,收到以下错误:

  

错误3错误C2440:'初始化':无法转换   'initializer-list'到'Vector'c:\ computer programming \ c ++   程序设计   language \ code \ ch3 \ consoleapplication3 \ consoleapplication3 \ consoleapplication3.cpp 28 1 ConsoleApplication3

     

错误1错误C2886:'vector&gt;' :符号不能   在成员使用声明c:\ computer programming \ c ++中使用   程序设计   language \ code \ ch3 \ consoleapplication3 \ consoleapplication3 \ consoleapplication3.cpp 17 1 ConsoleApplication3

     

错误2错误C2886:'vector&gt;' :符号不能   在成员使用声明c:\ computer programming \ c ++中使用   程序设计   language \ code \ ch3 \ consoleapplication3 \ consoleapplication3 \ consoleapplication3.cpp 17 1 ConsoleApplication3     4智能感知:没有构造函数的实例“Vector :: Vector [with   T = int]“匹配参数列表               参数类型是:(int,int,int,int)c:\ Computer Programming \ C ++ Programming   Language \ Code \ Ch3 \ ConsoleApplication3 \ ConsoleApplication3 \ ConsoleApplication3.cpp 28 16 ConsoleApplication3

我的问题主要涉及错误2错误C2886:它指的是using指令。 Visual Basic将错误定义如下:

  

'class :: identifier':符号不能在成员using-declaration中使用   using声明使用符号,例如命名空间名称。 using声明用于声明基类成员。

Stroustrup显然使用它,但我没有复制他的方法。是否有我缺少的标题或什么?有人可以解释我的错误吗?谢谢jmg。

0 个答案:

没有答案
相关问题