Vectors

Can someone help me implement enough functionality my my_vector.cpp file to appease the basic main.cpp? I was given the main.cpp and my_vector.h files. I also cannot edit the main.cpp and my_vector.h files.

my_vector.cpp
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
#include<iostream>
#include <string>
#include "my_vector.h"

using namespace std;

my_vector::my_vector()
{

}

my_vector::my_vector(unsigned int size)
{

}
my_vector::my_vector(unsigned int size, const T & initial)
{

}
my_vector::my_vector(const my_vector & v)
{

}
my_vector::~my_vector()
{

}
int my_vector::capacity() const
{

return 0;
}
unsigned int my_vector::size() const
{

return 0;
}

bool my_vector::empty() const
{
 return size() == 0;
}
void my_vector::push_back(const T & value)
{

}
void my_vector::pop_back() 
{

}    
void my_vector::reserve(unsigned int new_capacity)
{


}
void my_vector::resize(unsigned int new_size)
{

}


my_vector.h

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

#ifndef MY_VECTOR_H
#define MY_VECTOR_H

// my_vector.h
using namespace std;

template <class T>
class my_vector
{
  public:
    // Constructors / Destructor
    my_vector(); 
    // Constructor with initial size
    my_vector(unsigned int size);
    // Constructor with initial size -- set all initial spaces to initial param
    my_vector(unsigned int size, const T & initial);    
    // Copy constructor
    my_vector(const my_vector & v); 
    ~my_vector(); 

    // Return capacity of vector (in elements)
    int capacity() const; 
    // Return the number of elements in the vector
    unsigned int size() const; 
    // Return true if vector has no elements
    bool empty() const; 

    // Add a new element to the end of the backing array 
    // Don't forget to resize if needed!
    void push_back(const T & value); 
    // Remove the last element (don't return it)
    void pop_back(); 
    // Adjust capacity (make more space as needed)
    void reserve(unsigned int new_capacity); 
    // Resize vector (even if that means making it smaller)
    void resize(unsigned int new_size); // adjust size

    /* Overloaded operators */
    // Return a *reference* to numbered element
    T & operator [ ](unsigned int index) const; 

  private:
     unsigned int my_size;
     unsigned int my_capacity;
     T * buffer;
};

/* put all your code in my_vector.cpp */
#include "my_vector.cpp"

#endif


main.cpp
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
// main.cpp

#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include "my_vector.h"

using namespace std;

int main(char* args[])
{
  // Swap these to test against the STL vector
  //#define VECTOR vector
  #define VECTOR my_vector

  VECTOR v;

  v.reserve(2);
  assert(v.capacity() == 2);

  VECTOR v1(2);
  assert(v1.capacity() == 2);
  assert(v1.size() == 2);
  assert(v1[0] == "");
  assert(v1[1] == "");
}



When i run it i get this error:
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
my_vector.cpp:7: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:7: error: ISO C++ forbids declaration of ‘my_vector’ with no type
my_vector.cpp: In function ‘int my_vector()’:
my_vector.cpp:7: error: ‘int my_vector()’ redeclared as different kind of symbol
my_vector.h:8: error: previous declaration of ‘template<class T> class my_vector’
my_vector.cpp: At global scope:
my_vector.cpp:12: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:12: error: ISO C++ forbids declaration of ‘my_vector’ with no type
my_vector.cpp: In function ‘int my_vector(unsigned int)’:
my_vector.cpp:12: error: ‘int my_vector(unsigned int)’ redeclared as different kind of symbol
my_vector.h:8: error: previous declaration of ‘template<class T> class my_vector’
my_vector.cpp: At global scope:
my_vector.cpp:16: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:16: error: ISO C++ forbids declaration of ‘T’ with no type
my_vector.cpp:16: error: expected ‘,’ or ‘...’ before ‘&’ token
my_vector.cpp:16: error: ISO C++ forbids declaration of ‘my_vector’ with no type
my_vector.cpp: In function ‘int my_vector(unsigned int, int)’:
my_vector.cpp:16: error: ‘int my_vector(unsigned int, int)’ redeclared as different kind of symbol
my_vector.h:8: error: previous declaration of ‘template<class T> class my_vector’
my_vector.cpp: At global scope:
my_vector.cpp:20: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:20: error: ISO C++ forbids declaration of ‘my_vector’ with no type
my_vector.cpp:20: error: expected ‘,’ or ‘...’ before ‘&’ token
my_vector.cpp:20: error: ISO C++ forbids declaration of ‘my_vector’ with no type
my_vector.cpp: In function ‘int my_vector(int)’:
my_vector.cpp:20: error: ‘int my_vector(int)’ redeclared as different kind of symbol
my_vector.h:8: error: previous declaration of ‘template<class T> class my_vector’
my_vector.cpp: At global scope:
my_vector.cpp:24: error: expected constructor, destructor, or type conversion before ‘::’ token
my_vector.cpp:28: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:28: error: non-member function ‘int capacity()’ cannot have cv-qualifier
my_vector.cpp:33: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:33: error: non-member function ‘unsigned int size()’ cannot have cv-qualifier
my_vector.cpp:39: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:39: error: non-member function ‘bool empty()’ cannot have cv-qualifier
my_vector.cpp:43: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:43: error: ISO C++ forbids declaration of ‘T’ with no type
my_vector.cpp:43: error: expected ‘,’ or ‘...’ before ‘&’ token
my_vector.cpp:47: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:51: error: ‘template<class T> class my_vector’ used without template parameters
my_vector.cpp:56: error: ‘template<class T> class my_vector’ used without template parameters
main.cpp:11: warning: first argument of ‘int main(char**)’ should be ‘int’
main.cpp:11: warning: ‘int main(char**)’ takes only zero or two arguments
make: *** [main] Error 1

Last edited on
line 8 my_vector.h

template <class T>
OK i put that in. I guess my teacher forgot to put that piece in. Can you check my my_vector.cpp file to see what i need to appease the basic main.cpp? thank you so much for help.
Topic archived. No new replies allowed.