I have a class using templates, when I write it using just one .cpp file it works fine, but when I try to the class into its own .cpp and .hpp file I get an error message: The preLaunchTask "C/C++: g++ build active file" terminated with exit code 1, but when I click on "show errors" it tells me that no problems have been detected in the workspace so far.
It also works when I try to separate just a function, it means that linker is set up correctly
HelloWorld.hpp
#pragma once
#include <iostream>
void HelloWorld();
HelloWorld.cpp
#include "HelloWorld.hpp"
void HelloWorld()
{
std::cout<< "Hello World"<< std::endl;
}
main.cpp
#include "HelloWorld.hpp"
int main()
{
HelloWorld();
}
The snippet above is just a pseudo-code, so there might be errors, but when I run it properly written it works, but when I try to do something more complicated, like class definition it does not work.
this is my relevant code
myArray.hpp
# pragma once
# include<iostream>
#ifndef N_PLAN
#define N_PLAN 2 // Planned length of the arrays
#endif
template<typename T, size_t S>
class myArray
{
public:
int arraySize();
T& operator[](size_t index);
T* arrayData();
const T* arrayData() const;
private:
T m_Data[S];
myArray.cpp
#include"myArray.hpp"
template<typename T, size_t S>
class myArray
{
public:
constexpr int arraySize() const {return S; }
T& operator[](size_t index) {return m_Data[index]; }
T* arrayData() { return m_Data; }
const T* arrayData() const { return m_Data; }
private:
T m_Data[S];
};
main.cpp
#include "myArray.hpp"
int main()
{
myArray<int, N_PLAN> data;
data[0] = 0;
data[1] = 196996;
for(size_t i = 0; i <data.arraySize(); i++)
{
std::cout<< data[i] <<std::endl;
}
}