Custom Vector Implementation

So I'm trying to implement a custom vector class and I'm running into compile errors. Also, my code may or may not be totally off. Any help is much appreciated.


MyVector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef __MYVECTOR_H__
#define __MYVECTOR_H__

const int DEFAULT_VECTOR_SIZE = 0;

template<typename T>
class MyVector{
      private:
              int vtr_size;
              T *elements;  // = elements[vtr_size]
              
      public:
             MyVector<T>();
             MyVector<T>(int size);
             //other function prototypes are here
};

#endif;


MyVector.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
#include "MyVector.h"


MyVector::MyVector<T>()
{
 vtr_size = 0;
}
                        

MyVector::MyVector<T>(int size)
{                         
 vtr_size = size;
 elements = new T[vtr_size];
 
 while(size >= DEFAULT_VECTOR_SIZE)
 {
  elements = (T*)realloc(elements, vtr_size* sizeof(T));
  size--;
 }

 for(int i=0; i<size; i++)
 {
  elements[vtr_size] = 0;
 }
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdlib>
#include <iostream>
#include <string>
#include "MyVector.h"

using namespace std;

int main(int argc, char *argv[])
{
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
closed account (Dy7SLyTq)
what are the errors?
Errors are here.

http://imgur.com/NTVqcwk
closed account (Dy7SLyTq)
You have a ; at the end of the endif
1
2
3
4
MyVector::MyVector<T>()
{
 vtr_size = 0;
}

Your syntax is wrong.

See here: http://www.cplusplus.com/doc/tutorial/templates/
Scroll down to "Class templates"
You cannot use realloc with new.
Made some fixes. Also, I'm not sure I understand why realloc is wrong here, I'm using it with elements, not new T. At the moment I'm getting errors with my function calls in main.cpp

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <string>
#include "MyVector.h"

using namespace std;

int main(int argc, char *argv[])
{
    MyVector<int>MyVector();

    MyVector<int>MyVector(5);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


MyVector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef __MYVECTOR_H__
#define __MYVECTOR_H__

const int DEFAULT_VECTOR_SIZE = 0;

template<typename T>
class MyVector{
      private:
              int vtr_size;
              T *elements;
              
      public:
             MyVector();
             MyVector(int size);
             //other function prototypes
};

#endif 


MyVector.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
#include "MyVector.h"

template<typename T>
T MyVector<T>::MyVector()
{
 vtr_size = 0;
}
                        
template<typename T>
T MyVector<T>::MyVector(int size)
{                         
 vtr_size = size-1;
 elements = new T[vtr_size];
 
 while(size >= DEFAULT_VECTOR_SIZE)
 {
  elements = (T*)realloc(elements, vtr_size* sizeof(T));
  size--;
 }

 for(int i=0; i<vtr_size; i++)
 {
  elements[vtr_size] = 0;
 }
}



Errors
1
2
3
main.cpp:
12: expected primary expression before "int"
12: expected ';' before "int"

Constructors don't have a return type.

Read the last section "Templates and multiple-file projects":
http://www.cplusplus.com/doc/tutorial/templates/

Then you would instantiate objects like so:
1
2
3
MyVector<int> v1;

MyVector<int> v2(5);
Got that a few minutes before your comment. now getting a Linker error. Anyways, I think I've got this. thanks a bunch guys.
Or not.
Getting:

[Linker Error] undefined reference to 'MyVector<int>::MyVector(int)'
ld (Id?) returned 1 exit status
[Build error] error 1

This is running on Dev C++ as a requirement for my class. I understand it's saying I need to implement that function, but it is clearly implemented in MyVector.cpp....
closed account (zb0S216C)
http://www.cplusplus.com/forum/general/101554/#msg545850
http://www.cplusplus.com/forum/beginner/63833/#msg345417 (last post)

Wazzak
From the link in my last post:
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.
Oh wow, thanks so much. I would have never figured that out, didn't even know what to look for. See my reasoned logic is great, but my computer logic is pretty fuzzy still, and I don't known as much as I should to be honest. Thanks for the help!


Is this right because it's still not wanting to work....
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
#ifndef __MYVECTOR_H__
#define __MYVECTOR_H__

const int DEFAULT_VECTOR_SIZE = 0;

template<typename T>
class MyVector{
      private:
              int vtr_size;
              T *elements;
              
      public:
             MyVector();
             MyVector(int size);
             /*
             void push_back(T element);
             void pop_back();
             unsigned int size();
             T at(int index);
             bool empty();
             void clear();
             void swap(MyVector v2);
             */
};

template<typename T>
MyVector<T>::MyVector(){
 vtr_size = 0;
}
                       
template<typename T>
MyVector<T>::MyVector(int size){                         
 vtr_size = size-1;
 elements = new T[vtr_size];
 
 while(size >= DEFAULT_VECTOR_SIZE)
 {
  elements = (T*)realloc(elements, vtr_size* sizeof(T));
  size--;
 }

 for(int i=0; i<vtr_size; i++)
 {
  elements[vtr_size] = 0;
 }
}
#endif 
Last edited on
What errors do you get?
Just the Linker error. "Undefined reference" same one.
Compiles fine here. I copied your MyVector.h, added #include <cstdlib> at line 3 and used the following main.cpp:
1
2
3
4
5
6
#include "MyVector.h"

int main(){
    MyVector<int> v1;
    MyVector<int> v2(5);
}


I'm compiling from the command line on a Linux box. I'm not familiar with Dev C++. Do you need to clean your project (run make clean)?
Oh, that worked. Toldja I'm not very good at this. Well that problem took entirely too long to fix. Thanks again guys.
Topic archived. No new replies allowed.