This question already has answers here:
Default constructor with empty brackets
                                
                                    (9个答案)
                                
                        
                                3年前关闭。
            
                    
这是取自https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx的示例

#include <iostream>
using namespace std;

class Date
{
    int mo, da, yr;
public:
    Date()
    {
        mo = 10; da = 10; yr = 99;
    }
    Date(int m, int d, int y)
    {
        mo = m; da = d; yr = y;
    }
    friend ostream& operator<<(ostream& os, const Date& dt);
};

ostream& operator<<(ostream& os, const Date& dt)
{
    os << dt.mo << '/' << dt.da << '/' << dt.yr;
    return os;
}

int main()
{
    Date dt0();
    cout << dt0 << endl;

    Date dt(5, 6, 92);
    cout << dt;
}


我希望输出是

10/10/99
5/6/92


但是我得到的是

1
5/6/92


我该怎么办?

最佳答案

欢迎使用C ++,由于most vexing parse!而使您陷入困境

dt0不是对象,而是函数!输出时,实际上是在输出函数dt0的地址,该地址始终为true,即1

您可以将()替换为{},这样就不会产生歧义:

Date dt0{};


或完全删除括号:

Date dt0;

10-08 11:00