p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C++ Programming (http://p2p.wrox.com/forumdisplay.php?f=89)
-   -   Vectors/inheritance problem (http://p2p.wrox.com/showthread.php?t=9659)

hpy_gilmore8 February 22nd, 2004 10:21 PM

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,

Code:

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,

Code:

int  i;
for (i=-1; i<=8; i++) // initalize all vector elements to 0
vB[i]=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 "::".

Code:

template <typename T>
class genVector: public vector<T>
{
  public:
       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]

   private:

         int lower;
         int upper;
};

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

Code:

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

Code:

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:

Code:

#include <vector>

#include <iostream>

#include "d_except.h"



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

        lower=low;
        upper=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);
            lower=lowIndex;
            upper=highIndex;

        }

    private:
        int lower;
        int upper;
};

And the file I am using to test my class:
Code:

#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++)
        tempVector[i]=i;


    writeVector(tempVector);


    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.

Thanks!!!





nikolai February 23rd, 2004 08:21 PM

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]!!!

Quote:

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

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


Take care,

Nik
http://www.bigaction.org/


All times are GMT -4. The time now is 03:27 PM.

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