我很喜欢这个例子。根据教科书,一切都很好。无法编译简单的C++程序,需要澄清
然而,试图编译这些文件时,我得到一个问题(见下文)
3个文件
Date.cpp:
#include "Date.h"
Date::Date()
{
setDate(1,1,1900);
}
Date::Date(int month, int day, int year)
{
setDate(month, day, year);
}
Date.h:
class Date
{
public:
Date();
Date (int month, int day, int year);
void setDate(int month, int day, int year);
private:
int m_month;
int m_day;
int m_year;
};
Main.cpp:
#include "Date.h"
int main()
{
Date d1 ;
return 1;
}
当试图用g++ *
编译,我得到
Undefined symbols for architecture x86_64:
"Date::setDate(int, int, int)", referenced from:
Date::Date() in cc8C1q6q.o
Date::Date() in cc8C1q6q.o
Date::Date(int, int, int) in cc8C1q6q.o
Date::Date(int, int, int) in cc8C1q6q.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
当我宣布Date *d;
代替,程序编译。 当我声明Date *d = new Date
而不是,程序失败。
这是怎么回事?
叹了口气..我这么傻。谢谢 – 2012-08-16 04:12:00
不用担心。这就是你学习的方式 – lol 2012-08-16 04:13:41