I have a function that I want to call from within a class method. The function is in a file called mergeSort.cpp. Here is a snippet of the .cpp file that the class is implemented in:
// other includes
#include "mergeSort.cpp"
// other methods
void Servers::sortSites() {
mergeSort(server_sites.begin(), server_sites.end(), siteCompare);
}
// remaining methods
When I try to compile I get errors saying that mergeSort can’t be found. I think this is because it’s trying to call Servers::mergeSort. How would I go about calling an external function?
You have to use the “::” external namespace resolutor:
This tells the compiler to look for the function in the outer namespace. If this particular function is defined in another namespace or class, you have to specify it explicitly:
If you don’t want to have to resolve the name completely each time you use it, you can import the name into the current namespace by either using:
or
(where
Namespaceis the name in whichmergeShort()is defined).