473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Subscript Operator

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
15 2130
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
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
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

"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

"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
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
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

[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
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
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
> 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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hi, I have a class called cList as so: template<class T> class cList { // base class for Lists private: protected: vector<T> tListOf; // field list container public: void Add(const T& t)...
10
by: olson_ord | last post by:
Hi, I am not exactly new to C++, but I have never done operator overloading before. I have some old code that tries to implement a Shift Register - but I cannot seem to get it to work. Here's a...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
5
by: DaVinci | last post by:
/* how to overload the operation ,subscipt of 2D-array? * how can I get element through piece,rather than piece(i)? * */ #include"global.h" using namespace std; class Piece { public: Piece()...
3
by: murali | last post by:
hello everybody... how can i overload the subscript operator with more than one dimension... like .... if possible please give an example... thank you...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
4
by: gvr123 | last post by:
Hi all This seems to me a peculiar problem, but confounding nonetheless... The problem seems to be that an overloaded subscript operator isn't being called unless it is called explicitly ...
2
by: subramanian100in | last post by:
For vector and deque, the 'at( )' member function throws out_of_range exception if the argument to the 'at( )' function is not in range. But the subscript operator does not throw this exception...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.