473,406 Members | 2,549 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,406 software developers and data experts.

Overloaded [] operator + template

Hi All,

Can anybody explain how to fix the [] operator in the code below, Visual C++
gives the following error message:

error C2678: binary '[' : no operator found which takes a left-hand operand
of type 'const bool_vector<len>'

I tried a number of code permutations but ended up with more errors (I am a
beginner :-),

Thanks,
Hans

#include <iostream>
using namespace std;

template<int lenclass bool_vector;
template<int len ostream &operator<< (ostream &os, const bool_vector<len>
&v);

template<int len class bool_vector {
private:
bool *v;
int i;
public:
int sz;
bool_vector() {
v=new bool[sz=len];
for (i=0; i<sz; i++) v[i]=0;
};
~bool_vector() {
delete [] v;
};

friend ostream& operator << <>(ostream& os, const bool_vector<len>&); //
required?

void write(const string& initstr) {
for (i=0; i<len; i++) v[i]=initstr[i]-'0';
}

bool operator[] (const int& x){
return v[x];
}
};

template<int len ostream& operator << (ostream& os, const
bool_vector<len>& v) {
for (int i=0;i<v.sz;i++) os << v[i]; // **** Error C2678 ****
return os;
}

int main()
{
bool_vector<4a;
a.write("1011");
cout << "Using overloaded ostream " << a << endl;
}
Jul 4 '06 #1
4 1593

"Hans" <ha****@ht-lab.comwrote in message
news:Iy*****************@newsfe1-win.ntli.net...
Hi All,

Can anybody explain how to fix the [] operator in the code below, Visual
C++ gives the following error message:

error C2678: binary '[' : no operator found which takes a left-hand
operand of type 'const bool_vector<len>'

I tried a number of code permutations but ended up with more errors (I am
a beginner :-),

Thanks,
Hans

#include <iostream>
using namespace std;

template<int lenclass bool_vector;
template<int len ostream &operator<< (ostream &os, const
bool_vector<len&v);

template<int len class bool_vector {
private:
bool *v;
int i;
public:
int sz;
bool_vector() {
v=new bool[sz=len];
for (i=0; i<sz; i++) v[i]=0;
};
~bool_vector() {
delete [] v;
};

friend ostream& operator << <>(ostream& os, const bool_vector<len>&);
// required?

void write(const string& initstr) {
for (i=0; i<len; i++) v[i]=initstr[i]-'0';
}

bool operator[] (const int& x){
return v[x];
Should be:
bool operator[] (const int& x) const // note "const"
{
return v[x];
}

[snip]

Your class has other problems too. It needs both a copy constructor and an
assignment operator. Otherwise if you copy this object the pointer will be
deleted twice causing the dreaded undefined behavior.

From a style point of view, I suggest getting out of the habit of adding ;
at the end of function declarations and (more importantly) avoid public data
items.

Cy
Jul 4 '06 #2
Hans wrote:
Hi All,

Can anybody explain how to fix the [] operator in the code below, Visual
C++ gives the following error message:

error C2678: binary '[' : no operator found which takes a left-hand
operand of type 'const bool_vector<len>'

I tried a number of code permutations but ended up with more errors (I am
a beginner :-),

Thanks,
Hans

#include <iostream>
using namespace std;

template<int lenclass bool_vector;
template<int len ostream &operator<< (ostream &os, const
bool_vector<len&v);

template<int len class bool_vector {
private:
bool *v;
int i;
public:
int sz;
bool_vector() {
v=new bool[sz=len];
for (i=0; i<sz; i++) v[i]=0;
};
~bool_vector() {
delete [] v;
};

friend ostream& operator << <>(ostream& os, const bool_vector<len>&);
//
required?

void write(const string& initstr) {
for (i=0; i<len; i++) v[i]=initstr[i]-'0';
}

bool operator[] (const int& x){
try:

bool operator[] (const int& x) const {
return v[x];
}
};

template<int len ostream& operator << (ostream& os, const
bool_vector<len>& v) {
for (int i=0;i<v.sz;i++) os << v[i]; // **** Error C2678 ****
return os;
}

int main()
{
bool_vector<4a;
a.write("1011");
cout << "Using overloaded ostream " << a << endl;
}

Best

Kai-Uwe Bux
Jul 4 '06 #3

"Cy Edmunds" <sp***************@rochester.rr.comwrote in message
news:P_******************@twister.nyroc.rr.com...
>
"Hans" <ha****@ht-lab.comwrote in message
news:Iy*****************@newsfe1-win.ntli.net...
>Hi All,

Can anybody explain how to fix the [] operator in the code below, Visual
C++ gives the following error message:

error C2678: binary '[' : no operator found which takes a left-hand
operand of type 'const bool_vector<len>'
[snip]
>>
bool operator[] (const int& x){
return v[x];

Should be:
bool operator[] (const int& x) const // note "const"
{
return v[x];
}

[snip]

Your class has other problems too. It needs both a copy constructor and an
assignment operator. Otherwise if you copy this object the pointer will be
deleted twice causing the dreaded undefined behavior.

From a style point of view, I suggest getting out of the habit of adding ;
at the end of function declarations and (more importantly) avoid public
data items.

Cy
Hi Cy/Kai-Uwe,

That fixed the problem although I don't fully understand why. My current
understanding is that const tells the compiler that the function doesn't
modify the state of the class (variables?), but if you don't change them
(like I do in the bool operator[] function) then why would you get a compile
error?

Thanks,
Hans.


Jul 4 '06 #4
Hans wrote:
>
"Cy Edmunds" <sp***************@rochester.rr.comwrote in message
news:P_******************@twister.nyroc.rr.com...
>>
"Hans" <ha****@ht-lab.comwrote in message
news:Iy*****************@newsfe1-win.ntli.net...
>>Hi All,

Can anybody explain how to fix the [] operator in the code below, Visual
C++ gives the following error message:

error C2678: binary '[' : no operator found which takes a left-hand
operand of type 'const bool_vector<len>'
[snip]
>>>
bool operator[] (const int& x){
return v[x];

Should be:
bool operator[] (const int& x) const // note "const"
{
return v[x];
}

[snip]

Your class has other problems too. It needs both a copy constructor and
an assignment operator. Otherwise if you copy this object the pointer
will be deleted twice causing the dreaded undefined behavior.

From a style point of view, I suggest getting out of the habit of adding
; at the end of function declarations and (more importantly) avoid public
data items.

Cy

Hi Cy/Kai-Uwe,

That fixed the problem although I don't fully understand why. My current
understanding is that const tells the compiler that the function doesn't
modify the state of the class (variables?), but if you don't change them
(like I do in the bool operator[] function) then why would you get a
compile error?
Have a look at your definition of operator<<:

template<int len>
ostream& operator << (ostream& os, const bool_vector<len>& v) {
for (int i=0;i<v.sz;i++) os << v[i]; // **** Error C2678 ****
return os;
}

In the signature, you promise that the bool_vector reference that you pass
will be const. In the body of the function, you call a method on that
object that is not declared const, i.e., a method that potentially alters
the object. The compiler just takes your word for it, it does not check on
itself whether operator[] maybe secretly const.
Best

Kai-Uwe Bux
Jul 4 '06 #5

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

Similar topics

4
by: Roy Yao | last post by:
Why the following code let my compiler complain an overloaded function Init()? // code begin template<class T> class BicircularList { template<class T> class Iterator; template<class T> class...
2
by: Surya Kiran | last post by:
Hi all, I've a class template, of which i've an overloaded operator (operator <<) which was written like this =============== classa.hpp =========== template <class T> class A { .... ....
5
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit...
4
by: vladimir | last post by:
All, I have seemingly quite known problem, namely I need to watch overloaded in quick watch(by own vector) but I can not. It says overloaded operator is not found. And that is claimed normal...
1
by: Dave Corby | last post by:
Hi all, I have an overloaded template function, and in one particular spot can't get the right version of it to be called. Everywhere else in the program the correct version is called. Here's...
4
by: gobis | last post by:
Hello everyone, After I could not overload the << operator as a friend operator in my template class (yes, it did not have to be a template class, but I was testing something for the class I was...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
10
by: andrew browning | last post by:
i have overlaoded all of my arithmetic operators but all are functioning as multiplication. below is a sample of the addition operator: Rational operator + (const Rational& r1, const Rational&...
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
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...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.