expected initializer before ‘<’ token

I get this error: expected initializer before ‘<’ token on a template class function definition.

1
2
3
4
5
6
7
8
9
10
11
template <class T>
void Vector<T>::clear()
{
  if(m_data != NULL)
  {
    delete [] m_data;
    m_data = NULL;
    m_size = 0;
    m_max_size = 0;
  }
}

the error is on the void Vector<T>::clear() line
What you post is OK as far as I can tell. Most likely the error is a consequence of some incorrect line above these.
Here's the rest of the code

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
#include<stdexcept>
#include<iostream>

template <class T>
void Vector<T>::clear()
{
  if(m_data != NULL)
  {
    delete [] m_data;
    m_data = NULL;
    m_size = 0;
    m_max_size = 0;
  }
}

template <class T>
void Vector<T>::push_back(const T& x)
{
  if(m_size + 1 == m_max_size)
  {
    m_max_size *= 2;
    T *tmp;
    tmp = new T[m_size];
    for(int i = 0; i < m_size; i++)
      tmp[i] = m_data[i];
    delete [] m_data;
    m_data = new T[m_max_size];
    for(int i = 0; i < m_size; i++)
      m_data[i] = tmp[i];
    m_data[m_size] = x;
    delete [] tmp;
    m_size++;
  }
  
  else
  {
    m_data[m_size] = x;
    m_size++;
  }
}

template <class T>
void Vector<T>::remove(const T& x)
{
  bool inVec = false;
  int i = 0;
  do
  {
    if(x == m_data[i])
      inVec = true;
    i++;
  }while(inVec == false || i == m_size);
  
  if(inVec == true)
  {
    for(int j = i; j < m_size + 1; j++)
      m_data[j] = m_data[j + 1];
  }
  m_size--;
  if(m_size <= (m_max_size / 4))
    m_max_size /= 2;
}

template <class T>
void Vector<T>::pop_back()
{
  m_size--;
  if(m_size / m_max_size <= .25)
    m_max_size /= 2;
}

template <class T>
T& Vector<T>::operator[](unsigned int i)
{
  if(i < 0 || i >= m_size)
    throw std::out_of_range("i<0 or i>=m_size");
  return m_data[i];
}

template <class T>
const T& Vector<T>::operator[](unsigned int i) const
{
  if(i < 0 || i >= m_size)
    throw std::out_of_range("i<0 or i>=m_size");
  return m_data[i];
}

template <class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
{
  m_size  = rhs.m_size;
  m_max_size = rhs.m_max_size;
  m_data = new T[m_max_size];
  for(int i = 0; i < m_size; i++)
    m_data[i] = rhs.m_data[i];
  return *this;
}
You seem to be missing the class definition.
That's in a header file

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
#ifndef VECTOR_H
#define VECTOR_H

#include <cstdlib>

template <class T>
class Vector
{
  private:  
    unsigned int m_size;      // current # of elements (i.e., data items)
    unsigned int m_max_size;  // current maximum size of vector
    T* m_data;                // data
    
  public:
    // Purpose: default constructor
    // Postconditions: current size and maximum size set to 0, 
    // and data set to NULL
    Vector(): m_size(0), m_max_size(0), m_data(NULL) {}

    // Purpose: copy constructor
    // Parameters: cpy is Vector that is to be copied
    // Postconditions: current size, maximum size, and data set to those of cpy
    Vector(const Vector<T>& cpy) { *this = cpy; }

    // Purpose: destructor
    // Postconditions: current size and maximum size set to 0, 
    // and data set to NULL (via clear())
    ~Vector() { clear(); }

    // Purpose: effectively "empties" the vector
    // Postconditions: current size and maximum size set to 0, 
    // data set to NULL, and all dynamically allocated memory for data is
    // deallocated
    void clear();
    
    // Purpose: puts the value x at the "leftmost available" index in vector
    // Parameters: x is value to be added to vector
    // Postconditions: if current size + 1 == maximum size BEFORE the insertion
    // of x, memory for data is doubled; x now at the formerly "leftmost 
    // available" position in the vector, and current size incremented by 1
    void push_back(const T& x);

    // Purpose: Removes one instance of the value x from the vector if it exists
    // Parameters: x is the value to be removed from the vector
    // Postconditions: if x is in the vector, m_size is decremented and the
    //     first instance of x is removed from the vector. If x is not in the
    //     vector, do nothing.
    //     If m_size <= m_max_size/4, halve the amount of memory available
    void remove(const T& x);

    // Purpose: removes the "last" element in the vector
    // Postconditions: current size decremented by 1, and memory for data is
    // halved if current size / maximum size <= 0.25 AFTER the removal of x
    // Exceptions: if the vector is empty, then throw std::length_error
    void pop_back();

    // Purpose: accessor function for the current # data values in the vector
    // Returns: current size of the vector 
    unsigned int size() const {return m_size;}

    // Purpose: accessor function for the maximum size of the vector
    // Returns: current maximum size of the vector 
    unsigned int max_size() const { return m_max_size; }

    // Purpose: return a read-and-write reference to the element at 
    // the specified position
    // Parameters: i is 0-based index into the vector
    // Returns: the element at position i in the vector
    // Exceptions: if i < 0 or i >= current size, throw 
    // std::out_of_range("i<0 or i>=m_size")
    T& operator[](unsigned int i);

    // Purpose: return a read-only reference to the element at 
    // the specified position
    // Parameters: i is 0-based index into the vector
    // Returns: the element at position i in the vector
    // Exceptions: if i < 0 or i >= current size, throw 
    // std::out_of_range("i<0 or i>=m_size")
    const T& operator[](unsigned int i) const;

    // Purpose: performs a deep copy of the data from rhs into this vector
    // Parameters: rhs is vector to be copied
    // Returns: *this
    // Postconditions: current size, maximum size, and data set to those of
    // rhs
    Vector<T>& operator=(const Vector<T>& rhs);
};

#include "Vector.hpp"
#endif 
How exactly does this ctor work? It never allocates memory for data...
Vector(const Vector<T>& cpy) { *this = cpy; }

Shouldn't it be...
1
2
3
4
Vector(const Vector<T>& cpy) : m_data(new T[cpy.m_max_size]), m_size(cpy.m_size), m_max_size(cpy.m_max_size)
{
  //do copy stuff here...
}
Ok I see you are using the assignment operator for the copy constructor, that works but i would prefer the copy ctor

There is also a leak here..
1
2
3
4
5
6
7
8
9
10
template <class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
{
  m_size  = rhs.m_size;
  m_max_size = rhs.m_max_size;
  m_data = new T[m_max_size]; //If m_data already points to memory this is now leaking and shouldn't this be rhs.m_max_size
  for(int i = 0; i < m_size; i++)
    m_data[i] = rhs.m_data[i];
  return *this;
}


Also at the very bottom of your header is #include "Vector.hpp" What is that there for?
Last edited on
If I saw what I think I saw, you didn't include the header Vector.hpp in the program, but you included it in itself! it's kinda weir, and definitly isn't gonna work.
let me test it on my comp, and see of it works if I fix it.
Ok, it works, so just remove line 89 from your header and include it in you program. Now, another problem arise: the compiler says: " [Linker error] undefined reference to `WinMain@16' ". What should that mean?
There are two files. The first piece of code is Vector.hpp and the second piece is Vector.h. I'm not sure what the linker error is.
Why does Vector.h need to include Vector.hpp when it doesn't reference anything from that header?
Hi

remove #include<stdexcept> and #include<iostream> from your Vector.hpp and add them to Vector.h file after #include <cstdlib>

So compiler should not have problem with your codes, but your codes still have a lot of bugs, which
must be fixed.


hope it helps
Last edited on
Vector.hpp has the function definitions for Vector.h. How would I fix the leak in the operator = function?
Now, another problem arise: the compiler says: " [Linker error] undefined reference to `WinMain@16' ". What should that mean?
That means you've chosen to build the app as a Windows app. You need to change it to a Console app in the Linker section somewhere.
If Vector.hpp has the function defs for Vector.h, Vector.hpp should be including Vector.h not the other way around.

To fix the memory leak you need to check if that pointer is already pointing to data, if so then free the memory, then allocate memory.
Hi

@clanmjc
clanmjc said
If Vector.hpp has the function defs for Vector.h, Vector.hpp should be including Vector.h not the other way around.


Vector.hpp implements the class-methods, it must not include the vector.h file. On the contrary vector.h must include Vector.hpp, because it is not a source file, not a *.cpp file. That is one of the three ways you may implement Template Class Methods

In your application file you have to include vector.h, tha is all
Last edited on
I copy and pasted his code verbatim, removed the .hpp include from the header, commented out the win includes everything compiled and ran fine? (Minus the crash from the buggy implementation) That is why I asked about the .hpp, it's not used at least not from what I can tell.
Topic archived. No new replies allowed.