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(u nsigned size) : _reg(size) {
}
bool& operator[](unsigned ix) {
return _reg[ix];
}
const bool& operator[](unsigned ix) const {
return _reg[ix];
}
private:
std::vector<boo l> _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<bo ol,
_Alloc>::operat or[](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 3153 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(u nsigned size) : _reg(size) {
}
bool& operator[](unsigned ix) {
return _reg[ix]; }
const bool& operator[](unsigned ix) const {
return _reg[ix]; }
private: std::vector<boo l> _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<bo ol, _Alloc>::operat or[](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<boo l> 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(u nsigned size) : _reg(size) {
}
bool& operator[](unsigned ix) {
return _reg[ix]; }
const bool& operator[](unsigned ix) const {
return _reg[ix]; }
private: std::vector<boo l> _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<bo ol, _Alloc>::operat or[](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<boo l> 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<boo l> 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<boo l> 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<boo l> 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<boo l> 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*******@yaho o.it> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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(u nsigned 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*******@rock etmail.com> wrote: You got your answers in other posts. Just a comment on your code
<ol*******@yaho o.it> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.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(u nsigned 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. 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 operator for cFieldList as so:
cField& operator(int pos) { return myvec; }
|
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) {tListOf.push_back(t);} // add new object to list
unsigned int Count() { return tListOf.size(); } // number of list items
|
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 way to find &
check it wothout using operator()(int row, int column) function?
Uday.
|
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, obj2 ;
|
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
struct myStruct
{
myStruct& operator (int index)
| |
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 atype {
int a;
public:
atype() {
|
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 v);
|
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 called
MyString) and an integer i.e. std::map<MyString, int>. Whenever I
insert the elements in the map using the subscript operator, I
noticed that the copy constructor for MyString is invoked more number
of times than if I do it using the insert()...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |