I am learning cpp and I am creating a Vector class using templates, namespaces, header files... . Now this is my code so far
main.cpp
#include "my_vector.h"
#include <string>
#include <iostream>
int main(){
MY::Vector<int> vector;
}
my_vector.cpp
#include "my_vector.h"
#include <string>
#include <iostream>
// implementation of all the constructors
template <class T>
MY::Vector<T>::Vector(){
size = 0;
alloc = 20;
ptr = new T[alloc];
}
template <class T>
std::ostream& operator <<(std::ostream& out, const MY::Vector<T> vector){
for (auto iterator = vector.ptr; iterator < vector.size; iterator++){
out << *iterator;
}
return out;
}
template <class T>
MY::Vector<T>::~Vector(){delete[] ptr; alloc = 0; size = 0;}
my_vector.h
#ifndef MY_VECTOR_H
#define MY_VECTOR_H
#include <string>
#include <iostream>
namespace MY{
template <class T>
class Vector{
public:
// these are going to be some friend functions
// we are going to overload the operator << to print the content of the vector
friend std::ostream& operator<<(std::ostream& out, const Vector<T>& vector);
public:
// we need to store three things
// pointer to the first address in memory
// size of the vector so far
// actual space allocated
T* ptr;
size_t size;
size_t alloc;
public:
// we need 2 constructors
// default constructor
Vector<T>();
// custom constructor
Vector<T>(T value);
// overloaded custom constructor
Vector<T>(int cells, T value);
// destructor
~Vector<T>();
// Now the copy constructor
Vector<T>(const Vector<T>& other);
// Now the move constructor
Vector<T>(Vector<T>&& other);
// now a copy assignment
Vector<T>& operator=(const Vector<T>& other);
// now a move assignment
Vector<T>& operator=(Vector<T>&& other);
public:
// these are all the functions that we need
bool isEmpty();
size_t get_size();
size_t get_alloc();
void push_back(T value);
void push_front(T value);
void pop_back();
void pop_front();
public:
// these are the overloaded operations
// symbol > if one vector is greater than another one
// opposite goes for < operator;
// operator << to print the content of the vector using friend functions
// operator + to add two vector together
bool operator>(const Vector<T>& other);
bool operator<(const Vector<T>& other);
Vector<T>& operator+(const Vector& other);
};
}
#endif
As you can see most of the stuff is just declared. However I already implemented the default constructor and the destructor to test if everything is fine so far, and it seems it is not as I am getting this error
Undefined symbols for architecture x86_64:
"MY::Vector<int>::Vector()", referenced from:
_main in main-08d713.o
"MY::Vector<int>::~Vector()", referenced from:
_main in main-08d713.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
run.sh: line 3: ./output: No such file or directory
additionally I am compiling using this a bash script like this
#!/bin/bash
g++ *.cpp -o output -std=c++17
./output
I have also tried simply
g++ *.cpp
./a.out
I cannot really understand where is the problem. Could someone help?
Aucun commentaire:
Enregistrer un commentaire