【问题标题】:error request for member which is of non class type Date (int,int,int)对非类类型 Date (int,int,int) 的成员的错误请求
【发布时间】:2017-03-23 22:43:48
【问题描述】:

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

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

错误:在“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;
}

【问题讨论】:

  • 您希望Date d (int Day, int Month, int Year); 做什么?它是 not 构造函数调用,它是函数声明。考虑阅读good book 来学习。

标签: c++ class compiler-errors


【解决方案1】:

下面一行:

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多