0

I am trying to write a function to print std::vectors as

template <class T>
void PrintVector(const std::vector<T> vec)
{
     for(unsigned int j=0;j<vec.size();j++){ std::cout << vec[j] << "\t"; }
     std::cout << "\n";
}

This works if I put in the main file of the project. However when I put it in an external file (e.g. functions.cpp and functions.hpp) as

File functions.hpp:

#ifndef functions_hpp
#define functions_hpp

#include <iostream>
#include <vector>

template <class T> 
void PrintVector(const std::vector<T> vec);

#endif 

File functions.cpp:

#include "functions.hpp"

template <class T>
void PrintVector(const std::vector<T> vec)
{
     for(unsigned int j=0;j<vec.size();j++){ std::cout << vec[j] << "\t"; }
     std::cout << "\n";
}

I receive the following compile error:

Undefined symbols for architecture x86_64:
"void PrintVector<int>(std::__1::vector<int, std::__1::allocator<int>   >)", referenced from:
  _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see     invocation)

All the other functions defined in functions.cpp work. How to solve this issue?

Galuoises
  • 2,630
  • 24
  • 30

0 Answers0