I keep getting the same error in a program that is supposed to just be a simple date class. These are warnings
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation).
This is the code.
// Lab 2: Date.cpp
// Member-function definitions for class Date.
#include <iostream>
using std::cout;
#include "Date.h" // include definition of class Date
Date::Date( int m, int d, int y )
{
setDate( m, d, y ); // sets date
} // end Date constructor
void Date::setDate( int mo, int dy, int yr )
{
setMonth( mo ); // invokes function setMonth
setDay( dy ); // invokes function setDay
setYear( yr ); // invokes function setYear
} // end function setDate
void Date::setDay( int d )
{
if ( month == 2 && leapYear() )
day = ( d <= 29 && d >= 1 ) ? d : 1;
else
day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // end function setDay
void Date::setMonth( int m )
{
month = m <= 12 && m >= 1 ? m : 1; // sets month
} // end function setMonth
void Date::setYear( int y )
{
year = y >= 1900 ? y : 1900; // sets year
} // end function setYear
int Date::getDay()
{
return day;
} // end function getDay
int Date::getMonth()
{
return month;
} // end function getMonth
int Date::getYear()
{
return year;
} // end function getYear
void Date::print()
{
cout << month << '-' << day << '-' << year << '\n'; // outputs date
} // end function print
void Date::nextDay(int d)
{
d=d+1;
}
bool Date::leapYear()
{
if ( getYear() % 400 == 0 || ( getYear() % 4 == 0 && getYear() % 100 != 0 ) )
return true; // is a leap year
else
return false; // is not a leap year
} // end function leapYear
int Date::monthDays()
{
const int days[ 12 ] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return getMonth() == 2 && leapYear() ? 29 : days[ getMonth() - 1 ];
} // end function monthDays
and here is the .h file
// Lab 2: Date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date( int = 1, int = 1, int = 2000 ); // default constructor
void print(); // print function
void setDate( int, int, int ); // set month, day, year
void setMonth( int ); // set month
void setDay( int ); // set day
void setYear( int ); // set year
int getMonth(); // get month
int getDay(); // get day
int getYear(); // get year
void nextDay(int);
private:
int month; // 1-12
int day; // 1-31 (except February(leap year), April, June, Sept, Nov)
int year; // 1900+
bool leapYear(); // leap year
开发者_如何学Python int monthDays(); // days in month
}; // end class Date
#endif
Just out of curiosity, you're not trying to compile a program without a main
function, are you? If so, you're going to get a compilation error.
As an aside, you should be aware that your nextDay
function probably doesn't do what you think it does.
void Date::nextDay(int d)
{
d=d+1;
}
Since you're passing d
in by value, this simply increments a copy of d
and then exits. If you intended to modify the original value, you should change the function signature to void Date::nextDay(int& d)
.
精彩评论