从类方法调用“外部”函数(Calling an “external” function from a class method)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
从类方法调用“外部”函数(Calling an “external” function from a class method)

我有一个我想在类方法中调用的函数。 该函数位于名为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 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?

最满意答案

您必须使用“::”外部命名空间解析器:

::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).

更多推荐

本文发布于:2023-04-21 18:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ec5cda6779ed7fe506b93b3b92d627f0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   方法   Calling   external   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!