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

Subscript Operator

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
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, ... )
Jul 22 '05 #4

P: n/a

"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

Jul 22 '05 #5

P: n/a

"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
Jul 22 '05 #6

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

Jul 22 '05 #7

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



Jul 22 '05 #8

P: n/a

[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
Jul 22 '05 #9

P: n/a
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
Jul 22 '05 #10

P: n/a
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
Jul 22 '05 #11

P: n/a
> 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
Jul 22 '05 #12

P: n/a
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.
Jul 22 '05 #13

P: n/a
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.
Jul 22 '05 #14

P: n/a

"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.
Jul 22 '05 #15

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


Jul 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.