I've been told to write a function that will allow any data type of vector given to it and return the intersection of two vectors (very little error handling was required, which kind of irks me, but never mind that). To handle this I've written the following vectorHelper.h
header file:
template <typename t>
std::vector<t> intersect( const std::vector<t> & v1, const std::vector<t> & v2);
the following vectorHelper.cpp
file:
#include <cmath>
#include <vector>
#include "vectorHelper.h"
using namespace std;
template <typename t>
vector<t> intersect( const vector<t> & v1, const vector<t> & v2)
{
vector<t> results;
for (int i=0; i<v1.size(); i++)
{
for (int j=0; j<v2.size(); j++)
{
if (v1[i] == v2[j])
{
results.push_back(v1[i]);
break;
}
}
}
return results;
}
and the following main.cpp
file to run the method:
#include <iostream>
#include <string>
#include <limits>
#include <vector>
#include "vectorHelper.h"
using namespace std;
int main()
{
const int CAPACITY = 5;
vector<string> v1;
vector<string> v2;
string input;
cout << "Enter five strings for vector 1: ";
for (int i=0; i<CAPACITY; i++)
{
cin >> input;
v1.push_back(input);
}
cin.ignore(INT_MAX, '\n');
cout << "\n";
cout << "Enter five strings for vector 2: ";
for (int i=0; i<CAPACITY; i++)
{
cin >> input;
v2.push_back(input);
}
vector<string> results = intersect(v1, v2);
cout << "The common strings ( the intersection ): ";
for (int k=0; k<results.size(); k++)
cout << results[k] << " ";
}
and building and running within Code::Blocks gives the following error:
g++ -o bin/Debug/Problem3 obj/Debug/main.o obj/Debug/vectorHelper.o
Undefined symbols for architecture x86_64:
"std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > intersect<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Classmates of mine have compiled similar programs on their own machines (we all run Mac OSX 10.8.2 on MacBook Pros), but when given my project, they get the same error on their machines. Therefore, my initial thought that I had some install of Code::Blocks bug has been "disproven" (possibly). Ultimately, I need the program to compile and run in Code::Blocks on a 32-bit machine. Does anyone know how to fix these errors?
UPDATE BEFORE ORIGINAL POST: I just found this question that says templates need to be defined in the header file COMPLETELY. However, my classmates don't seem to be limited as such. Defining my template in the header file does fix the issue, but is there some way that would not require that?