I am trying to call a function based on user input. I cannt figure out what I am doing wrong. I keep getting this error
error: no matching function for call to 'sort(std::vector<int, std::allocator<int> >&)'
Can someone tell me what I am doing wrong. Please explain any suggestions thoroughly as I am brand new to C++. Here is my code:
#include <iterator>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
std::ifstream in("");
std::ofstream out("outputfile.txt");
std::vector<int> numbers;
std::string sortType = "";
std::string file = "";
int main()
{
std::cout << "Which type of sort would you like to perform(sort or mergesort)?\n";
std::cin >> sortType;
std::cout << "Which file would you like to sort?\n";
std::cin >> file;
//Check if file exists
if(!in)
{
std::cout << std::endl << "The File is corrupt or does not exist! ";
return 1;
}
// Read all the ints from in:
copy(std::istream_iterator<int>(in), std::istream_iterator<int>(),
std::back_inserter(numbers));
//check if the file has values
if(numbers.empty())
{
std::cout << std::endl << "The file provided is empty!";
return 1;
} else
{
if(file == "sort")
{
sort(numbers);
}else
{
mergeSort();
}
}
}
void sort(std::vector<int>)
{
// Sort the vector:
sort(numbers.begin(), numbers.end());
unique(numbers.begin(), numbers.end());
// Print the vector with tab separators:
copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\t"));
std::cout << std::endl;
// Write the vector to a text file
copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(out, "\t"));
std::cout << std::endl;
}
void mergeSort()
{
//mergesort code..
}
You need to declare the
sortfunction before you call it. Move its definition abovemain, or putvoid sort(std::vector<int>);beforemain.And the same goes for
mergeSort.You should also fully qualify the call
sort(numbers.begin(), numbers.end());asstd::sort(numbers.begin(), numbers.end());, and the same forcopy,unique. If you don’t, then for technical reasons called “ADL”, which you can look up if you like, then the call only compiles if the arguments that you’re calling it on (the iterators) are classes in namespacestd. It’s up to the specific implementation whether or not they are, so the call won’t work on some compilers.