模板、嵌套类和“预期的构造函数、析构函数或转换”之前令牌”
在使用一些模板并使用迭代器编写自己的基本容器类时,我发现自己需要将成员函数的主体从模板类移动到单独的文件中以符合样式指南。但是,我遇到了一个有趣的编译错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您缺少
typename
关键字。例如,
应该是
依赖于模板参数的“嵌套”类型,需要
typename
关键字告诉编译器它们应该是类型,否则会产生歧义。I think that you are missing the
typename
keyword.e.g.
should be
'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.这是一个非常有趣的风格指南。一般来说,模板函数的定义必须位于头文件中。这是几个小时前发生的:分割将模板化的 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?
这不会按您希望的方式工作。所有函数声明和定义都必须出现在定义 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