Calling an "external" function from a class method

I have a function that I want to call from a class method. The function is in a file called mergeSort.cpp. Here is a snippet of the .cpp file that is implemented in the class:

// 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 could not be found. I think this is due to the fact that he is trying to call Servers :: mergeSort. How do I access an external function?

+4
source share
3 answers

You should use the external namespace editor "::":

 ::mergeSort(...); 

This tells the compiler to look for a function in the external namespace. If this particular function is defined in another namespace or class, you must specify it explicitly:

 Namespace::mergeSort(...); 

If you do not want to completely resolve the name each time you use it, you can import the name into the current namespace using:

 using namespace Namespace; 

or

 using Namespace::mergeSort; 

(where Namespace is the name in which mergeShort() defined).

+6
source

There seems to be a couple of issues here. Firstly, are there really Servers::mergeSort ? You assumed it was looking, but you didn’t really say that there is such a thing. If not, this is not a problem. In this case, the probable reason why he cannot see mergeSort is because he is not in the global namespace (as other answers suggested). If Servers::mergeSort exists, see Diego's answer.

A separate issue is that you include the .cpp file (which is usually odd) because mergeSort is a template? If not, you should probably include the accompanying .h, I think. If so, a more ordinary template should include a file with the template code in the header, for example:

 // mergeSort.h // <Begin include guard // <Lots of header stuff> #include "mergeSort.tpp" // <End include guard> 

Then you turn on mergeSort.h elsewhere, and this is another minute for customers to remember.

+1
source

Make sure mergeSort() is in the specific namespace .

0
source

All Articles