3

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

runtimearray.cpp:17: 错误: '&' 标记之前的预期构造函数、析构函数或类型转换 runtimearray.cpp:24: 错误: '&' 标记之前的预期构造函数、析构函数或类型转换 runtimearray.cpp:32: 错误: '&' 标记之前的预期构造函数、析构函数或类型转换 runtimearray.cpp:39:错误:'&' 标记之前的预期构造函数、析构函数或类型转换 runtimearray.cpp:85:错误:预期的构造函数、析构函数或类型转换在“RuntimeArray”之前 runtimearray.cpp:91:错误:预期的构造函数、析构函数或“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_

运行时数组.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_);
}

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

4

3 回答 3

12

我认为您缺少typename关键字。

例如

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

应该

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

依赖于模板参数的“嵌套”类型需要typename关键字来告诉编译器它们应该是类型,否则这将是模棱两可的。

于 2009-11-12T20:34:35.943 回答
2

这是一个有趣的风格指南。一般来说,模板函数的定义必须在头文件中。这是几个小时前才出现的:将模板化的 C++ 类拆分为 .hpp/.cpp 文件——这可能吗?

于 2009-11-12T20:33:26.997 回答
2

这不会按您希望的方式工作。所有函数声明和定义都必须出现在定义 RuntimeArray 的 .h 文件中。您看到的错误可能是别的东西,也许是类型名的东西,但是即使您可以使 RunTimeArray.cpp 单独编译,也没有人能够使用它。

如果您确实必须将定义放在单独的文件中,#include请将其放在 runtimearray.h 的末尾

于 2009-11-12T20:37:30.807 回答