gpt4 book ai didi

c++ - 模板、嵌套类和 "expected constructor, destructor, or conversion before ' &' token"

转载 作者:太空狗 更新时间:2023-10-29 19:40:04 25 4
gpt4 key购买 nike

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

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'

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

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

最佳答案

我认为您缺少 typename 关键字。

例如

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

应该是

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

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

关于c++ - 模板、嵌套类和 "expected constructor, destructor, or conversion before ' &' token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1725147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com