G++ 4.6 抱怨迭代器差异类型。g++ 4.4 和 Visual Studio 没有
g++ 4.6 complains about iterator difference type. g++ 4.4 and Visual Studio don't
当我尝试编译下面给出的示例代码时,我在g++(版本4.6)中得到以下错误:
~/tmp/iterator_tmp$ g++ -ansi -Wall iteg.cpp -o iteg
iteg.cpp:115:11: error: ‘ptrdiff_t’ does not name a type
iteg.cpp: In instantiation of ‘CMyItr<CMyBuff<double>, double>’:
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: instantiated from ‘std::iterator_traits<CMyItr<CMyBuff<double>, double> >’
/usr/include/c++/4.6/bits/stl_iterator.h:97:11: instantiated from ‘std::reverse_iterator<CMyItr<CMyBuff<double>, double> >’
iteg.cpp:204:40: instantiated from here
iteg.cpp:21:46: error: no type named ‘difference_type’ in ‘CMyItr<CMyBuff<double>, double>::BuffType {aka class CMyBuff<double>}’
这个例子在g++ 4.4中编译得很好,在Visual Studio 2010中也是如此。谁能指出我哪里做错了吗?
我在g++ 4.6和4.4两种情况下使用的两个命令如下:
g++ -ansi -Wall iteg.cpp -o iteg
g++-4.4 -ansi -Wall iteg.cpp -o iteg
谢谢。
代码示例:
//---------------------------------------------------------------------------//
//---------------------------------------------------------------------------//
#include <memory>
#include <iostream>
#include <iterator>
template < typename T, typename elem_type=typename T::value_type>
class CMyItr {
public:
typedef T BuffType;
typedef CMyItr<T> self_type;
typedef CMyItr<self_type, elem_type> iterator;
typedef typename std::bidirectional_iterator_tag iterator_category;
typedef typename BuffType::value_type value_type;
typedef typename BuffType::size_type size_type;
typedef typename BuffType::pointer pointer;
typedef typename BuffType::const_pointer const_pointer;
typedef typename BuffType::reference reference;
typedef typename BuffType::const_reference const_reference;
typedef typename BuffType::difference_type difference_type;
CMyItr( BuffType *pB, size_type pos):
PtrItr_(pB), PtrPos_(pos){
};
friend class CMyItr< const T, const elem_type>;
elem_type &operator*(){
return (*PtrItr_)[PtrPos_];
};
elem_type *operator->(){
return &(operator*());
};
self_type & operator++(){
++PtrPos_;
return *this;
};
self_type operator++(int){
self_type tmp(*this);
++(*this);
return tmp;
};
self_type operator+(difference_type n) {
self_type tmp(*this);
tmp.PtrPos_ = tmp.PtrPos_ + n;
return tmp;
};
self_type &operator+=(difference_type n){
PtrPos_ = PtrPos_ + n;
return *this;
};
self_type & operator--(){
--PtrPos_;
return *this;
};
/*!
The decrement operator which decrements the position index.
*/
self_type operator--(int){
self_type tmp(*this);
--(*this);
return tmp;
};
self_type operator-(difference_type n) {
self_type tmp(*this);
tmp.PtrPos_ = tmp.PtrPos_ - n;
return tmp;
};
self_type &operator-=(difference_type n){
PtrPos_ -= n;
return *this;
};
bool operator!=(const self_type &other) const {
return PtrPos_ != other.PtrPos_ && PtrItr_ == other.PtrItr_;
};
bool operator==(const self_type &other) const {
return PtrPos_ == other.PtrPos_ && PtrItr_ == other.PtrItr_;
};
private:
BuffType * PtrItr_;
size_type PtrPos_;
};
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
template < typename T > class CMyBuff {
public:
enum {default_size = 4 };
typedef CMyBuff<T> self_type;
typedef T value_type;
typedef T & reference;
typedef const T & const_reference;
typedef T * pointer;
typedef const T * const_pointer;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef CMyItr<self_type> iterator;
typedef CMyItr<const self_type> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const iterator> const_reverse_iterator;
/*! Starting for forward iterator.*/
iterator begin(){
return iterator(this, 0);
};
/*! Forward iterator should go till here.*/
iterator end(){
return iterator(this, Size_);
};
/*! Starting for constant forward iterator.*/
const_iterator begin() const {
return const_iterator(this, 0);
};
/*! Constant forward iterator should go till here.*/
const_iterator end() const {
return const_iterator(this, Size_);
};
/*! Reverse iterator starts from here.*/
reverse_iterator rbegin(){
return reverse_iterator(end());
}
/*! Reverse iterator end.*/
reverse_iterator rend() {
return reverse_iterator(begin());
}
/*! Constant reverse iterator starting point.*/
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
/*! Constant reverse iterator should end here.*/
const_reverse_iterator rend() const {
return const_reverse_iterator( begin());
}
/* Ctor for my buffer*/
explicit CMyBuff(size_type capacity = default_size):
Ptr_(NULL),
Size_(capacity) {
Ptr_ = new value_type [sizeof(value_type) * Size_];
Ptr_[0] = 0;
Ptr_[1] = 1;
Ptr_[2] = 8;
Ptr_[3] = 27;
};
~CMyBuff() {
delete [] Ptr_;
}
reference operator[](size_type i){
return rAtUnChecked(i);
};
const_reference operator[](size_type i) const {
return rAtUnChecked(i);
};
size_type size() const {
return Size_;
};
reference rAtUnChecked(size_type k) const {
return Ptr_[k];
};
private:
pointer Ptr_;
size_type Size_;
};
//------------------------------------------------------------------//
//----------------------------------------- MAIN ------------------//
// Use the following command line to compile:
// g++-4.4 -ansi -Wall -std=c++0x mybuffer.cpp -o mybuffer
int main(){
CMyBuff<double> Buffer;
CMyBuff < double >::reverse_iterator RItr = Buffer.rbegin();
//prints last but one element
std::cout << *(++RItr) << std::endl;
return 0;
}
//---------------------------------------------------------------------------//
ptrdiff_t
在<stddef.h>
中定义。将#include <stddef.h>
添加到您的代码顶部,您将会很好。
在c++中,系统头文件可以包含其他系统头文件,但不是必需的。
更新:#include <cstddef>
也会工作,但你必须使用std::ptrdiff_t
。
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何在MS Visual Studio 2019中运行QT UI
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio 2019:插入多个C++风格的单行注释
- Visual Studio Code "undefined reference to `WinMain@16'"
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- Visual Studio中的函数声明和函数定义问题
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual Studio mkl_link_tool.exe链接错误
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 不同的Visual Studio版本中缺少.dll
- 用Visual Studio在C++中嵌入Julia
- Visual Studio 2017循环自动向量化问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- Visual Studio在尝试读取resource.txt文件时崩溃
- Visual Studio 2017 不允许我创建 C++ 专用模板