cool, now I wonder why it's not recognizing the function;
I have a vector I'm making using a function that returns a reference to an array but when I try to use it I get a
[code]main.cpp:21: error: no matching function for call to `fillArray(std::istream&)'[code]
here is my header file
Code:
#ifndef ALGOS_H
#define ALGOS_H
#include <iostream>
#include <vector>
template<class T>
std::vector<T> fillArray(std::istream&);
int find_majority(std::vector<int>&);
#endif
and here's my source file
Code:
#include <iostream>
#include <cstdlib>
#include <iterator>
#include "algos.h"
using std::vector;
using std::istream;
using std::istream_iterator;
template<class T>
vector<T> fillArray(istream& in)
{
vector<T> aVec;
istream_iterator<T> inIter(in), eof;
copy(inIter, eof, back_inserter(aVec));
return aVec;
}
int find_majority(std::vector<int>& arr)
{
typedef vector<int>::size_type vSize;
vector<int> possibles;
if(arr.begin() == arr.end())
return -1;
else if(arr.size() == 1)
return arr[0];
else
{
vSize arrSize = arr.size();
if(arrSize % 2 == 1)
{
possibles.push_back(arr[arrSize - 1]);
--arrSize;
}
for(vSize i = 0; i < arrSize; ++i)
{
vSize j = i + 1;
if(arr[i] == arr[j])
possibles.push_back(arr[i]);
++i;
}
}
return find_majority(possibles);
}
and here is the test file
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include "algos.h"
using std::vector;
using std::cout;
using std::cin;
using namespace std;
int main(int argc, char *argv[])
{
vector<int> numArr(fillArray(cin));
int maj = find_majority(numArr);
if(maj < 0)
cout << "\n\nThis array has no majority element";
else
cout << "\n\nThe majority element is: " << maj;
return 0;
}