C++:如何从我自己的函数中的头文件中调用一个函数(比如 funA()),它的名字也是 funA()?

Ris*_*ena 1 c++ overloading namespaces function name-conflict

我想reverse(BidirectionalIterator first, BidirectionalIterator last)<algorithm>我的函数内部的头文件中调用一个函数,它的名字也是reverse(int).

代码:

#include<iostream>
#include<algorithm>

using namespace std;

class Solution{
public:
    int reverse(int x){
        string num = to_string(x);
        reverse(num.begin(), num.end());
    }
};
Run Code Online (Sandbox Code Playgroud)

我认为它会根据传递的参数自动调用适当的函数,就像函数重载一样。但是,它没有。

我试过:

namespace algo{
    #include<algorithm>
}
Run Code Online (Sandbox Code Playgroud)

但它给出了很多错误。

sco*_*001 5

啊,现在您正在体验 StackOverflow 上的人们总是大喊不要使用using namespace std;. 问题是您将整个命名空间带入全局命名空间,这会导致这样的冲突。

但是,如果删除该行,现在所有导入的函数都保留在std命名空间中,因此您可以执行以下操作:

#include<iostream>
#include<algorithm>

// BAD
// using namespace std;

class Solution{
public:
    int reverse(int x){
        std::string num = std::to_string(x);
        std::reverse(num.begin(), num.end());
        return std::stoi(num); // Don't forget to return!
    }
};
Run Code Online (Sandbox Code Playgroud)