473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Subscript operator overloading with vectors

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.

Jan 27 '06 #1
10 3095
ol*******@yahoo.it wrote:
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.


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?
Jan 27 '06 #2
Ben Radford wrote:
ol*******@yahoo.it wrote:
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.


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?


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 =)
Jan 27 '06 #3
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.

Jan 27 '06 #4
ol*******@yahoo.it wrote:
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.
<snip std::vector<bool> stuff>
I hope someone has some ideas on how to get this to work.
Thanks a lot.


Are you aware of std::bitset?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 27 '06 #5

ol*******@yahoo.it wrote:
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.)


Use chars...there really isn't a diff anyway.

Jan 27 '06 #6
Ben Radford <be**************@new.ox.ac.uk> wrote:
Ben Radford wrote:
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?


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 =)


Yes, for more information:

When Is a Container Not a Container?
http://www.gotw.ca/publications/mill09.htm

vector<bool> Is Nonconforming, and Forces Optimization Choice
http://www.gotw.ca/publications/N1185.pdf

vector<bool>: More Problems, Better Solutions
http://www.gotw.ca/publications/N1211.pdf

--
Marcus Kwok
Jan 27 '06 #7
You got your answers in other posts. Just a comment on your code

<ol*******@yahoo.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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) {

}


_reg is a no no. You shouldn't use an underscore as the first char of a
variable/function/whatever.
Jan 28 '06 #8
In article <BK*****************@fe05.lga>,
"Jim Langston" <ta*******@rocketmail.com> wrote:
You got your answers in other posts. Just a comment on your code

<ol*******@yahoo.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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) {

}


_reg is a no no. You shouldn't use an underscore as the first char of a
variable/function/whatever.


There is absolutely no problem with a member-variable having a
underscore as its first character as long as the next character is not
an upper-case letter.

The restrictions are as follows:

any global-scope name beginning with _.
any name beginning with _ followed by an upper-case letter.
any name containing __.
Jan 28 '06 #9
Thanks a lot guys for your posts and your comments.
Regarding Ben's (Ben Pope - because there are two Bens here)
suggestion of std::bitset - I cannot use it, because of the same
problem of providing a reference i.e. a pointer.

I finally got this working by declaring my own class - that had a
bool as its private member.
Thanks to all.
O.O.

Jan 29 '06 #10
ol*******@yahoo.it wrote:
Thanks a lot guys for your posts and your comments.
Regarding Ben's (Ben Pope - because there are two Bens here)
suggestion of std::bitset - I cannot use it, because of the same
problem of providing a reference i.e. a pointer.
I was assuming you could just use the std::bitset as your register:

typedef std::bitset<16> my16bitShiftRegister;

my16bitShiftRegister reg = 0x1234;
reg <<= 4; // reg contains 0x2340 (I think :)

I didn't know what other functionality you required.
I finally got this working by declaring my own class - that had a
bool as its private member.
Thanks to all.
O.O.


Job done then!

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 30 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
5
by: Steve | last post by:
Hi, I have a class called cList as so: template<class T> class cList { // base class for Lists private: protected: vector<T> tListOf; // field list container public: void Add(const T& t)...
2
by: Uday | last post by:
hello friends, i have defined a template two dimensional array. its working fine. since the operator function gives me only the row index, i couldn't catch the column index overflow. is there any...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
4
by: gvr123 | last post by:
Hi all This seems to me a peculiar problem, but confounding nonetheless... The problem seems to be that an overloaded subscript operator isn't being called unless it is called explicitly ...
5
by: ashu | last post by:
i have studied this example of overloading operator, but i don`t know exactly what happened in code .kindly explain me: #include <iostream> using namespace std; const int SIZE = 3; class...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
5
by: sendos | last post by:
Consider the following sample code #include <iostream> using namespace std; class A { public: int x; A(int n = 0) : x(n) {};
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.