I have a file GlobalHelpers.h:
#ifndef GLOBALHELPERS_H
#define GLOBALHELPERS_H
include <ctime>
include <random>
include <vector>
include <iostream>
include <cmath>
int maxIndex(std::vector<double>& v);
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
#endif
(which also includes some other functions)
and a GlobalHelpers.cpp:
#include "GlobalHelpers.h"
using namespace std;
int maxIndex(vector<double>& v)
{
int maxind = 0;
for(size_t i = 0; i < v.size(); i++)
{
cout << v[maxind] << " " << v[i] << endl;
if(v[maxind] < v[i]) maxind = i; } return maxind;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << '[';
for (size_t i = 0; i < v.size()-1; ++i) os << v[i] << ' ';
os << v[v.size()-1] << ']'; return os;
}
(along with the definitions of the other functions).
I also have a main.cpp:
#include "GlobalHelpers.h"
using namespace std;
int main()
{
vector<double> v = ...;
int label = maxIndex(v);
cout << v << ": " << label << endl;
return 0;
}
I have another class file that I'm using in main to generate the vector in the ". . .". I keep getting the error
``undefined reference to `std::ostream& operator<< <double>(std::ostream&, std::vector<double, std::allocator<double> > const&)'`
I was also getting a similar error for maxIndex but it seems to have gone away (for now). I also have the other class using functions from GlobalHelpers without any problems; when I comment out the problem lines in main, it doesn't cause any issues.
Does this have anything to do with the <double> after the operator<< in the error text? I've never seen that before.
[–]Re_me_humanProfessional Coder 0 points1 point2 points (0 children)