Writing my own Vector Class

I'm attempting to write my own vector class for an assignment. Been going through the text book for hours and its just not explained very well how to use Templates and an operator override at the same time. The errors I keep getting are so generic it doesn't help much. Here's what I got so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  #include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;

template<typename T>
class DynArray {
   public:
      DynArray();
      DynArray(const DynArray& a);
      DynArray& operator=(const DynArray&);
      DynArray& operator[](const DynArray&);
      void clear();
      void push_back(const T& value);
      void pop_back();
      int size();
      int back();
      int front();
      int capacity();
      int at(int n);
   private:
      int getSize; 
      int getCapacity;
      T *list = new T[2];
};

template<typename T>
DynArray<T>::DynArray(){
   getSize = 0;
   getCapacity = 2;
}

template<typename T>
DynArray<T>::DynArray(const DynArray& a){
   a.getSize = getSize;
   a.getCapacity = getCapacity; 
}

template<typename T>
DynArray& DynArray<T>::operator=(const DynArray<T>& a){
   DynArray newArray;
   a.getSize = newArray.getSize;
   a.getCapacity = newArray.getCapacity;
   delete a;
   return newArray;
}

template<typename T>
DynArray<T>& DynArray<T>::operator[](const DynArray& a){
   return *this;
}

template<typename T>
void DynArray<T>::clear(){
   this->getSize = 0;
   this->getCapacity = 2;
   delete[] list;
   T * listPtr = new T[getCapacity];
   list = listPtr;
}

template<typename T>
void DynArray<T>::push_back(const T& value){
   if(getSize == getCapacity){
      //Make a bigger list
      getCapacity *= 2;
      T * listPtr = new T[getCapacity];
      memcpy( listPtr, list, getCapacity * sizeof(T) );
      listPtr[getSize] = value;
      getSize += 1;
      delete [] list;
      list = listPtr;
   }
   else{
      //append the number on the end pf current list
      list[getSize] = value;
      getSize += 1;
   }
}

template<typename T>
void DynArray<T>::pop_back(){
   getSize -= 1;
}

template<typename T>
int DynArray<T>::size(){
   return getSize;
}

template<typename T>
int DynArray<T>::capacity(){
   return getCapacity;
}

template<typename T>
int DynArray<T>::at(int n){
   //cout << "the value of n is " << n << " the value of size is " << getSize << endl;
   if(n > getSize-1){
      throw runtime_error("invalid index");
   }
   else{
   return list[n];
   }
}


Here are a few of the errors it throws. Ignore the main ones that main should work once the class is fixed.


Again I'm mainly trying to figure out how to do a template override?

main.cpp: In function ‘int main()’:
main.cpp:38:17: error: no match for ‘operator[]’ (operand types are ‘DynArray<char>’ and ‘int’)
cout << vectD2[i] << ", ";
^
In file included from main.cpp:9:0:
dynarray.h:51:14: note: candidate: DynArray<T>& DynArray<T>::operator[](const DynArray<T>&) [with T = char]
DynArray<T>& DynArray<T>::operator[](const DynArray& a){
^~~~~~~~~~~
dynarray.h:51:14: note: no known conversion for argument 1 from ‘int’ to ‘const DynArray<char>&’
main.cpp:48:17: error: no match for ‘operator[]’ (operand types are ‘DynArray<char>’ and ‘int’)
cout << vectD3[i] << ", ";
^
In file included from main.cpp:9:0:
dynarray.h:51:14: note: candidate: DynArray<T>& DynArray<T>::operator[](const DynArray<T>&) [with T = char]
DynArray<T>& DynArray<T>::operator[](const DynArray& a){
^~~~~~~~~~~
dynarray.h:51:14: note: no known conversion for argument 1 from ‘int’ to ‘const DynArray<char>&’
main.cpp:52:19: error: lvalue required as left operand of assignment
vectD3.front() = '{';
^~~
main.cpp:53:18: error: lvalue required as left operand of assignment
vectD3.back() = '}';
^~~
main.cpp:57:17: error: no match for ‘operator[]’ (operand types are ‘DynArray<char>’ and ‘int’)
cout << vectD3[i] << ", ";
^
In file included from main.cpp:9:0:
dynarray.h:51:14: note: candidate: DynArray<T>& DynArray<T>::operator[](const DynArray<T>&) [with T = char]
DynArray<T>& DynArray<T>::operator[](const DynArray& a){
^~~~~~~~~~~
dynarray.h:51:14: note: no known conversion for argument 1 from ‘int’ to ‘const DynArray<char>&’
main.cpp:59:16: error: no match for ‘operator[]’ (operand types are ‘DynArray<char>’ and ‘int’)
cout << vectD3[vectD3.size()-2] << vectD3.back() << endl;
^
In file included from main.cpp:9:0:
dynarray.h:51:14: note: candidate: DynArray<T>& DynArray<T>::operator[](const DynArray<T>&) [with T = char]
DynArray<T>& DynArray<T>::operator[](const DynArray& a){
^~~~~~~~~~~
dynarray.h:51:14: note: no known conversion for argument 1 from ‘int’ to ‘const DynArray<char>&’
dynarray.h: In instantiation of ‘DynArray<T>::DynArray(const DynArray<T>&) [with T = char]’:
main.cpp:33:26: required from here
dynarray.h:37:14: error: assignment of member ‘DynArray<char>::getSize’ in read-only object
a.getSize = getSize;
~~~~~~~~~~^~~~~~~~~
dynarray.h:38:18: error: assignment of member ‘DynArray<char>::getCapacity’ in read-only object
a.getCapacity = getCapacity;
~~~~~~~~~~~~~~^~~~~~~~~~~~~
dynarray.h: In instantiation of ‘DynArray<T>& DynArray<T>::operator=(const DynArray<T>&) [with T = char]’:
main.cpp:44:11: required from here
dynarray.h:44:14: error: assignment of member ‘DynArray<char>::getSize’ in read-only object
a.getSize = newArray.getSize;
~~~~~~~~~~^~~~~~~~~~~~~~~~~~
dynarray.h:45:18: error: assignment of member ‘DynArray<char>::getCapacity’ in read-only object
a.getCapacity = newArray.getCapacity;
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
dynarray.h:46:4: error: type ‘const class DynArray<char>’ argument given to ‘delete’, expected pointer
delete a;
^~~~~~
dynarray.h:43:13: warning: reference to local variable ‘newArray’ returned [-Wreturn-local-addr]
DynArray newArray;
^~~~~~~~

When getting a long list of errors you should always start looking at the first error because often later errors are just a side effect of earlier errors. The first error you have posted is about the main() function but you haven't even posted the code for the main() function so something tells me this is not the first error.

When I try to compile the code that you have posted I get the following error.
test.cpp:42:11: error: deduced class type ‘DynArray&’ in function return type
test.cpp:9:7: note: ‘template<class T> class DynArray’ declared here
test.cpp:42:11: error: prototype for ‘DynArray& DynArray<T>::operator=(const DynArray<T>&)’ does not match any in class ‘DynArray<T>’
test.cpp:13:17: error: candidate is: DynArray<T>& DynArray<T>::operator=(const DynArray<T>&)

In older compilers (before class template argument deduction was added in C++17) this error message used to be slightly more comprehensible.
test.cpp:42:1: error: invalid use of template-name ‘DynArray’ without an argument list
test.cpp:9:7: ...

So the problem here is that you need to specify the template argument for the return type when defining the function outside the class definition.
1
2
3
4
template<typename T>
DynArray<T>& DynArray<T>::operator=(const DynArray<T>& a){
   ...
}
Last edited on
Topic archived. No new replies allowed.