Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板、嵌套类和;在'之前需要构造函数、析构函数或转换&';代币;_C++_Templates_Nested Class - Fatal编程技术网

C++ 模板、嵌套类和;在'之前需要构造函数、析构函数或转换&';代币;

C++ 模板、嵌套类和;在'之前需要构造函数、析构函数或转换&';代币;,c++,templates,nested-class,C++,Templates,Nested Class,在使用一些模板并使用迭代器编写基本容器类时,我发现自己需要将成员函数体从模板类移动到单独的文件中,以符合样式指南。但是,我遇到了一个有趣的编译错误: runtimearray.cpp:17:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:24:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:32:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:39:错误:应为

在使用一些模板并使用迭代器编写基本容器类时,我发现自己需要将成员函数体从模板类移动到单独的文件中,以符合样式指南。但是,我遇到了一个有趣的编译错误:

runtimearray.cpp:17:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:24:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:32:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:39:错误:应为 构造函数、析构函数或类型 “&”标记之前的转换 runtimearray.cpp:85:错误:应为 构造函数、析构函数或类型 “RuntimeArray”之前的转换 runtimearray.cpp:91:错误:应为 构造函数、析构函数或类型 “RuntimeArray”之前的转换

runtimearray.h:

#ifndef RUNTIMEARRAY_H_
#define RUNTIMEARRAY_H_

template<typename T>
class RuntimeArray
{
 public:
  class Iterator
  {
    friend class RuntimeArray;
   public:
    Iterator(const Iterator& other);

    T& operator*();
    Iterator& operator++();
    Iterator& operator++(int);
    Iterator& operator--();
    Iterator& operator--(int);
    bool operator==(Iterator other);
    bool operator!=(Iterator other);

   private:
    Iterator(T* location);

    T* value_;
  };

  RuntimeArray(int size);
  ~RuntimeArray();

  T& operator[](int index);

  Iterator Begin();
  Iterator End();

 private:
  int size_;
  T* contents_;
};

#endif  // RUNTIMEARRAY_H_
#ifndef运行时数组_
#定义运行时数组_
模板
类运行时数组
{
公众:
类迭代器
{
友元类运行时数组;
公众:
迭代器(常量迭代器和其他);
T&算子*();
迭代器和运算符++();
迭代器和运算符++(int);
迭代器和运算符——();
迭代器和运算符--(int);
布尔运算符==(迭代器其他);
布尔运算符!=(迭代器其他);
私人:
迭代器(T*位置);
T*值u;
};
运行时数组(int-size);
~RuntimeArray();
T&运算符[](整数索引);
迭代器Begin();
迭代器End();
私人:
int-size_389;;
T*内容uu;
};
#endif//RUNTIMEARRAY\u H_
runtimearray.cpp:

#include "runtimearray.h"

template<typename T>
RuntimeArray<T>::Iterator::Iterator(const Iterator& other)
    : value_(other.value_)
{
}

template<typename T>
T& RuntimeArray<T>::Iterator::operator*()
{
  return *value_;
}

template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++()
{
  ++value_;
  return *this;
}

template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++(int)
{
  Iterator old = *this;
  ++value_;
  return old;
}

template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator--()
{
  --value_;
  return *this;
}

template<typename T>
RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator--(int)
{
  Iterator old = *this;
  --value_;
  return old;
}

template<typename T>
bool RuntimeArray<T>::Iterator::operator==(Iterator other)
{
  return value_ == other.value_;
}

template<typename T>
bool RuntimeArray<T>::Iterator::operator!=(Iterator other)
{
  return value_ != other.value_;
}

template<typename T>
RuntimeArray<T>::Iterator::Iterator(T* location)
    : value_(location)
{
}

template<typename T>
RuntimeArray<T>::RuntimeArray(int size)
    : size_(size),
      contents_(new T[size])
{
}

template<typename T>
RuntimeArray<T>::~RuntimeArray()
{
  if(contents_)
    delete[] contents_;
}

template<typename T>
T& RuntimeArray<T>::operator[](int index)
{
  return contents_[index];
}

template<typename T>
RuntimeArray<T>::Iterator RuntimeArray<T>::Begin()
{
  return Iterator(contents_);
}

template<typename T>
RuntimeArray<T>::Iterator RuntimeArray<T>::End()
{
  return Iterator(contents_ + size_);
}
#包括“runtimearray.h”
模板
RuntimeArray::Iterator::Iterator(常量迭代器和其他)
:value_u2;(其他.value_2;)
{
}
模板
运行时数组::迭代器::运算符*()
{
返回*值;
}
模板
RuntimeArray::Iterator&RuntimeArray::Iterator::operator++()
{
++价值观;
归还*这个;
}
模板
RuntimeArray::Iterator&RuntimeArray::Iterator::operator++(int)
{
迭代器old=*this;
++价值观;
返老还童;
}
模板
RuntimeArray::Iterator&RuntimeArray::Iterator::operator--()
{
--价值观;
归还*这个;
}
模板
RuntimeArray::Iterator&RuntimeArray::Iterator::operator--(int)
{
迭代器old=*this;
--价值观;
返老还童;
}
模板
布尔运行时数组::迭代器::运算符==(迭代器其他)
{
返回值==其他.value;
}
模板
布尔运行时数组::迭代器::运算符=(迭代器其他)
{
返回值\!=其他值\;
}
模板
RuntimeArray::Iterator::Iterator(T*位置)
:值(位置)
{
}
模板
RuntimeArray::RuntimeArray(整数大小)
:尺寸(尺寸),
内容(新的[尺寸])
{
}
模板
RuntimeArray::~RuntimeArray()
{
如果(内容)
删除[]项内容;
}
模板
T&RuntimeArray::运算符[](int索引)
{
返回内容[索引];
}
模板
RuntimeArray::迭代器RuntimeArray::Begin()
{
返回迭代器(contents);
}
模板
RuntimeArray::Iterator RuntimeArray::End()
{
返回迭代器(内容+大小);
}

我怎样才能消除这些错误?这些文件对我来说很有意义,但唉,重要的是编译器的说法。

这是一个非常有趣的风格指南。通常,模板函数的定义必须在头文件中。这是几个小时前发来的:

我认为您缺少了
typename
关键字

e、 g

模板
RuntimeArray::Iterator&RuntimeArray::Iterator::operator++()
应该是

template<typename T>
typename RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++()
模板
typename RuntimeArray::Iterator&RuntimeArray::Iterator::operator++()

依赖于模板参数的“嵌套”类型需要
typename
关键字来告诉编译器,它们应该是不明确的类型。

这将无法按您希望的方式工作。所有函数声明和定义都必须出现在定义RuntimeArray的.h文件中。您看到的错误可能是其他错误,可能是typename错误,但即使您可以使RunTimeArray.cpp单独编译,也没有人能够使用它


如果您真的必须将定义放在单独的文件中,
#在runtimearray.h的末尾包含它。这是为大学准备的,我不同意大多数风格指南的一半。然而,要意识到这不是作业的一部分,而是我为解决作业所做的一些事情。我们甚至还没有找到指针、模板甚至类。在Charles Bailey修复之后,包括.cpp文件允许客户端程序工作。这不是我要做的方式,(见鬼,据我所知,标准库和Boost使用的是唯一的头方法),但这是老师想要的方式。你可能想向老师指出,这些类型的“外部模板”是非标准的,只有少数编译器支持(有些编译器不支持).外部模板?我将类的声明和定义都包含在使用文件中。这看起来就像是我在一个文件中对自己做的一样。第一个文件怎么会模棱两可?我没有看到。在编译器到达
Iterator
时,它不知道
t
将是什么,并且
Iterator
依赖于
RuntimeArray
模板。
Iterator
可能会为某些专门化命名成员函数或数据成员。为了正确解析声明,需要告诉编译器它是一个带有
typename
关键字的类型。因为您的类型是一个嵌套类,所以不象在其他一些示例中那样清楚为什么需要这个规则。
template<typename T>
typename RuntimeArray<T>::Iterator& RuntimeArray<T>::Iterator::operator++()