473,804 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::operat or
=(cFieldList)'

Anybody any ideas what I'm doing wrong?
Thanks in advance,
Steve.
Jul 22 '05 #1
15 2170
On Fri, 30 Apr 2004 16:36:21 +0100, "Steve" <st*******@hotm ail.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::operat or
=(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.co m> 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**@nilsschne ider.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*******@hotm ail.com> wrote in message
news:znukc.42$O S2.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::operat or
=(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_b ack(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::operat or=(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*******@hotm ail.com> wrote in message
news:znukc.42$O S2.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::operat or
=(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::operat or[](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*******@hotm ail.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_b ack(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::operat or=(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*******@hotm ail.com> wrote in message
news:znukc.42$O S2.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::operat or
=(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::operat or=(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*******@hotm ail.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::opera tor=(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1889
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) {tListOf.push_back(t);} // add new object to list unsigned int Count() { return tListOf.size(); } // number of list items
10
3154
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 simpler version of it. -------------------- main.cpp--------------------------- # include <iostream>
51
23751
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 10018-clc.c: In function `main': 10018-clc.c:22: warning: array subscript has type `char' I don't like warnings ... or casts.
5
4643
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
3282
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
2735
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 based on the const type. Infact if I use the Array on the left side i.e like a1 = 111 then it uses the first while if I use cout << a1 it uses the second... why????
4
5209
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 struct myStruct { myStruct& operator (int index)
2
2322
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 for the same situation. My question: Why can't the subscript operator itself throw the out_of_range exception in which case the 'at ( )' member function may not be needed ?
19
3533
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 called MyString) and an integer i.e. std::map<MyString, int>. Whenever I insert the elements in the map using the subscript operator, I noticed that the copy constructor for MyString is invoked more number of times than if I do it using the insert()...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.