Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old February 22nd, 2004, 09:21 PM
Registered User
Join Date: Feb 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Vectors/inheritance problem

Implement a class genVector that generalizes the vector class to create a safe array with general starting and ending indices. For instance,

genVector<int> vA(1,10), vB(-1,8);

creates objects vA and vB with index ranges 1 <= i <=10 and -1 <i < 8 , respectively. Objects of type genVector can be indexed within their defined range. For instance,

int  i;
for (i=-1; i<=8; i++) // initalize all vector elements to 0

Derive genVector from the vector class by using public inheritance. Override the index operator so it accepts indices in the correct range. Implement a derived member function resize() that resizes the vector and resets the beginning and ending indices. These actions prevent references to the vector index operator and resize() function unless the programmer uses the scope operator "::".

template <typename T>
class genVector: public vector<T>
       genVector(int low, int high);
       // vector has high - low+1 elements in range [low,high]

       T& operator [] (int i);
       // operator verifies that lower <= i <= upper. 
       // if not, it throws the indexRangeError exception

       void resize(int lowIndex, int highIndex);
       // resize vector and set range to [lowIndex, highIndex]


         int lower;
         int upper;
Place genVector in header file "genvec.h" and write a program that declares the following objects:

genVector<char> ucLetter(65,90);
genVector<double> tempVector(-10,25);

Initialize ucLetter so that ucLetter[65]='A',...,ucLetter[90]='Z'. Initialize tempVector so that tempVector is the Fahrenheit equivalent of Celsiius tempature t. Recall that

F=9.0/5.0 * C+32

Display the contents of each vector.

Based on these requirements, here's what I have so far.

The genvec.h header file:

#include <vector>

#include <iostream>

#include "d_except.h"

template <typename T>
class genVector:public vector<T>
        genVector(int low, int high)    


        T& operator[] (int i)
            if ((i < lower) || (i > upper))
                throw indexRangeError(
                "genVector: index range error",i,size());
            return vector<T>::operator [] (i);

        void resize(int lowIndex, int highIndex)
            genVector<T>(lowIndex, highIndex);


        int lower;
        int upper;
And the file I am using to test my class:
#include <iostream>
#include "genvec.h"
#include "d_util.h"

using namespace std;

int main()
    genVector<double> tempVector (-10,25);

    int i;
    for (i=-10;i<=25;i++)


    return 0;
When I compile the code, it does not provide any errors from the compiler. I get an error once it's compiled indicating that the problem has encountered a problem and needs to close. I've found in the past that usually means that I have a memory leak or something of that nature. I am not sure where the problem lies in this specific code, however.

Any assistance that anyone could provide would be greatly appreciated.


Reply With Quote
  #2 (permalink)  
Old February 23rd, 2004, 07:21 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Okay, so that sounds like it's a homework assignment. I'd suggest talking to your professor/teacher/teacher's aide/tutor about homework-specific problems, as many schools, classes, and teachers have restrictions on where you can get help for your assignments.

That said:

you don't ever store a vector anywhere! You *create* a vector in your constructor, but don't assign it to anything, so when you're constructor finishes... *poof*! It's gone.

Also, you explicitly create a vector<int>. That doesn't sound like a generic vector to me... you need to create a vector of the same data type your genVector contains.

I would assume that the reason you're creating a genVector is to be able to use arbitrary integer indexes, whereas std::vector is a zero-based index container. That means that your primary goal in creating this vector is to intercept and translate generic indexes to zero-based indexes.

That's really what you should be doing in your constructor and operator[] -- converting between the generic index and the "real" one. After all, it's still std::vector that's storing everything.

If you're still having problems, please let us know (if you're allowed to, that is). I don't want to give you too much information because you really won't learn anything unless you figure it out for yourself. I know it's challenging, but once the light goes on in your head, it'll all seem easy because of your new level of understanding. If one of us just gave you the answer(s), you would just turn in our work and not really understand the "why" or "how" of the assignment.

Finally, I should ask: where is writeVector() defined?

One more thing! For the love of all things decent, [b]use spacing between tokens[b]!!!

    int i;
    for (i=-10;i<=25;i++)
would be a lot more readable as:

    int i;
    for (i = -10; i <= 25; i++)
        tempVector[i] = i;

Take care,

Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance problem kaos_frack BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 March 25th, 2007 09:28 AM
Problem With Inheritance jazzcatone ASP.NET 2.0 Basics 1 April 23rd, 2006 11:33 AM
Inheritance problem flyin General .NET 2 May 20th, 2004 10:55 AM
c# inheritance bhohman C# 2 March 26th, 2004 12:47 PM

All times are GMT -4. The time now is 12:56 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.