1

我正在使用具有继承的类。我的学生。h 自己调用时可以正常工作,但是当我将它用作基类时,会出现以下错误:

/tmp/ccSCMNjf.o:umain.cpp:function main: error: undefined reference to 'student::showStudent()'
/tmp/ccSCMNjf.o:umain.cpp:function main: error: undefined reference to 'student::showStudent()'
/tmp/ccSCMNjf.o:umain.cpp:function main: error: undefined reference to 'student::showStudent()'
/tmp/ccSCMNjf.o:umain.cpp:function main: error: undefined reference to 'student::showStudent()'
/tmp/ccl9pOYI.o:underGradImp.cpp:function underGrad::underGrad(std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::basic_string<char,  std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, double, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, sStat): error: undefined reference to 'student::student(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> , std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, double, double)'
collect2: ld returned 1 exit status

我正在使用标头保护,并且我也尝试过使用 #pragma 一次,结果相同。

如果我取出标头防护装置,我会收到此错误:

In file inlcluded from underGrad.h:160,
                  from umain.cpp:9:
student.h:19:7: error: redefinition of  'class student'
student.h:19:7  previous definition of 'class student'

这是 student.h 文件

#ifndef H_student
#define H_student

#include <iostream>
#include <string>


using namespace std;

class student 
{

public:

  student(string = "", string = "", string = "", string = "", double = 0.0, double = 0.0, double = 0.0);

  string getLastName() const;

  string getFirstName() const;

  string getID() const;

  void getCharges(double&, double&) const;

  double getBalance() const;

  string getMajor() const;

  double getGPA() const;

  void setName(string, string);

  void setID(string);

  void setMajor(string);

  void setCharges(double, double);

  void setGPA(double);

  void showStudent();

private:

  bool checkID(string) const;

  string expandMajorCode(string);

  string lastName, firstName, studentID, major;

  double gpa, charges, financialAid, balance;

  static const double MAX_CHARGES = 10000.0;

  static const double MAX_GPA = 4.0;

};

#endif

这是我的 underGrad.h 文件,它使用 student.h 作为基类


#ifndef H_underGrad
#define H_underGrad

#include <iostream>
#include <string>

#include "student.h"


using namespace std;

enum sStat {PROBATION, GOOD, SPECIAL, NONE};

class underGrad : public student {

public:
  underGrad(string = "", string = "", string = "", string = "", double = 0.0, double =         0.0, double = 0.0, string = "", sStat  = NONE);
  string getAdvisor() const;
  sStat getStatus() const;
  void setAdvisor(string);
  void setStatus(sStat);
  void showStudent();

private:
  string advisor;
  sStat sStatus;

};

#endif

还有一个 main 包括 student.h 和 underGrads 。h 但除了这些电话之外,这并不重要。我已经包括了 student 。h 就像在一个实现文件中一样,它工作得很好,所以我认为我的第二个类定义或者包含以某种方式有问题。有人可以指出我正确的方向吗?

NOTE:
The errors are produced for every member of student - not just showStudent

任何帮助是极大的赞赏

4

1 回答 1

2
  1. 保留包括警卫。
  2. 确保将实现student( student.cpp?) 的文件链接到可执行文件中。
于 2013-02-19T06:28:41.637 回答