在C ++中用class预期的nonqualified-id错误?

时间:2014-11-03 02:29:17

标签: c++ class

我的代码中有一些错误,我无法弄清楚如何修复它们。我试图谷歌类似的错误,但我无法解决我一直有的问题。

继承我的代码:

#include <iostream>

using namespace std;

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

public:
    void setDay(int);
    void setMonth(int);
    void setYear(int);
    int getDay() const;
    int getMonth() const;
    int getYear() const;
    int getDate() const;

};


void Date::setDay(int d)
{
day = d;
}

void Date::setMonth(int m)
{
month = m;
}



void Date::setYear(int y)
{
year = y;
}


int Date::getDay() const;
{   
return day;
}



int Date::getMonth() const;
{
return month;
}

int Date::getYear() const;
{
return year;
}



int main() {

Date dat;
int datDay; //local variable for day
int datMon; //local variable for month
int datYea; //local variable for year

cout << "What is the day?";
cin >> datDay;

cout << "What is the month?";
cin >> datMon;

cout << "What is the year?";
cin >> datYea;

dat.setDay(datDay);
dat.setMonth(datMon);
dat.setYear(datYea);

//display
cout << "Day: " << dat.getDay() << endl;
cout << "Month: " << dat.getMonth() << endl;
cout << "Year: " << dat.getYear() << endl;
return 0;

}

这是我的错误:

gr_hw8.cpp:26:1: error: expected unqualified-id
void Date::setDay(int d)
^
gr_hw8.cpp:33:1: error: expected unqualified-id
void Date::setMonth(int m)
^
gr_hw8.cpp:40:1: error: expected unqualified-id
void Date::setYear(int y)
^
gr_hw8.cpp:47:1: error: expected unqualified-id
int Date::getDay() const;
^
gr_hw8.cpp:48:1: error: expected unqualified-id
{       
^
gr_hw8.cpp:54:1: error: expected unqualified-id
int Date::getMonth() const;
^
gr_hw8.cpp:55:1: error: expected unqualified-id
{
^
gr_hw8.cpp:60:1: error: expected unqualified-id
int Date::getYear() const;
^
gr_hw8.cpp:61:1: error: expected unqualified-id
{
^
gr_hw8.cpp:68:1: error: expected unqualified-id
int main() {
^
10 errors generated.

Thannks !!

1 个答案:

答案 0 :(得分:3)

就像尼尔在评论中所说的那样

int Date::getDay() const;
{   
return day;
}

应该是

int Date::getDay() const
{   
return day;
}

所以它也适用于getMonth和getYear

getDate的定义在哪里?