定义函数以在 C++ 中返回结构

zom*_*000 -1 c++ struct function

using namespace std;
#include <iostream>
#include <cstring>

struct distance {
    int Kilometer;
    int Meter;
    int Centimeter;
};

distance add() {
}

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

试图定义一个返回“距离”数据类型的函数。在尝试定义函数时,我遇到了“距离不明确”的问题。

Tas*_*Tas 6

正如@1202ProgramAlarm 在评论中指出的那样,这是您永远不应该包含using namespace std;. 因为您已经引入了std命名空间,所以编译器不确定distance您的意思:您的 custom struct distance, or std::distance,因此“距离不明确”。

最简单和最好的解决方法是不使用using namespace std. 当然,您可能会多花一些时间写作,std::但您可以避免这些头痛。

您也可以将您的名称重命名structDistance,或者您可以使用::distance add()