模板、嵌套类和“预期的构造函数、析构函数或转换”之前令牌”

发布于 2024-08-11 04:40:54 字数 3202 浏览 4 评论 0原文

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

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_

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_);
}

如何使这些错误消失?这些文件对我来说很有意义,但可惜的是,重要的是编译器的说法。

While working with some templates and writing myself a basic container class with iterators, I found myself needing to move the body of member functions from a template class into a separate file to conform to style guidelines. However, I've run into an interesting compile error:

runtimearray.cpp:17: error: expected
constructor, destructor, or type
conversion before '&' token
runtimearray.cpp:24: error: expected
constructor, destructor, or type
conversion before '&' token
runtimearray.cpp:32: error: expected
constructor, destructor, or type
conversion before '&' token
runtimearray.cpp:39: error: expected
constructor, destructor, or type
conversion before '&' token
runtimearray.cpp:85: error: expected
constructor, destructor, or type
conversion before 'RuntimeArray'
runtimearray.cpp:91: error: expected
constructor, destructor, or type
conversion before '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_

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_);
}

How can I make these errors go away? The files make sense to me, but alas, it's the compiler's say that matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

王权女流氓 2024-08-18 04:40:54

我认为您缺少 typename 关键字。

例如,

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

应该是

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

依赖于模板参数的“嵌套”类型,需要 typename 关键字告诉编译器它们应该是类型,否则会产生歧义。

I think that you are missing the typename keyword.

e.g.

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

should be

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

'Nested' types which are dependent on a template parameter need the typename keyword to tell the compiler that they should be types where this otherwise would be ambiguous.

失与倦" 2024-08-18 04:40:54

这是一个非常有趣的风格指南。一般来说,模板函数的定义必须位于头文件中。这是几个小时前发生的:分割将模板化的 C++ 类放入 .hpp/.cpp 文件中——可能吗?

That is one heck of a funny style guideline. In general, definitions of template functions have to be in the header file. This came by just a few hours ago: Splitting templated C++ classes into .hpp/.cpp files--is it possible?

嘿哥们儿 2024-08-18 04:40:54

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

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

This will not work the way you want it to. All your function declarations and definitions must appear in the .h file in which you defined RuntimeArray. The error you're seeing may be something else, perhaps a typename thing, but even if you can make RunTimeArray.cpp compile in isolation no one will be able to use it.

If you really must have the definitions in a separate file, #include it at the end of runtimearray.h

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文