By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Overloaded [] operator + template

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.