Can anyone tell me why I am getting the error Undefined Symbol: Vector::Vector(int)?
I am trying to learn the language by making a simple vector class.
The first iteration used doubles as elements. Now I am trying to get it to work with templates so it will work with any type. It was working fine until I did that. I must have implemented it wrong.
Here is the code:
//main.cpp
#include <iostream>
#include "Vector.h"
#include <list>
#include <algorithm>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
Vector<int> a(5);
Vector<int> b(5);
return 0;
}
//vector.h
#ifndef Vector_h
#define Vector_h
template<typename T>
class Vector {
public:
explicit Vector(int s); // constr uctor: establish invariant, acquire resources
~Vector() { delete[] elem; } // destructor: release resources
// ... copy and move operations ...
T& operator[](int i); // for non-const Vectors
const T& operator[](int i) const; // for const Vectors (§4.2.1)
int size() const { return sz; }
friend Vector operator+(const Vector& a, const Vector& b);
Vector(const Vector& a); // copy constructor
Vector& operator=(const Vector& a); // copy assignment
Vector(Vector&& a); // move constructor
Vector& operator=(Vector&& a); // move assignment move assignment operator
T* begin(Vector<T>& x);
T* end(Vector<T>& x);
private:
T* elem; // elem points to an array of sz elements of type T
int sz;
};
//vector.cpp:
#include <stdio.h>
#include "Vector.h"
#include <iostream>
#include <exception>
template<typename T>
Vector<T>::Vector(int s) // definition of the constructor
//:elem{new double[s]}, sz{s} // initialize members or do whats in bottom here.
{
elem = new T[s];
sz = s;
std::cout << "Main Constructor" << std::endl;
}
template<typename T>
T& Vector<T>::operator[](int i) // definition of subscripting
{
return elem[i];
}
template<typename T>
const T& Vector<T>::operator[](int i) const // definition of subscripting
{
return elem[i];
}
template<typename T>
Vector<T> operator+(const Vector<T>& a, const Vector<T>& b)
{
//if (a.size()!=b.size())
// throw Vector_size_mismatch{};
std::cout << "Move Constructor" << std::endl;
Vector<T> res(a.size());
for (int i=0; i!=a.size(); ++i)
res[i]=a[i]+b[i];
return res;
}
template<typename T>
Vector<T>::Vector(const Vector<T>& a) // copy constructor
:elem{new double[a.sz]}, // allocate space for elements
sz{a.sz}
{
for (int i=0; i!=sz; ++i) // copy elements
elem[i] = a.elem[i];
std::cout << "Copy Constructor" << std::endl;
}
template<typename T>
Vector<T>& Vector<T>::operator=(const Vector<T>& a) // copy assignment
{
std::cout << "Copy Assignment Constructor" << std::endl;
std::cout << "Element is " << a[0] << std::endl;
double* p = new double[a.sz];
for (int i=0; i!=a.sz; ++i)
p[i] = a.elem[i];
delete[] elem; // delete old elements
elem = p;
sz = a.sz;
return *this;
}
template<typename T>
Vector<T>::Vector(Vector<T>&& a) // Move construtor
:elem{a.elem}, // "grab the elements" from a
sz{a.sz}
{
a.elem = nullptr; // now a has no elements
a.sz = 0;
}
template<typename T>
Vector<T>& Vector<T>::operator=(Vector<T>&&) { return *this; } // Move assignment
template<typename T>
T* begin(Vector<T>& x)
{
return x.size() ? &x[0] : nullptr; // pointer to first element or nullptr
}
template<typename T>
T* end(Vector<T>& x)
{
return x.size() ? &x[0]+x.size() : nullptr; // pointer to one-past-last element
}