I am new to C++ and am having difficulties resolving an Undefined Reference problem that I am running into. I am trying to create a test class that takes as input an array into a constructor, and everything seems to work if I have everything located in one file as below:
main.cpp
#include <iostream>
class Test
{
public:
template<typename T,int SIZE>
Test(T (&array)[SIZE]);
};
template<typename T,int SIZE>
Test::Test(T (&array)[SIZE])
{
std::cout << "Array constructor was called" << std::endl;
std::cout << "Size of array is: " << SIZE << std::endl;
}
int main()
{
int myIntArray[10];
Test myTest(myIntArray);
return 0;
}
When I run this example, I get the following output:
Array constructor was called
Size of array is: 10
However, when I break this example up into the following three files:
Test.h
class Test
{
public:
template<typename T,int SIZE>
Test(T (&array)[SIZE]);
};
Test.cpp
#include "Test.h"
#include <iostream>
template<typename T,int SIZE>
Test::Test(T (&array)[SIZE])
{
std::cout << "Array constructor was called" << std::endl;
std::cout << "Size of array is: " << SIZE << std::endl;
}
main.cpp
#include "Test.h"
#include <iostream>
int main()
{
int myIntArray[10];
Test myTest(myIntArray);
return 0;
}
I receive an undefined reference to Test::Test<int, 10>(int (&) [10])'
. I am not exactly sure what I am doing incorrectly and am thinking that perhaps I am overlooking something. Any insight would be appreciated. Thank you for the help.