Overloaded [] operator + template

H

Hans

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 len> class 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=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=initstr-'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; // **** Error C2678 ****
return os;
}

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

Cy Edmunds

Hans said:
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 len> class 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=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=initstr-'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
 
K

Kai-Uwe Bux

Hans said:
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 len> class 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=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=initstr-'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; // **** Error C2678 ****
return os;
}

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



Best

Kai-Uwe Bux
 
H

Hans

Cy Edmunds said:
Hans said:
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.
 
K

Kai-Uwe Bux

Hans said:
Cy Edmunds said:
Hans said:
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; // **** 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top