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;
}
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;
}