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[pos]; }
however, when I compile the following code, I get an error:
cField f = MyFieldList[1]; // this an example, assume the objects exists
correctly
The compiler returns - Could not find a match for 'cField::operator
=(cFieldList)'
Anybody any ideas what I'm doing wrong?
Thanks in advance,
Steve. 15 1990
On Fri, 30 Apr 2004 16:36:21 +0100, "Steve" <st*******@hotmail.com> wrote: 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[pos]; }
however, when I compile the following code, I get an error:
cField f = MyFieldList[1]; // this an example, assume the objects exists correctly
The compiler returns - Could not find a match for 'cField::operator =(cFieldList)'
Anybody any ideas what I'm doing wrong?
No. So give us a complete working program whittled down to the minimum it
takes to draw the error. I'd say you've got at least a 50% chance of
figuring out the problem on your own before you get to the point of posting
your code ;-)
-leor
Thanks in advance, Steve.
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Fri, 30 Apr 2004 16:46:33 GMT, Leor Zolman <le**@bdsoft.com> wrote: ody any ideas what I'm doing wrong?
No. So give us a complete working program whittled down to the minimum it takes to draw the error. I'd say you've got at least a 50% chance of figuring out the problem on your own before you get to the point of posting your code ;-) -leor
Heh...OK, it doesn't have to be "working" (Duh)
-leor Thanks in advance, Steve.
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
I don't know if this helps, but I would try to use:
const cField& operator[](int pos) { return myvec[pos]; }const
Regards,
Nils
-- http://www.nilsschneider.de (Music, Guestbook, Studio, Tools, ... )
"Nils Schneider" <ni**@nilsschneider.de> wrote in message
news:c6*************@news.t-online.com... I don't know if this helps, but I would try to use:
const cField& operator[](int pos) { return myvec[pos]; }const
It's unlikely to, I don't think its the OP's problem (but its hard to tell
given the tiny amount of code he posted), but in any case the correct syntax
is
const cField& operator[](int pos) const { return myvec[pos]; }
john
"Steve" <st*******@hotmail.com> wrote in message
news:znukc.42$OS2.25@newsfe1-win... 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[pos]; }
however, when I compile the following code, I get an error:
cField f = MyFieldList[1]; // this an example, assume the objects exists correctly
The compiler returns - Could not find a match for 'cField::operator =(cFieldList)'
Anybody any ideas what I'm doing wrong? Thanks in advance, Steve.
None at all. Post real compilable code, this should be easy to solve.
john
Hi Guys,
Sorry, I realise I didn't leave much to go on, I was just wondering if there
was some simple syntax thing I was missing. Anyway, I have a bit more info,
I hope you can get enough from what I write.
I have a class called cField. I have another class called cFieldList, which
is derived from a template class, cList, which contains the std::vector. The
subscript operator function is declared in this base class. Here's cList
definition
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
cList(); // default constructor
cList(const cList&); // copy constructor
cList& operator=(const cList&); // assignment constructor
virtual ~cList(); // destructor
T& operator[](int& pos) {return tListOf[pos];} // subscript operator
const T& operator[](const int& pos) {return tListOf[pos];} // subscript
operator
};
This is inherited by cFieldList:
class cFieldList : public cList<cField> {
private:
// some stuff
public:
// c'tors etc
};
I then have a class called cDCF which basically opens a file, which contains
a public pointer to cFieldList:
class cDCF {
private:
// some stuff
public:
// c'tors etc
cFieldList* FieldList;
};
Ok, if I use cDCF in a program dynamically, ie:
cDCF *dcf = new dcf(filename);
and use:
cField f = dcf->FieldList[0];
I get a compile error: Could not find a match for
'cField::operator=(cFieldList)'
However, if I make cFieldList* FieldList become cFieldList FieldList and use
cField f = dcf->FieldList[0];
the program compiles and runs fine, so it appears as though the [] operator
isn't working if it's class is dynamically declared. Any ideas why this is?
By the way, I am new to this, so may well be overlooking something stupid
that I don't yet understand!
Thanks for your patience,
Steve.
"Steve" <st*******@hotmail.com> wrote in message
news:znukc.42$OS2.25@newsfe1-win... 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[pos]; }
however, when I compile the following code, I get an error:
cField f = MyFieldList[1]; // this an example, assume the objects exists correctly
The compiler returns - Could not find a match for 'cField::operator =(cFieldList)'
Anybody any ideas what I'm doing wrong? Thanks in advance, Steve.
Steve,
Did you realise that you never really defined the behavior for []
operator?
T& tListOf::operator[](int& pos) {return tListOf[pos];}
The operator [] is calling itself ! You have to use some other method to
find the element pos in your list and return it.
Adriano
"Steve" <st*******@hotmail.com> wrote in message
news:yIKkc.3$74.0@newsfe1-win... Hi Guys,
Sorry, I realise I didn't leave much to go on, I was just wondering if
there was some simple syntax thing I was missing. Anyway, I have a bit more
info, I hope you can get enough from what I write.
I have a class called cField. I have another class called cFieldList,
which is derived from a template class, cList, which contains the std::vector.
The subscript operator function is declared in this base class. Here's cList definition
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 cList(); // default constructor cList(const cList&); // copy constructor cList& operator=(const cList&); // assignment constructor virtual ~cList(); // destructor
T& operator[](int& pos) {return tListOf[pos];} // subscript operator const T& operator[](const int& pos) {return tListOf[pos];} //
subscript operator };
This is inherited by cFieldList:
class cFieldList : public cList<cField> { private: // some stuff public: // c'tors etc };
I then have a class called cDCF which basically opens a file, which
contains a public pointer to cFieldList:
class cDCF { private: // some stuff public: // c'tors etc cFieldList* FieldList; };
Ok, if I use cDCF in a program dynamically, ie:
cDCF *dcf = new dcf(filename);
and use:
cField f = dcf->FieldList[0];
I get a compile error: Could not find a match for 'cField::operator=(cFieldList)'
However, if I make cFieldList* FieldList become cFieldList FieldList and
use cField f = dcf->FieldList[0];
the program compiles and runs fine, so it appears as though the []
operator isn't working if it's class is dynamically declared. Any ideas why this
is? By the way, I am new to this, so may well be overlooking something stupid that I don't yet understand!
Thanks for your patience, Steve.
"Steve" <st*******@hotmail.com> wrote in message news:znukc.42$OS2.25@newsfe1-win... 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[pos]; }
however, when I compile the following code, I get an error:
cField f = MyFieldList[1]; // this an example, assume the objects exists correctly
The compiler returns - Could not find a match for 'cField::operator =(cFieldList)'
Anybody any ideas what I'm doing wrong? Thanks in advance, Steve.
[snip] Ok, if I use cDCF in a program dynamically, ie:
cDCF *dcf = new dcf(filename);
and use:
cField f = dcf->FieldList[0];
I get a compile error: Could not find a match for 'cField::operator=(cFieldList)'
However, if I make cFieldList* FieldList become cFieldList FieldList and
use cField f = dcf->FieldList[0];
the program compiles and runs fine, so it appears as though the []
operator isn't working if it's class is dynamically declared. Any ideas why this
is? By the way, I am new to this, so may well be overlooking something stupid that I don't yet understand!
It's a simple precedence issue, [] is higher than ->
cField f = (dcf->FieldList)[0];
Sorry to labour the point (but I do like repeating myself) but this question
could not have been answered on the code originally posted.
john
On Sat, 1 May 2004 11:11:22 +0100, "Steve" <st*******@hotmail.com> wrote: Hi Guys,
Sorry, I realise I didn't leave much to go on, I was just wondering if there was some simple syntax thing I was missing. Anyway, I have a bit more info, I hope you can get enough from what I write.
....
Ok, if I use cDCF in a program dynamically, ie:
cDCF *dcf = new dcf(filename);
you mean new cDCF, I gather. and use:
cField f = dcf->FieldList[0];
In that case, the type of dcf->FieldList[0] is cFieldList, and as it says,
there's no conversion from cFieldList to CField. How were you expecting
there to be? I get a compile error: Could not find a match for 'cField::operator=(cFieldList)'
However, if I make cFieldList* FieldList become cFieldList FieldList and use
cField f = dcf->FieldList[0];
the program compiles and runs fine,
Yes, because now it ends up using your user-defined operator[] that class
cFieldList inherits from CList.
-leor
so it appears as though the [] operator isn't working if it's class is dynamically declared. Any ideas why this is? By the way, I am new to this, so may well be overlooking something stupid that I don't yet understand!
Thanks for your patience, Steve.
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
Sorry that last post was complete garbage.
Your problem is that you are missing a layer of indirection.
cField f = (*(dcf->FieldList))[i];
dcf->FieldList is a pointer, so you were calling the builting operator[]
that operates on pointers, not the one you had defined for your class.
john
> Sorry to labour the point (but I do like repeating myself) but this
question could not have been answered on the code originally posted.
I know, I appreciate that, sorry.....and thanks!
Steve
Hi, T& tListOf::operator[](int& pos) {return tListOf[pos];}
Oops, typo in my post, sorry.....should have been:
T& cList::operator[](int& pos) {return tListOf[pos];}
Thanks for your advice,
Steve.
Hi, Ok, if I use cDCF in a program dynamically, ie:
cDCF *dcf = new dcf(filename);
you mean new cDCF, I gather.
Blimey, my typing's bad today.....yup, I meant what you said, sorry. and use:
cField f = dcf->FieldList[0];
In that case, the type of dcf->FieldList[0] is cFieldList, and as it says, there's no conversion from cFieldList to CField. How were you expecting there to be?
I was obviously doing it wrong....as John posted, I was a layer out.
Thanks for the advice, and your patience,
Steve.
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c7************@ID-196037.news.uni-berlin.de... Sorry that last post was complete garbage.
Your problem is that you are missing a layer of indirection.
cField f = (*(dcf->FieldList))[i];
dcf->FieldList is a pointer, so you were calling the builting operator[] that operates on pointers, not the one you had defined for your class.
john
D'oh.....cheers John, like I said, I'm a bit of a noob, so the help is
greatly appreciated, thanks,
Steve.
Sorry,
Your original post was correct. I misundertood it.
Adriano
"Steve" <st*******@hotmail.com> wrote in message
news:3TOkc.60$74.33@newsfe1-win... Hi,
T& tListOf::operator[](int& pos) {return tListOf[pos];}
Oops, typo in my post, sorry.....should have been:
T& cList::operator[](int& pos) {return tListOf[pos];}
Thanks for your advice, Steve.
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by Steve |
last post: by
|
10 posts
views
Thread by olson_ord |
last post: by
|
51 posts
views
Thread by Pedro Graca |
last post: by
|
5 posts
views
Thread by DaVinci |
last post: by
|
3 posts
views
Thread by murali |
last post: by
|
6 posts
views
Thread by josh |
last post: by
| |
2 posts
views
Thread by subramanian100in |
last post: by
|
19 posts
views
Thread by C++Liliput |
last post: by
| | | | | | | | | | |