473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

More problems with subscript operator

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

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 another class, cDataList, and another, cResultList. The
data list object is a list of cData objects, and the result list is a list
of cResult objects.

class cResultList : public cList<cResult> {
private:
// some stuff
public:
// c'tors etc
};

class cDataList : public cList<cData> {
private:
// some stuff
public:
// c'tors etc

cResultList Results;
};
The cDataList object contains a cResultList object called Results.

I then have a class called cDCF like so:

class cDCF {
private:
// some stuff
public:
// c'tors etc
cDataList Data;
};

My problem is, I'm struggling to access the elements of Results (the cResult
class has a 'value' member):

cDCF *dcf = new cDCF(myfilename);

float f = dcf->Data[1].Results[1].value;

This gives a compiler error saying:
operator+ not implemented in type cResultList for arguements of type 'int'

however, if I use this:

cDCF *dcf = new cDCF(myfilename);
cData d = dcf->Data[1];
float f = d.Results[1].value;

this works. Now I know it's something to do with how I'm using the subscript
operator, but I'm not sure what. Can anybody see what I'm doing wrong
please?

Thanks for your time,
Steve
Jul 22 '05 #1
5 1870
Steve wrote:
Hi, I have a class called cList as so:
It would really help if you posted a complete compilable example that
demonstrated your issue.

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
I suspect the operator above is probably not what you want.

T& operator[](int pos) {return tListOf[pos];}
const T& operator[](int pos) const {return tListOf[pos];}

I think passing an (int) or a (const int &) is really not going to be an
issue but I think you never want to oass an (int &) becuase that limits
what can be passed in as a subscript parameter.

The const [] operator need to be a const function.

This *might* be the issue you have, I don't know.
};
This is inherited by another class, cDataList, and another, cResultList. The
data list object is a list of cData objects, and the result list is a list
of cResult objects.

class cResultList : public cList<cResult> {
private:
// some stuff
public:
// c'tors etc
};

class cDataList : public cList<cData> {
private:
// some stuff
public:
// c'tors etc

cResultList Results;
};
The cDataList object contains a cResultList object called Results.

I then have a class called cDCF like so:

class cDCF {
private:
// some stuff
public:
// c'tors etc
cDataList Data;
};

My problem is, I'm struggling to access the elements of Results (the cResult
class has a 'value' member):

cDCF *dcf = new cDCF(myfilename);

float f = dcf->Data[1].Results[1].value;

This gives a compiler error saying:
operator+ not implemented in type cResultList for arguements of type 'int'

however, if I use this:

cDCF *dcf = new cDCF(myfilename);
cData d = dcf->Data[1];
float f = d.Results[1].value;

this works. Now I know it's something to do with how I'm using the subscript
operator, but I'm not sure what. Can anybody see what I'm doing wrong
please?

Thanks for your time,
Steve


Jul 22 '05 #2
Hi Gianni

T& operator[](int pos) {return tListOf[pos];}
const T& operator[](int pos) const {return tListOf[pos];}

This fixed the problem, many thanks.


I think passing an (int) or a (const int &) is really not going to be an
issue but I think you never want to oass an (int &) becuase that limits
what can be passed in as a subscript parameter.

The const [] operator need to be a const function.


I'm still trying to get to grips with when to pass by reference and when not
to.

Thanks again,
Steve
Jul 22 '05 #3
Steve wrote:
Hi Gianni
T& operator[](int pos) {return tListOf[pos];}
const T& operator[](int pos) const {return tListOf[pos];}


This fixed the problem, many thanks.
I think passing an (int) or a (const int &) is really not going to be an
issue but I think you never want to oass an (int &) becuase that limits
what can be passed in as a subscript parameter.

The const [] operator need to be a const function.

I'm still trying to get to grips with when to pass by reference and when not
to.


The only time you want to pass by (non const) reference is when you want
the function being called to modify the caller's parameter.

Passing by value or by const & depends on what you're doing. For
example, passing a large struct by value may be unwise because the
construction and destruction are expensive in resources, passing an int
by value is probably less expensive resource wise than passing by
reference. However, there is one other issue, passing a by const &
means that you are required not to modify the parameter while in the
function being called otherwise you will get unpredictable behaviour.

See this:

int x = 2;

int func( const int & z )
{
x = z + 1;
return z * x;
}

int main()
{
return func( x );
}

The return value from main is undefined. This is because func()
modifies it's own const parameter meaning that it's not really const
after all. The compiler may make the optimization of reading the value
of z only once and use it in both expressions (note that the compiler is
free to not make the optimization according to the standard).

If we declare func() as "int func( int z )" then there is a copy of the
value x passed into the function.

However, this is rarely an issue and the cost of making a copy is
usually far greater than passing a reference.
Jul 22 '05 #4
Hi Steve,
I'm still trying to get to grips with when to pass by reference and when not to.


The only time you want to pass by (non const) reference is when you want
the function being called to modify the caller's parameter.

Passing by value or by const & depends on what you're doing.


Another consideration is whether you want polymorphic behaviour within your
function. A value parameter will always be read according to it's static
type, while a reference or pointer parameter behaves according to it's
dynamic type. Here's an example:

#include <string>
#include <iostream>

class C {
public:
virtual const std::string name() const{
return "class C";
}
};

class D : public C {
public:
const std::string name() const {
return "class D";
}
};

void f_ref(const C& c) {
std::cout << "f_ref sees: " << c.name() << "\n";
}

void f_ptr(const C* c) {
std::cout << "f_ptr sees: " << c->name() << "\n";
}

void f_val(const C c) {
std::cout << "f_val sees: " << c.name() << "\n";
}

int main() {
D d;
f_ref(d);
f_ptr(&d);
f_val(d);
return 0;
}

Output:
f_ref sees: class D
f_ptr sees: class D
f_val sees: class C
--
Regards,
Sean.
Jul 22 '05 #5
Thanks for the advice guys, your wisdom is always appreciated!

Steve.

"Woebegone" <wo*************@THIScogeco.ca> wrote in message
news:XE****************@read1.cgocable.net...
Hi Steve,
I'm still trying to get to grips with when to pass by reference and
when
not to.
The only time you want to pass by (non const) reference is when you want
the function being called to modify the caller's parameter.

Passing by value or by const & depends on what you're doing.


Another consideration is whether you want polymorphic behaviour within

your function. A value parameter will always be read according to it's static
type, while a reference or pointer parameter behaves according to it's
dynamic type. Here's an example:

#include <string>
#include <iostream>

class C {
public:
virtual const std::string name() const{
return "class C";
}
};

class D : public C {
public:
const std::string name() const {
return "class D";
}
};

void f_ref(const C& c) {
std::cout << "f_ref sees: " << c.name() << "\n";
}

void f_ptr(const C* c) {
std::cout << "f_ptr sees: " << c->name() << "\n";
}

void f_val(const C c) {
std::cout << "f_val sees: " << c.name() << "\n";
}

int main() {
D d;
f_ref(d);
f_ptr(&d);
f_val(d);
return 0;
}

Output:
f_ref sees: class D
f_ptr sees: class D
f_val sees: class C
--
Regards,
Sean.

Jul 22 '05 #6

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

Similar topics

15
by: Steve | last post by:
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...
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...
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
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.