类的实现与类的头文件分成2个文件产生错误。
这是date类定义的头文件
- C/C++ code
#ifndef HEADER_DATE#define HEADER_DATE#include <iostream>using namespace std;class Date{ int y, m, d;public: Date(int year, int month, int day); void display();};#endif这是date类的实现
- C# code
//#include "Date.h"Date::Date(int year, int month, int day){ y=year; m=month; d=day;}void Date::display(){ cout<<y<<"-" <<m<<"-" <<d<<endl;}下面是date类的应用程序
- C/C++ code
#include "date.h"#include <iostream>using namespace std;int main(){ Date d(2012, 12, 21); d.display(); return 0;}编译后产生的错误是:
Linking...
HelloWorld.obj : error LNK2001: unresolved external symbol "public: void __thiscall Date::display(void)" (?display@Date@@QAEXXZ)
HelloWorld.obj : error LNK2001: unresolved external symbol "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z)
Debug/HelloWorld.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
[解决办法]
你怎么把cpp里面的include 头文件注释掉了,去掉注释就好了。
[解决办法]
date.cpp没放进工程
[解决办法]
感觉是工程建错了
[解决办法]
对啊,你的cpp文件里边,也是需要保护一下这个类的声明文件所在的头文件的吧?