什么导致LNK2005?

时间:2015-02-12 15:10:22

标签: c++ c-preprocessor linker-errors preprocessor-directive

如果我将实现留在头文件d_date.h中,则以下程序运行正常但是,我需要通过源d_date.cpp实现它。所有重载都可以正常工作但由于某种原因存在链接器错误。它可能与我的预处理器指令有关,但我似乎无法弄明白。

主要来源

// File: Driver.cpp
// Note: Used to test our date class with overloaded functions

#include <iostream>
#include "d_date.h"

using namespace std;

bool operator ==(date, date);
bool operator !=(date, date);

void main (void)
{
date date1;
date date2;

cout << "Enter date1 and date2:" << endl;


while (cin >> date1 >> date2)
{
    cout << "Printing date1 and date2" << endl;
    cout << date1 << endl << date2 << endl;

    if (date1 == date2) 
        cout << date1 << " is equal to " << date2 << endl;

    if (date1 != date2) 
        cout << date1 << " is not equal to " << date2 << endl;

    if (date1 < date2) 
        cout << date1 << " is less than " << date2 << endl;

    if (date1 > date2) 
        cout << date1 << " is greater than " << date2 << endl;

    ++date1;
    ++date2;


    cout << "Increment of date1: " << date1 << endl;
    cout << "Increment of date2: " << date2 << endl;

    cout << endl << "---End of Run---" << endl << endl;

    cout << "Enter date1 and date2:" << endl; 
}
}

bool operator ==(date lhs, date rhs)
{
return (lhs.getYear() == rhs.getYear() &&
    lhs.getMonth() == rhs.getMonth() &&
    lhs.getDay() == rhs.getDay());
}

bool operator !=(date lhs, date rhs)
{
   return !(lhs == rhs);
}

班级标题

//d_date.h
#ifndef DATE_CLASS
#define DATE_CLASS

#include <iostream>
#include <iomanip>
#include <string>

#include "d_except.h"

using namespace std;

class date
{
public:
  date (int mm=1, int dd=1, int yyyy=1900);
        // supply date in format MM/DD/YYYY
        // preconditions: 1 <= mm <= 12,
        //                1 <= dd <= daysInMonth()

  void writeShortDate () const;
        // output the date in the format "MM/DD/YYYY"
  void writeLongDate () const;
        // output the date in the format "month day, year"

  void incrementDate(int ndays);
        // add ndays days to the date
        // precondition: 0 <= ndays <= 365

  int numberOfDays() const;
        // return the number of days into the year

  int getMonth() const;
        // return the month as integer value 1 to 12
  int getDay() const;
        // return day of the month
  int getYear() const;
        // return the year

  void setMonth(int mm);
        // update the month
        // precondition: 1 <= mm <= 12
  void setDay(int dd);
        // update the day
        // precondition: 1 <= dd <= daysInMonth()
  void setYear(int yyyy);
        // update the year
        // precondition:  if the date is February 29,
        //                yyyy must be a leap year
  int daysInMonth() const;
        // return number of days in the month

  bool isLeapYear() const;
        // is the current year a leap year (true/false)

  bool operator >(date);
  bool operator <(date);

  date operator ++();
  date operator ++(int);

  friend istream& operator >> (istream& , date& );
  friend ostream& operator << (ostream& , date );


private:
  enum monthName {Jan = 1, Feb, Mar, Apr, May, Jun,
                  Jul, Aug, Sep, Oct, Nov, Dec};
        // private type used by date

  int month, day, year;
        // private members that specify the date

};

我没有包含其余的标题,因为它是一堆函数,而且它有点长。

这是重载函数的实现

//implementation of member overload operators
//d_date.cpp
#include "d_date.h"
#include <iostream>
#include <cassert>

using namespace std;

istream& operator >> (istream& istr, date& rhs)
{
char temp;
int m, d, y;

istr >> m >> temp >> d >> temp >> y;

rhs.setMonth(m);
rhs.setDay(d);
rhs.setYear(y);

return istr;
}

ostream& operator << (ostream& ostr, date rhs)
{
ostr << rhs.month << "/" << rhs.day << "/" << rhs.year;
return ostr;
}

date date::operator ++()
{
this->incrementDate(1);
return *this;
}

date date ::operator++(int)
{
date temp = *this;

this->incrementDate(1);

return temp;
}

bool date::operator >(date rhs)
{
if (this->year > rhs.year)
    return true;
else if (this->year == rhs.year)
    return this->numberOfDays() > rhs.numberOfDays();
else
    return false;
}

bool date::operator <(date rhs)
{
if (this->year < rhs.year)
    return true;
else if (this->year == rhs.year)
    return this->numberOfDays() < rhs.numberOfDays();
else
    return false;

}

1 个答案:

答案 0 :(得分:0)

我的猜测是问题是&#34;其中的一系列功能&#34;在头文件中。

如果您将函数定义放在头文件中,它将在包含标题的所有translation units(即源文件)中定义。

基本上有三种方法可以解决这个问题:

  1. 使用linkage-specifier关键字inline使函数内联,例如。

    inline int date::daysInMonth() const {
        ...
    }
    
  2. 在类规范中使函数定义内联,例如

    class date {
        ...
        int daysInMonth() const {
            ...
        }
        ...
    };
    
  3. 从头文件中移动定义,并将它们放在源文件中。

相关问题