我有一个我想在类方法中调用的函数。 该函数位于名为mergeSort.cpp的文件中。 以下是该类实现的.cpp文件的片段:
// other includes #include "mergeSort.cpp" // other methods void Servers::sortSites() { mergeSort(server_sites.begin(), server_sites.end(), siteCompare); } // remaining methods当我尝试编译时,我得到错误,说无法找到mergeSort。 我认为这是因为它试图调用Servers :: mergeSort。 我怎么去调用外部函数?
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 methodsWhen 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?
最满意答案
您必须使用“::”外部命名空间解析器:
::mergeSort(...);这告诉编译器在外部命名空间中查找该函数。 如果在另一个名称空间或类中定义了此特定函数,则必须明确指定它:
Namespace::mergeSort(...);如果您不想在每次使用时完全解析名称,可以使用以下命令将名称导入当前名称空间:
using namespace Namespace;要么
using Namespace::mergeSort;(其中Namespace是定义mergeShort()的名称)。
You have to use the "::" external namespace resolutor:
::mergeSort(...);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:
Namespace::mergeSort(...);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:
using namespace Namespace;or
using Namespace::mergeSort;(where Namespace is the name in which mergeShort() is defined).
更多推荐
发布评论