473,396 Members | 1,891 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,396 software developers and data experts.

How to overload Subscript operator

The following code results in Segmentation fault

=========================
#include <iostream>
using namespace std;
class abc{
public:
int a;
int b;
int c;
abc() {
a = 0;
b = 0;
c = 0;
}
};
int main()
{
for (int i = 0; i < 10; i++)
{
abc a[i];
abc *pa = &a[i];
pa->a = 10*i;
pa->b = 10*i;
pa->c = 10*i;
cout << "pa->a[" << i << "]: " << pa->a << endl;
cout << "pa->b[" << i << "]: " << pa->b << endl;
cout << "pa->c[" << i << "]: " << pa->c << endl;
}
return 0;
}
=========================

I know I need to overload subscript operator?
1. I am not sure how to do that. can some one suggest a way?
2. Is there anything else missing, all I want to do is read data, and
later display it?

Aug 9 '06 #1
2 4139

pasa_1 wrote:
The following code results in Segmentation fault

=========================
#include <iostream>
using namespace std;
class abc{
public:
int a;
int b;
int c;
abc() {
a = 0;
b = 0;
c = 0;
}
};
int main()
{
for (int i = 0; i < 10; i++)
{
abc a[i];
abc *pa = &a[i];
pa->a = 10*i;
pa->b = 10*i;
pa->c = 10*i;
cout << "pa->a[" << i << "]: " << pa->a << endl;
cout << "pa->b[" << i << "]: " << pa->b << endl;
cout << "pa->c[" << i << "]: " << pa->c << endl;
}
return 0;
}
=========================

I know I need to overload subscript operator?
1. I am not sure how to do that. can some one suggest a way?
2. Is there anything else missing, all I want to do is read data, and
later display it?
you probably want the
abc a[i];
outside the for loop as
abc a[10];

Aug 9 '06 #2
pasa_1 schrieb:
>
I know I need to overload subscript operator?
You don't. See below.
class abc{
public:
int a;
int b;
int c;
abc() {
a = 0;
b = 0;
c = 0;
In general case, prefer initialization over assignment:

abc() : a(0), b(0), c(0)
{
}
}
};
int main()
{
for (int i = 0; i < 10; i++)
{
abc a[i];
The size of an array has to be constant. This shouldn't compile unless
your compiler has variable length Arrays as extension (gcc has).
You would want this to be outside the for-loop with size equal to the
maximum subcript you use plus 1:

abc a[10];
abc *pa = &a[i];
[snip]

Here you make a pointer to one past the last element of the array. Using
this pointer is undefined behaviour, giving you a segmentation fault.

--
Thomas
Aug 9 '06 #3

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

Similar topics

1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
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...
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()...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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,...

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.