下标运算符使用向量重载 [英] Subscript operator overloading with vectors

查看:92
本文介绍了下标运算符使用向量重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对C ++并不陌生,但我之前从未做过运算符

重载。

我有一些旧的代码试图实现移位寄存器 - 但我

似乎无法让它工作。这是一个更简单的版本。


-------------------- main.cpp ------ ---------------------


#include< iostream>

#include< ; vector>

#include< cassert>


class ShiftRegister {

public:

ShiftRegister(无符号大小):_reg(大小){


}


bool& operator [](unsigned ix){


返回_reg [ix];

}


const bool& operator [](unsigned ix)const {


return _reg [ix];

}


private:

std :: vector< bool> _reg;

};

int main(int argc,char * argv []){


ShiftRegister sftreg(2) ;


}

------------------ main.cpp的结束----- -----------


上面包含下标运算符的两个定义:

普通和一个常量。编译我得到

1.对于普通或变量下标运算符

错误:无法转换`std :: vector< bool,

_Alloc> :: operator [](unsigned int)[with _Alloc =

std :: allocator< bool>](ix)''

to`bool&''


2.对于常数下标运算符

警告:返回临时引用


我想我错过了这里的东西,但看书或在互联网上没有让我理解我做错了什么,即他们

似乎在做同样的事情(但是有数组)或指针 - 而不是

向量。)


我希望有人对如何使其工作有一些想法。

非常感谢。

OO

Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here''s a simpler version of it.

-------------------- main.cpp---------------------------

# include <iostream>
# include <vector>
# include <cassert>

class ShiftRegister {
public:
ShiftRegister(unsigned size) : _reg(size) {

}

bool& operator[](unsigned ix) {

return _reg[ix];
}

const bool& operator[](unsigned ix) const {

return _reg[ix];
}

private:
std::vector<bool> _reg;
};
int main(int argc, char* argv[]) {

ShiftRegister sftreg(2);

}
------------------ end of main.cpp ----------------

The above contains two definitions of the subscript operator one
ordinary and one constant. Compiling this I get
1. For the ordinary or variable subscript operator
error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](ix)''
to `bool&''

2. For the constant subscript operator
warning: returning reference to temporary

I think I am missing something here, but looking in books or on the
internet does not make me understand what I am doing wrong i.e. they
seem to be doing the same thing (but with arrays or pointers - not with
vectors.)

I hope someone has some ideas on how to get this to work.
Thanks a lot.
O.O.

推荐答案

ol ******* @ yahoo.it 写道:


我不是全新的到C ++,但我有从来没有做过运算符
之前的重载。
我有一些旧的代码试图实现移位寄存器 - 但我似乎无法让它工作。这是一个更简单的版本。

-------------------- main.cpp ---------- -----------------

#include< iostream>
#include< vector>
#include< cassert> ;

类ShiftRegister {
公开:
ShiftRegister(无符号大小):_reg(size){

}

布尔和放大器; operator [](unsigned ix){

return _reg [ix];
}

const bool& operator [](unsigned ix)const {

返回_reg [ix];
}
私人:
std :: vector< bool> _reg;
};

int main(int argc,char * argv []){

ShiftRegister sftreg(2);
}

------------------ main.cpp结束---------------- <上面包含下标运算符的两个定义,一个是普通的,一个是常量。编译我得到
1.对于普通或变量下标运算符
错误:无法转换`std :: vector< bool,
_Alloc> :: operator [](unsigned int)[用_Alloc =
std :: allocator< bool>](ix)''
到bool&''

2.对于常量下标运算符
警告:回复临时参考

我想我在这里遗漏了一些东西,但是在书上或在互联网上查看并不能让我理解我做错了什么,即他们似乎做同样的事情(但是使用数组或指针 - 而不是
向量。)

我希望有人对如何使其工作有一些想法。
谢谢很多。
OO
Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here''s a simpler version of it.

-------------------- main.cpp---------------------------

# include <iostream>
# include <vector>
# include <cassert>

class ShiftRegister {
public:
ShiftRegister(unsigned size) : _reg(size) {

}

bool& operator[](unsigned ix) {

return _reg[ix];
}

const bool& operator[](unsigned ix) const {

return _reg[ix];
}

private:
std::vector<bool> _reg;
};
int main(int argc, char* argv[]) {

ShiftRegister sftreg(2);

}
------------------ end of main.cpp ----------------

The above contains two definitions of the subscript operator one
ordinary and one constant. Compiling this I get
1. For the ordinary or variable subscript operator
error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](ix)''
to `bool&''

2. For the constant subscript operator
warning: returning reference to temporary

I think I am missing something here, but looking in books or on the
internet does not make me understand what I am doing wrong i.e. they
seem to be doing the same thing (but with arrays or pointers - not with
vectors.)

I hope someone has some ideas on how to get this to work.
Thanks a lot.
O.O.




我对此的第一个想法是它与

std的专业化有关:: bool的矢量模板(即std :: vector< bool>是
位的向量,而不是bool,标准表示必须至少为1个字节)。如果您使用其他原始类型,代码是否有效?



My first thoughts on this are that it''s to do with the specialisation of
the std::vector template for bools (ie std::vector<bool> is a vector of
bits not bools which the standard says must be at least 1 byte). Does
the code work if you use another primitive type?


Ben Radford写道:
Ben Radford wrote:
ol ******* @ yahoo.it 写道:
ol*******@yahoo.it wrote:


我对C ++并不陌生,但我之前从未做过运算符重载。
我有一些旧代码试图实现移位寄存器 - 但我似乎无法让它发挥作用。这是一个更简单的版本。

-------------------- main.cpp ---------- -----------------

#include< iostream>
#include< vector>
#include< cassert> ;

类ShiftRegister {
公开:
ShiftRegister(无符号大小):_reg(size){

}

布尔和放大器; operator [](unsigned ix){

return _reg [ix];
}

const bool& operator [](unsigned ix)const {

返回_reg [ix];
}
私人:
std :: vector< bool> _reg;
};

int main(int argc,char * argv []){

ShiftRegister sftreg(2);
}

------------------ main.cpp结束---------------- <上面包含下标运算符的两个定义,一个是普通的,一个是常量。编译我得到
1.对于普通或变量下标运算符
错误:无法转换`std :: vector< bool,
_Alloc> :: operator [](unsigned int)[用_Alloc =
std :: allocator< bool>](ix)''
到bool&''

2.对于常量下标运算符
警告:回复临时参考

我想我在这里遗漏了一些东西,但是在书上或在互联网上查看并不能让我理解我做错了什么,即他们似乎做同样的事情(但是使用数组或指针 - 而不是
向量。)

我希望有人对如何使其工作有一些想法。
谢谢很多。
OO
Hi,

I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here''s a simpler version of it.

-------------------- main.cpp---------------------------

# include <iostream>
# include <vector>
# include <cassert>

class ShiftRegister {
public:
ShiftRegister(unsigned size) : _reg(size) {

}

bool& operator[](unsigned ix) {

return _reg[ix];
}

const bool& operator[](unsigned ix) const {

return _reg[ix];
}

private:
std::vector<bool> _reg;
};
int main(int argc, char* argv[]) {

ShiftRegister sftreg(2);

}
------------------ end of main.cpp ----------------

The above contains two definitions of the subscript operator one
ordinary and one constant. Compiling this I get
1. For the ordinary or variable subscript operator
error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](ix)''
to `bool&''

2. For the constant subscript operator
warning: returning reference to temporary

I think I am missing something here, but looking in books or on the
internet does not make me understand what I am doing wrong i.e. they
seem to be doing the same thing (but with arrays or pointers - not with
vectors.)

I hope someone has some ideas on how to get this to work.
Thanks a lot.
O.O.



我对此的第一个想法是,它与用于bools的std :: vector模板的专业化有关(即std :: vector< bool>是一个
位的向量,而不是bool,标准所说的必须至少为1个字节)。如果使用其他原始类型,代码是否有效?



My first thoughts on this are that it''s to do with the specialisation of
the std::vector template for bools (ie std::vector<bool> is a vector of
bits not bools which the standard says must be at least 1 byte). Does
the code work if you use another primitive type?




详细说明,std :: vector< bool>中的bool不要像布尔一样存在。当你访问向量时,你会得到一个由

创建的临时bool来检查相对位是打开还是关闭。由于临时只有
,所以你不能返回它的引用。我希望'比我最初的回复更清楚'
更清楚=)



To elaborate, the bools in std::vector<bool> don''t exist as bools. When
you access the vector you get given a temporary bool created by
examining whether the relative bit is on or off. Since it is only
temporary you can''t return a reference to it. I hope that''s a bit
clearer than my initial reply =)


非常,非常好猜猜Ben。我从来没有意识到甚至没有想过它。

是的我尝试编译我的程序,用这个bool替换为int - 和

它的工作原理。

但是我实际上需要布尔的。 (如果你对什么是b / b $移位寄存器有什么想法 - 你会知道为什么我真的不需要

整数。)我也想优化这个速度。关于这个问题,我会认为

,看看能做些什么 - 现在我至少知道

问题是什么。

谢谢你再次。

OO

Very, very good guess Ben. I never realised or even thought about it.
Yes I tried to compile my program with this bool replaced by int - and
it works.
However I actually need bool ''s. (If you have any idea about what a
Shift Register is - you would know why I don''t really require
integers.) I also wanted to optimize this for speed. I would think
about this problem and see what can be done - now that I at least know
what the problem is.
Thanks once again.
O.O.


这篇关于下标运算符使用向量重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆