在结构中使用大写字母和小写字母的区别

Difference in using capital and small letters within a structure

当我使用 "distance" 和 "time" 作为我的结构的标识符名称时,出现错误。编译器说 对 "distance" 的引用不明确。

#include <iostream>
using namespace std;
struct distance{
    float feet;
    float inches;
};
struct time {
    int hrs;
    int mins;
    int secs;
};
int main()
{
    struct tour {
        distance d;
        time t;
    };
    return 0;
}

但是当我使用大写字母时,"Distance"和"Time",

#include <iostream>
using namespace std;
struct Distance{
    float feet;
    float inches;
};
struct Time {
    int hrs;
    int mins;
    int secs;
};
int main()
{
    struct tour {
        Distance d;
        Time t;
    };
    return 0;
}

编译器没有显示任何错误。谁能告诉我原因吗?

std 库中有一个名为 distance and time 的成员。因此编译器不确定你调用的是哪个 distance/time 。如果你放弃你的 "using namespace std" 那么你可以用小写定义你的结构并且你的代码仍然有效。否则,您将需要重新定义结构的名称,例如首字母大写。