找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 865|回复: 8

[求助]我错在哪了?关于类的小疑问

[复制链接]
发表于 2009-11-6 01:32:31 | 显示全部楼层 |阅读模式 IP:江苏扬州
菜鸟题,直接看下错误信息,帮我看下错在哪了好吗``谢谢了。
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class Name
{
public:
Name();
Name (string first,string middle,string last);
string getFirstName() const;
string getLastName() const;
string getMiddleName() const;
string getsignature() const;
void print(ostream& out) const;
private:
string myFirstName,
myMiddleName,
myLastName;
};
Name::Name(string first,string middle,string last)
{
myFirstName=first;
myMiddleName= middle;
myLastName= last ;
}
inline string Name::getFirstName() const
{
return myFirstName;
}
inline string Name::getLastName() const
{
return myLastName;
}
inline string Name::getMiddleName() const
{
assert (myMiddleName.size()>0);
return myMiddleName;
}
inline string Name::getsignature() const
{
return getFirstName()+' '
+getMiddleName()+"."
+getLastName();
}
inline void Name::print(ostream& out) const
{
cout <<getFirstName()+' '
+getMiddleName()+' '
+getLastName();
}

class Student:public Name
{
public:
Student();
Student(string first,string middle,string last,int id);
Student(string first,string middle,string last,int id,double wage,double hours);
Name getName() const;
int getIdNumber() const;
double getHoursWage() const;
double getHoursWorked() const;
void print(ostream& out) const;
private:
Name myName;
int myIdNumber;
double myHoursWage,
myHoursWorked;
};
inline Student::Student(string first,string middle,string last,int id)
{
myName=Name(first,middle,last);
myIdNumber=id;
myHoursWage=0.0;
myHoursWorked=0.0;
}
inline Student::Student(string first,string middle,string last,int id,double wage,double hours)
{
myName=Name(first,middle,last);
myIdNumber=id;
myHoursWage=wage;
myHoursWorked=hours;
}
inline Name Student::getName()const
{
return myName;
}
inline int Student::getIdNumber()const
{
return myIdNumber;
}
inline double Student::getHoursWage()const
{
return myHoursWage;
}
inline double Student::getHoursWorked()const
{
return myHoursWorked;
}
inline void Student::print(ostream& out)const
{
myName.print(out);
cout <<' ' <<getIdNumber()
<<' ' <<getHoursWorked()
<<' ' <<getHoursWage();
}

/*******************************/
驱动程序

#include <iostream>
#include "Student.h"
using namespace std;
void main()
{
Student oneStudent("Alex","Bob","Colt",1234,7.25,15.0),
anotherStudent("Debra","Ellen","Fazio",9876);
oneStudent.print(cout);
cout <<"\n";
anotherStudent.getName().print(cout);
cout <<' ' <<anotherStudent.getIdNumber() <<endl;
}
/*******************************/
错误信息:

例题.obj : error LNK2001: unresolved external symbol "public: __thiscall Name::Name(void)" (??0Name··QAEXZ)
Debug/书本211页.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
发表于 2009-11-6 01:32:32 | 显示全部楼层 IP:江苏扬州
帮下芒啦``。
回复

使用道具 举报

发表于 2009-11-6 01:32:33 | 显示全部楼层 IP:江苏扬州
错误信息已经说的很清楚了啊!

你Name类的默认构造函数只声明没定义。

这样改: Name(){}
回复

使用道具 举报

发表于 2009-11-6 01:32:35 | 显示全部楼层 IP:江苏扬州
我还是不明白```。
在哪改呀?我英语不行啊``汗。
回复

使用道具 举报

发表于 2009-11-6 01:32:36 | 显示全部楼层 IP:江苏扬州
class Name
{
public:
Name(); //*******
Name (string first,string middle,string last);
string getFirstName() const;
string getLastName() const;
string getMiddleName() const;
string getsignature() const;
void print(ostream& out) const;
////////////////把上面 Name(); //*******改成 Name(){};
回复

使用道具 举报

发表于 2009-11-6 01:32:37 | 显示全部楼层 IP:江苏扬州
可以啦谢谢两位了。
为什么要这样改啊,刚刚接触类,还蛮费解的
回复

使用道具 举报

发表于 2009-11-6 01:32:39 | 显示全部楼层 IP:江苏扬州
我也是刚刚接触类
回复

使用道具 举报

发表于 2009-11-6 01:32:40 | 显示全部楼层 IP:江苏扬州
以下是引用a8451727在2007-5-25 19:09:35的发言:
可以啦谢谢两位了。
为什么要这样改啊,刚刚接触类,还蛮费解的

道理很简单,在类定义中,任何成员函数的组成都是 声明+定义

比如
class x{
void f();
}
这里void f()仅仅为函数声明,表示一个接口,提供给使用,至于这个函数具体的细节实现,是在定义中。这就是所谓的抽象。
这时候还不能调用,因为我们还没有定义。你就犯了这个错误,所以产生了那个错误。

定义有多种形式,可以在类定义中定义,也可以在类外部。
在类定义中定义的一般是一些简单的函数,编译器会识别它为内联函数。
比如我在这个类内部定义,就可以这样重写这个类,
class x{
void f(){//do something}
}
注释那里可以写你要执行的代码,如果你什么都不需要,那就什么都不写,就比如你那个Name的构造函数。
回复

使用道具 举报

发表于 2009-11-6 01:32:42 | 显示全部楼层 IP:江苏扬州
谢谢各位帮忙,小弟记下了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 17:34 , Processed in 0.201114 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表