注册 登录
编程论坛 C++教室

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

a8451727 发布于 2007-05-25 15:35, 445 次点击

菜鸟题,直接看下错误信息,帮我看下错在哪了好吗``谢谢了。

#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 时出错.


8 回复
#2
a84517272007-05-25 16:45
帮下芒啦``。
#3
aipb20072007-05-25 17:22
错误信息已经说的很清楚了啊!

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

这样改: Name(){}

#4
a84517272007-05-25 18:29
我还是不明白```。
在哪改呀?我英语不行啊``汗。
#5
独孤风2007-05-25 18:57

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(){};

#6
a84517272007-05-25 19:09
可以啦谢谢两位了。
为什么要这样改啊,刚刚接触类,还蛮费解的
#7
孤魂居士2007-05-25 20:10
  我也是刚刚接触类
#8
aipb20072007-05-25 20:45
以下是引用a8451727在2007-5-25 19:09:35的发言:
可以啦谢谢两位了。
为什么要这样改啊,刚刚接触类,还蛮费解的

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

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

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

#9
a84517272007-05-25 21:20

谢谢各位帮忙,小弟记下了

1