前进声明错误驱使我坚果

时间:2014-10-31 04:56:27

标签: c++

好的,我知道新手C ++用户会对此提出很多要求,因为我已经阅读了很多帖子,但是即使在阅读完答案后我也没有看到我做错了什么。代码很长,所以......尽可能宽容。

#include "dateType.h"

using namespace std;

void dateType::setDate(int y, int m, int d)
{
  y = year;
  m = month;
  d = day;
}

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

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

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

void dateType::getDate(int& year, int& month, int& day) const
{
  return (year, month, day);
}

bool dateType::operator==(const dateType& otherDate) const
{
  return (getYear() == otherDate.getYear() && getMonth() == otherDate.getMonth()
      && d == otherDate.getDay());
}

bool dateType::operator!=(const dateType& otherDate) const
{
  return !(*this == otherDate);
}

bool dateType::operator<(const dateType& otherDate) const
{
  return (getYear() < otherDate.getYear() || getMonth() < otherdate.getMonth() 
      || getDay() < otherDate.getDay());
}

bool dateType::operator>(const dateType& otherDate) const
{
  return (getYear() > otherDate.getYear() || getMonth() > otherDate.getMonth()
      || getDay() > otherDate.getDay());
}

bool dateType::operator<=(const dateType& otherDate) const
{
  return *this == otherDate || *this < otherDate;
}

bool dateType::operator>=(const dateType& otherDate) const
{
  return *this == otherDate || *this > otherDate;
}

ostream& operator<<(ostream& out, const dateType& d)
{
  out << dateType.getYear() << "-" << dateType.getMonth() << "-" << dateType.getDay();
  return out;
}

istream& operator>>(istream& in, dateType& d)
{
  //Variables
  int year,
      month,
      day;
  char dummy;

  in >> year >> dummy >> month >> dummy >> day;
  d.setDate(year, month, day);
  return in;
} 

dateType::dateType()
{
  year = 0000;
  month = 00;
  day = 00;
}

dateType::dateType(int y, int m, int d)
{
  setDate(y, m, d);
}

我的.h文件是:

#ifndef _DATETYPE_H_
#define _DATETYPE_H_

#include <iostream>

using namespace std;

class dateType
{
  public:
    /* Method:  Default contructor
     * Description:  Constructs a new dateType object
     * Pre-conditions:  None
     * Post-conditions:  dateType object created and initialized to a date of
     * '0000-00-00' (format yyyy-mm-dd)
     * Method input:  Year (int), Month (int), Day (int)
     * Method output:  None
     */
    dateType();

    /* Method:  Constructor
     * Description:  Constructs a new dateType object
     * Pre-conditions:  None
     * Post-conditions:  dateType object create and initialized to a date of
     * format yyyy-mm-dd
     * Method input:  year (int), month (int), day (int)
     * Method output:  None
     */
    dateType(int, int, int);

    /* Method:  setDate
     * Description:  Sets the date of a dateType object
     * Pre-conditions:  dateType object has been initialized
     * Post-conditions:  new year, month and day has been set for dateType 
     * object
     * Method input:  year(int), month(int), day(int)
     * Method output:  None
     */
    void setDate(int y, int m, int d);

    /* Method:  getYear
     * Descritpion:  Returns the year of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object year has been returned
     * Method input:  None
     * Method output:  Year of the dateType (int)
     */
    int getYear() const;

    /* Method:  getMonth
     * Descritpion:  Returns the month of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object month has been returned
     * Method input:  None
     * Method output:  Month of the dateType object (int)
     */
    int getMonth() const;

    /* Method:  getDay
     * Description:  Returns the day of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object month has been returned
     * Method input:  None
     * Method output:  Day of the dateType object (int)
     */
    int getDay() const;

    /* Method:  getDate
     * Description:  Returns the year, month, and day of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object year, month and day has been returned
     * Method input:  None
     * Method output:  year (int), month(int), and day(int) of the dateType
     * object
     */
    void getDate() const;

  private:
    int year;
    int month;
    int day;
};

ostream& operator<<(ostream&, const dateType);
#endif

我还在处理.h文件,但我试图在main.cpp文件中测试类的初始化。

dateType first_date(2014, 10, 31);

我收到了一些错误,但首先是: dateType.cpp:13:43:错误:无效使用不完整类型'class dateType'  void dateType :: setDate(int y,int m,int d)

请告诉我我错过了什么。

非常感谢。

3 个答案:

答案 0 :(得分:0)

你想回来什么?。

void dateType::getDate(int& year, int& month, int& day) const
{
  return (year, month, day); <<<<<<<<<<<<<
}

请将此更改为

dateType dateType::getDate(int& year, int& month, int& day) 
{
  return dateType(year, month, day);
}

答案 1 :(得分:0)

你的setDate函数错误。它应该是

   void dateType::setDate(int y, int m, int d)
   {
      year = y;
      month = m;
      day = d;
   } 

你也不需要在.h

中声明
ostream& operator<<(ostream&, const dateType);

答案 2 :(得分:0)

我已更正所有错误,请检查以下文件

检查链接http://ideone.com/nfdYo5

你的.h文件

#ifndef _DATETYPE_H_
#define _DATETYPE_H_

#include <iostream>

using namespace std;

class dateType
{
  public:
    /* Method:  Default contructor
     * Description:  Constructs a new dateType object
     * Pre-conditions:  None
     * Post-conditions:  dateType object created and initialized to a date of
     * '0000-00-00' (format yyyy-mm-dd)
     * Method input:  Year (int), Month (int), Day (int)
     * Method output:  None
     */
    dateType();

    /* Method:  Constructor
     * Description:  Constructs a new dateType object
     * Pre-conditions:  None
     * Post-conditions:  dateType object create and initialized to a date of
     * format yyyy-mm-dd
     * Method input:  year (int), month (int), day (int)
     * Method output:  None
     */
    dateType(int, int, int);

    /* Method:  setDate
     * Description:  Sets the date of a dateType object
     * Pre-conditions:  dateType object has been initialized
     * Post-conditions:  new year, month and day has been set for dateType 
     * object
     * Method input:  year(int), month(int), day(int)
     * Method output:  None
     */
    void setDate(int y, int m, int d);

    /* Method:  getYear
     * Descritpion:  Returns the year of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object year has been returned
     * Method input:  None
     * Method output:  Year of the dateType (int)
     */
    int getYear() const;

    /* Method:  getMonth
     * Descritpion:  Returns the month of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object month has been returned
     * Method input:  None
     * Method output:  Month of the dateType object (int)
     */
    int getMonth() const;

    /* Method:  getDay
     * Description:  Returns the day of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object month has been returned
     * Method input:  None
     * Method output:  Day of the dateType object (int)
     */
    int getDay() const;

    /* Method:  getDate
     * Description:  Returns the year, month, and day of the dateType object
     * Pre-conditions:  dateType object exists
     * Post-conditions:  dateType object year, month and day has been returned
     * Method input:  None
     * Method output:  year (int), month(int), and day(int) of the dateType
     * object
     */
    dateType getDate() const;
    bool operator==(const dateType& otherDate) const;
    bool operator!=(const dateType& otherDate) const;
    bool operator<(const dateType& otherDate) const;
    bool operator>(const dateType& otherDate) const;
    bool operator<=(const dateType& otherDate) const;
    bool operator>=(const dateType& otherDate) const;
    ostream& operator<<(ostream& out);
    istream& operator>>(istream& in);

  private:
    int year;
    int month;
    int day;
};
#endif

你的cpp文件

#include "dateType.h"

void dateType::setDate(int y, int m, int d)
{
  year = y;
  month = m;
  day = d;
}

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

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

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

dateType dateType::getDate() const
{
 return dateType(year, month, day);
}

bool dateType::operator==(const dateType& otherDate) const
{
  return (getYear() == otherDate.getYear() && getMonth() == otherDate.getMonth()
      && getDay() == otherDate.getDay());
}

bool dateType::operator!=(const dateType& otherDate) const
{
  return !(*this == otherDate);
}

bool dateType::operator<(const dateType& otherDate) const
{
  return (getYear() < otherDate.getYear() || getMonth() < otherDate.getMonth() 
      || getDay() < otherDate.getDay());
}

bool dateType::operator>(const dateType& otherDate) const
{
  return (getYear() > otherDate.getYear() || getMonth() > otherDate.getMonth()
      || getDay() > otherDate.getDay());
}

bool dateType::operator<=(const dateType& otherDate) const
{
  return *this == otherDate || *this < otherDate;
}

bool dateType::operator>=(const dateType& otherDate) const
{
  return *this == otherDate || *this > otherDate;
}

ostream& dateType::operator<<(ostream& out)
{
  out << this->getYear() << "-" << this->getMonth() << "-" << this->getDay();
  return out;
}

istream& dateType::operator>>(istream& in)
{
  //Variables
  int year,
      month,
      day;
  char dummy;

  in >> year >> dummy >> month >> dummy >> day;
  this->setDate(year, month, day);
  return in;
} 

dateType::dateType()
{
  year = 0000;
  month = 00;
  day = 00;
}

dateType::dateType(int y, int m, int d)
{
  setDate(y, m, d);
}

复制并过去并享受乐趣

它有很多错误和拼写错误... :)

相关问题