View Single Post
  #1 (permalink)  
Old February 22nd, 2004, 10:21 PM
hpy_gilmore8 hpy_gilmore8 is offline
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,

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




Reply With Quote