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. 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?
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 =)
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. 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... 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.
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
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.
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 __.
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. 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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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)...
|
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...
|
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,...
|
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
...
|
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...
|
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...
|
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) {};
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |