C++ 类编写基础问题

nmglzlong 2008-03-23 02:45:49

Date.h

class Date
{
public:
void SetDate(int m,int d,int y);
void ShowDate();
private:
int month,day,year;
};

cpp1.cpp

#include<iostream>
using namespace std;

void
SetDate(int m,int d,int y)
{
month=m;
day=d;
year=y;
}

void
ShowDate()
{
cout<<month<<"."<<day<<"."<<year<<endl;
}


cpp2.cpp

#include<iostream>
#include"Date.h"
using namespace std;
int main()
{
int m,d,y;
cin>>m>>d>>y;
Date date;
date.SetDate(m,d,y);
date.ShowDate();
return 0;
}

为什么不行啊。。。编译的时候没错误。。。可杀死build的时候就有错误了。。帮忙看一下谢谢了。。。。快郁闷死了。。搞了一天还是没有结果谢谢各位了。

在下QQ 329648628

有没有高手能留下QQ讨论一下啊。。。谢谢了
...全文
81 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hastings 2008-03-23
  • 打赏
  • 举报
回复
13楼看错了.哈.
hastings 2008-03-23
  • 打赏
  • 举报
回复
没有默认构造函数..
nmglzlong 2008-03-23
  • 打赏
  • 举报
回复
明白了。。。是我添加文件的顺序问题。。。
nmglzlong 2008-03-23
  • 打赏
  • 举报
回复
请问楼上的和我写的有什么区别吗?
xiaochen57 2008-03-23
  • 打赏
  • 举报
回复
Date.h

class Date
{
public:
void SetDate(int m,int d,int y);
void ShowDate();
private:
int month,day,year;
};

cpp1.cpp

#include <iostream>
using namespace std;

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

void Date::ShowDate()
{
cout < <month < <"." < <day < <"." < <year < <endl;
}


cpp2.cpp

#include <iostream>
#include"Date.h"
using namespace std;
int main()
{
int m,d,y;
cin>>m>>d>>y;
Date date;
date.SetDate(m,d,y);
date.ShowDate();
return 0;
}
这样就ok啦
nmglzlong 2008-03-23
  • 打赏
  • 举报
回复
编译的时候是没有问题可是build exe文件的时候就有问题
nmglzlong 2008-03-23
  • 打赏
  • 举报
回复
7楼的编译的时候是没有问题可是build exe文件的时候就有问题了。
Supper_Jerry 2008-03-23
  • 打赏
  • 举报
回复
说一下你的错误。我用vc6.0没有问题
nmglzlong 2008-03-23
  • 打赏
  • 举报
回复
--------------------Configuration: cpp1 - Win32 Debug--------------------
Linking...
cpp1.obj : error LNK2001: unresolved external symbol "public: void __thiscall Date::ShowDate(void)" (?ShowDate@Date@@QAEXXZ)
cpp1.obj : error LNK2001: unresolved external symbol "public: void __thiscall Date::SetDate(int,int,int)" (?SetDate@Date@@QAEXHHH@Z)
Debug/cpp1.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

cpp1.exe - 3 error(s), 0 warning(s)


这是vc6.0报的
nmglzlong 2008-03-23
  • 打赏
  • 举报
回复
楼上的还是不行。。
arong1234 2008-03-23
  • 打赏
  • 举报
回复
你遇到啥错误了?
ttkk_2007 2008-03-23
  • 打赏
  • 举报
回复

//修改一下
Date.h

class Date
{
public:
void SetDate(int m,int d,int y);
void ShowDate();
private:
int month,day,year;
};

cpp1.cpp

#include <iostream>
#include "Date.h"
using namespace std;

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

void
Date::ShowDate()
{
cout < <month < <"." < <day < <"." < <year < <endl;
}
xiaofang3100 2008-03-23
  • 打赏
  • 举报
回复
是不是函数实现部分,要加上类的名字,如void Data::SetDate(int m,int d,int y)
ttkk_2007 2008-03-23
  • 打赏
  • 举报
回复

Date.h

class Date
{
public:
void SetDate(int m,int d,int y);
void ShowDate();
private:
int month,day,year;
};

cpp1.cpp

#include <iostream>
using namespace std;

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

void
Date::ShowDate()
{
cout < <month < <"." < <day < <"." < <year < <endl;
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧