Subscript Operator

S

Steve

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::eek:perator
=(cFieldList)'

Anybody any ideas what I'm doing wrong?
Thanks in advance,
Steve.
 
L

Leor Zolman

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::eek:perator
=(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
 
L

Leor Zolman

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
 
N

Nils Schneider

I don't know if this helps, but I would try to use:

const cField& operator[](int pos) { return myvec[pos]; }const

Regards,

Nils
 
J

John Harrison

Nils Schneider said:
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
 
J

John Harrison

Steve said:
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::eek:perator
=(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
 
S

Steve

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::eek:perator=(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.
 
J

John Harrison

[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::eek:perator=(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
 
A

Adriano Dal Bosco

Steve,

Did you realise that you never really defined the behavior for []
operator?

T& tListOf::eek:perator[](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 said:
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::eek:perator=(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 said:
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::eek:perator
=(cFieldList)'

Anybody any ideas what I'm doing wrong?
Thanks in advance,
Steve.
 
L

Leor Zolman

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::eek:perator=(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.
 
J

John Harrison

Sorry that last post was complete garbage.

Your problem is that you are missing a layer of indirection.

cField f = (*(dcf->FieldList));

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
 
S

Steve

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
 
S

Steve

Hi,
T& tListOf::eek:perator[](int& pos) {return tListOf[pos];}


Oops, typo in my post, sorry.....should have been:

T& cList::eek:perator[](int& pos) {return tListOf[pos];}


Thanks for your advice,
Steve.
 
S

Steve

Hi,
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.
 
S

Steve

John Harrison said:
Sorry that last post was complete garbage.

Your problem is that you are missing a layer of indirection.

cField f = (*(dcf->FieldList));

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.
 
A

Adriano Dal Bosco

Sorry,
Your original post was correct. I misundertood it.

Adriano



Steve said:
Hi,
T& tListOf::eek:perator[](int& pos) {return tListOf[pos];}


Oops, typo in my post, sorry.....should have been:

T& cList::eek:perator[](int& pos) {return tListOf[pos];}


Thanks for your advice,
Steve.
 

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,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top