0

I keep getting this error and I can't figure out what is wrong.

error C2227: left of '->capacity' must point to class/struct/union/generic type
1> type is 'int *'

Here is the class header and declaration that I am working on.

#ifndef MYVECTOR_H
#define MYVECTOR_H

#include <iostream>
//#include <sstream>
//#include <string>
//#include <algorithm>
#include "arrayList.h"
#include "changeLength1D.h"

using namespace std;

template <class T>
class MyVector : public arrayList<T>
{
	public: 
		MyVector();
		MyVector(int n);
		int size();
		int capacity();
		T& at(int n);
		T& operator[](int n);
		void erase(int n);
		bool empty();
		void clear();
		void push_back(const T & item);
		void pop_back();
	protected:
};

template <class T>
MyVector<T>::MyVector()
{
	arrayLength=10;
	element=new T[arrayLength];
}

template <class T>
MyVector<T>::MyVector(int n)
{
	arrayLength=n;
	element=new T[n];
}

template <class T>
int MyVector<T>::size()
{
	return element -> size();
}

template <class T>
int MyVector<T>::capacity()
{
	return element->capacity();
}

template <class T>
T& MyVector<T>::at(int n)
{
	return element->get(n);
}

template <class T>
T& MyVector<T>::operator [](int n)
{
  return element->get(n);
}

template <class T>
void MyVector<T>::erase(int n)
{
	element->erase(n);
	if(element->size()<= (element->capacity()/2))
	{
		T* temp;
		temp=new T[(element->capacity()*(3/4))];
		copy(element,element + element->size(), temp);
		delete [] element;
		element=temp;
		arrayLength= (element->capacity()*(3/4));
	}
}

template <class T>
bool MyVector<T>::empty()
{
	return element->empty();
}

template <class T>
void MyVector<T>::clear()
{
	delete [] element;
	listSize=0;
	arrayLength=10;
	element=new T[element->arrayLength];
}

template <class T>
void MyVector<T>::push_back(const T& item)
{
	if(element->size()= (element->capacity))
	{
		T* temp;
		temp=new T[(element->capacity()*(2))];
		copy(element,element + element->size(), temp);
		delete [] element;
		element=temp;
		arrayLength= (element->capacity()*(2));
	}
	element->insert(element->size()-1,item);
}

template <class T>
void MyVector<T>::pop_back()
{
	element->erase(n);
	if(element->size()<= (element->capacity()/2))
	{
		T* temp;
		temp=new T[(element->capacity()*(3/4))];
		copy(element,element + element->size(), temp);
		delete [] element;
		element=temp;
		element->arrayLength= (element->capacity()*(3/4));
	}
}
#endif

The rest of this code I am not to change but I am adding it because it is included.

// array implementation of a linear list
// derives from abstract class linearList just to make sure
// all methods of the ADT are implemented
// USES STL ALGORITHMS TO SIMPLIFY CODE

#ifndef arrayList_
#define arrayList_

#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include "linearList.h"
#include "myExceptions.h"
#include "changeLength1D.h"

using namespace std;

template<class T>
class arrayList : public linearList<T> 
{
   public:
      // constructor, copy constructor and destructor
      arrayList(int initialCapacity = 10);
      arrayList(const arrayList<T>&);
      ~arrayList() {delete [] element;}

      // ADT methods
      bool empty() const {return listSize == 0;}
      int size() const {return listSize;}
      T& get(int theIndex) const;
      int indexOf(const T& theElement) const;
      void erase(int theIndex);
      void insert(int theIndex, const T& theElement);
      void output(ostream& out) const;

      // additional method
      int capacity() const {return arrayLength;}

   protected:
      void checkIndex(int theIndex) const;
            // throw illegalIndex if theIndex invalid
      T* element;            // 1D array to hold list elements
      int arrayLength;       // capacity of the 1D array
      int listSize;          // number of elements in list
};

template<class T>
arrayList<T>::arrayList(int initialCapacity)
{// Constructor.
   if (initialCapacity < 1)
   {ostringstream s;
    s << "Initial capacity = " << initialCapacity << " Must be > 0";
    throw illegalParameterValue(s.str());
   }
   arrayLength = initialCapacity;
   element = new T[arrayLength];
   listSize = 0;
}

template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{// Copy constructor.
   arrayLength = theList.arrayLength;
   listSize = theList.listSize;
   element = new T[arrayLength];
   copy(theList.element, theList.element + listSize, element);
}

template<class T>
void arrayList<T>::checkIndex(int theIndex) const
{// Verify that theIndex is between 0 and listSize - 1.
   if (theIndex < 0 || theIndex >= listSize)
   {ostringstream s;
    s << "index = " << theIndex << " size = " << listSize;
    throw illegalIndex(s.str());
   }

}

template<class T>
T& arrayList<T>::get(int theIndex) const
{// Return element whose index is theIndex.
 // Throw illegalIndex exception if no such element.
   checkIndex(theIndex);
   return element[theIndex];
}

template<class T>
int arrayList<T>::indexOf(const T& theElement) const
{// Return index of first occurrence of theElement.
 // Return -1 if theElement not in list.

   // search for theElement
   int theIndex = (int) (find(element, element + listSize, theElement)
                         - element);

   // check if theElement was found
   if (theIndex == listSize)
     // not found
     return -1;
   else return theIndex;
 }

template<class T>
void arrayList<T>::erase(int theIndex)
{// Delete the element whose index is theIndex.
 // Throw illegalIndex exception if no such element.
   checkIndex(theIndex);

   // valid index, shift elements with higher index
   copy(element + theIndex + 1, element + listSize,element + theIndex);

   element[--listSize].~T(); // invoke destructor
}

template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement)
{// Insert theElement so that its index is theIndex.
   if (theIndex < 0 || theIndex > listSize)
   {// invalid index
      ostringstream s;
      s << "index = " << theIndex << " size = " << listSize;
      throw illegalIndex(s.str());
   }

   // valid index, make sure we have space
   if (listSize == arrayLength)
      {// no space, double capacity
         changeLength1D(element, arrayLength, 2 * arrayLength);
         arrayLength *= 2;
      }

   // shift elements right one position
   copy_backward(element + theIndex, element + listSize,
                 element + listSize + 1);

   element[theIndex] = theElement;

   listSize++;
}

template<class T>
void arrayList<T>::output(ostream& out) const
{// Put the list into the stream out.
   copy(element, element + listSize, ostream_iterator<T>(cout, "  "));
}

// overload <<
template <class T>
ostream& operator<<(ostream& out, const arrayList<T>& x)
   {x.output(out); return out;}

#endif
// abstract class linearList
// abstract data type specification for linear list data structure
// all methods are pure virtual functions

#ifndef linearList_
#define linearList_
#include <iostream>

using namespace std;

template<class T>
class linearList 
{
   public:
      virtual ~linearList() {};
      virtual bool empty() const = 0;
                  // return true iff list is empty
      virtual int size() const = 0;
                  // return number of elements in list
      virtual T& get(int theIndex) const = 0;
                  // return element whose index is theIndex
      virtual int indexOf(const T& theElement) const = 0;
                  // return index of first occurence of theElement
      virtual void erase(int theIndex) = 0;
                  // remove the element whose index is theIndex
      virtual void insert(int theIndex, const T& theElement) = 0;
                  // insert theElement so that its index is theIndex
      virtual void output(ostream& out) const = 0;
                  // insert list into stream out
};
#endif
// exception classes for various error types

#ifndef myExceptions_
#define myExceptions_
#include <string>

using namespace std;

// illegal parameter value
class illegalParameterValue 
{
   public:
      illegalParameterValue(string theMessage = "Illegal parameter value")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal input data
class illegalInputData 
{
   public:
      illegalInputData(string theMessage = "Illegal data input")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal index
class illegalIndex 
{
   public:
      illegalIndex(string theMessage = "Illegal index")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix index out of bounds
class matrixIndexOutOfBounds 
{
   public:
      matrixIndexOutOfBounds
            (string theMessage = "Matrix index out of bounds")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix size mismatch
class matrixSizeMismatch 
{
   public:
      matrixSizeMismatch(string theMessage = 
                   "The size of the two matrics doesn't match")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// stack is empty
class stackEmpty
{
   public:
      stackEmpty(string theMessage = 
                   "Invalid operation on empty stack")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// queue is empty
class queueEmpty
{
   public:
      queueEmpty(string theMessage = 
                   "Invalid operation on empty queue")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// hash table is full
class hashTableFull
{
   public:
      hashTableFull(string theMessage = 
                   "The hash table is full")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// edge weight undefined
class undefinedEdgeWeight
{
   public:
      undefinedEdgeWeight(string theMessage = 
                   "No edge weights defined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// method undefined
class undefinedMethod
{
   public:
      undefinedMethod(string theMessage = 
                   "This method is undefined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};
#endif
// change the length of an array

#ifndef changeLength1D_
#define changeLength1D_

#include "myExceptions.h"

using namespace std;

template<class T>
void changeLength1D(T*& a, int oldLength, int newLength)
{
   if (newLength < 0)
      throw illegalParameterValue("new length must be >= 0");

   T* temp = new T[newLength];              // new array
   int number = min(oldLength, newLength);  // number to copy
   copy(a, a + number, temp);
   delete [] a;                             // deallocate old memory
   a = temp;
}

#endif

Here is my test driver

/********************************************************
*  This program tests some of the functionality of the 
*  MyVector class. Add your own tests to completely test 
*  the MyVector class and its methods.
********************************************************/
#include <iostream>
#include <iterator>
#include <algorithm>
#include "MyVector.h"

using namespace std;

int main()
{
    MyVector<int> numbers;

    cout << "Testing adding items to the MyVector..." << endl;
    cout << "Starting Capacity: " << numbers.capacity() << endl;

    const int TEST_LENGTH = 30;
    for(int i = 0; i < TEST_LENGTH; i++)
    {
        cout << "Pushing back " << i; 
        numbers.push_back(i);
        cout << "  Size: " << numbers.size();
        cout << "  Capacity: " << numbers.capacity() << endl;
    }

    cout << endl << "Testing accessing an index outside of range..." << endl;
    cout << "Accessing index 200 " << endl;
    try
    {
        cout << numbers[200] << endl;
    }
    catch(illegalIndex i)
    {
        cout << "Illegal index!" << endl;
    }
    cout << endl;

    cout << endl << "Testing removing items for the MyVector..." << endl;
    for(int i = 1; i <= TEST_LENGTH; i++)
    {
        cout << "Popping back #" << i; 
        numbers.pop_back();
        cout << "  Size: " << numbers.size();
        cout << "  Capacity: " << numbers.capacity() << endl;
    }

    MyVector<char> name;

    cout << endl;
    name.push_back('C');
    name.push_back('S');
    name.push_back('S');
    name.at(2) = 'E';

    cout << endl << "name contains: ";
    for (int i = 0; i < name.size(); i++)
        cout << name.at(i);
    cout << endl;

    cout << endl << "Creating name2 from name" << endl;
    MyVector<char> name2 = name;
    cout << "Modifying name2..." << endl;
    name2[0] = 'B';

    cout << "name contains: ";
    for (int i = 0; i < name.size(); i++)
        cout << name.at(i);
    cout << endl;

    cout << "name2 contains: ";
    for (int i = 0; i < name2.size(); i++)
        cout << name2.at(i);
    cout << endl;

    cout << endl << "Clearing name" << endl;
    name.clear();

    cout << "name contains: ";
    for (int i = 0; i < name.size(); i++)
        cout << name.at(i);
    cout << endl;

    cout << "name2 contains: ";
    for (int i = 0; i < name2.size(); i++)
        cout << name2.at(i);
    cout << endl;

    /****************************************************
    * Add code to test your iterator if you are in the 
    *    honors section....
    * Make sure that algorithms such as reverse and 
    *    accumulate
    ****************************************************/

    system("pause");
    return EXIT_SUCCESS;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

variable element is not a class, its a T* (or int* as used in the test program).

MyVector<int> numbers;

template <class T>
class MyVector : public arrayList<T>


template<class T>
class arrayList : public linearList<T>
{
T* element; // 1D array to hold list elements
};

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.