首页 文章

成员的错误请求,非类型类型Date(int,int,int)

提问于
浏览
0

我刚刚开始学习类,我在使用我的主要成员函数来打印我想要的数据时遇到了困难 . 这是我得到的错误:

main.cpp:在函数'int main()'中:error:'d'中成员'printNumerical'的请求,它是非类型'Date(int,int,int)'d.printNumerical(); ^

error:请求'd'中的成员'printMonth',它是非类型'Date(int,int,int)'d.printMonth(); ^

错误:请求'd'中的成员'printDateFirst',它是非类型类型'Date(int,int,int)'d.printDateFirst(); ^

这是我的主要内容:

int main ()
{
int Day, Month, Year;

cout << "date information: ";
cin >> Day;
cin >> Month;
cin >> Year;

cout << Day << " " << Month << " " << Year << endl;

Date d (int Day, int Month, int Year);

//where I am having issues
d.printNumerical(); 
d.printMonth();
d.printDateFirst();

return 0;
}

这是我的类定义Date.h

class Date
{
private:
int month,
    day,
    year;

public:
Date(int Day,int Month,int Year); //constructor
Date();                           //constructor if not passed arguments
void printNumerical(); //functions to output in certain format
void printMonthFirst();
void printDateFirst();
};

这是Date.cpp

Date::Date()
{
month = 1;
day = 1;
year = 2001;
}

Date::Date(int Day, int Month, int Year)
{                                        //input validation
if((Month < 1) || (Month > 12) || (Day < 1) || (Day > 31) || (Year < 0)) 
{
month = 1;
day = 1;
year = 2001;
}
else{
        month = Month; //accept passed arguments if valid
        day = Day;
        year = Year;
}
}

void Date::printNumerical()
{
cout << month << "/" << day << "/" << year << endl;
}

void Date::printMonthFirst()
{
switch(month)
{
    case 1 : cout << "January ";
        break;
    case 2 : cout << "February ";
        break;
    case 3 : cout << "March ";
        break;
    case 4 : cout << "April ";
        break;
    case 5 : cout << "May ";
        break;
    case 6 : cout << "June ";
        break;
    case 7 : cout << "July ";
        break;
    case 8 : cout << "August ";
        break;
    case 9 : cout << "September ";
        break;
    case 10 : cout << "October ";
        break;
    case 11 : cout << "November ";
        break;
    case 12 : cout << "December ";
        break;
}
cout << day << ", " ;
cout << year << endl;
}

void Date::printDateFirst()
{
cout << day << " ";
switch(month)
{
    case 1 : cout << "January ";
        break;
    case 2 : cout << "February ";
        break;
    case 3 : cout << "March ";
        break;
    case 4 : cout << "April ";
        break;
    case 5 : cout << "May ";
        break;
    case 6 : cout << "June ";
        break;
    case 7 : cout << "July ";
        break;
    case 8 : cout << "August ";
        break;
    case 9 : cout << "September ";
        break;
    case 10 : cout << "October ";
        break;
    case 11 : cout << "November ";
        break;
    case 12 : cout << "December ";
        break;
}
cout << year << endl;
}

2 回答

  • 0

    以下行:

    Date d (int Day, int Month, int Year);
    

    实际上是函数的前向声明,它将三个整数作为输入并返回 Date 对象 .

    Date d (Day, Month, Year); // proper construction
    
  • 0

    在您的代码中发生此错误的原因是因为您以错误的方式对对象d进行了Decalred:在main函数中,尝试将 Date d (int Day, int Month, int Year); 替换为 Date d (1,1,2001); (或传递任何整数而不是此) .

    好吧,即使在那之后,我注意到你的程序中会有另一个编译时错误 . 您已调用 d.printMonth() (在main()中),该类在该类中不存在 . 我想你想打电话给 d.printMonthFirst() .

    我希望这能解决问题 . 祝你好运!

相关问题