我在使用ifndef时遇到问题..我得到并且错误:错误未定义引用

时间:2013-02-19 06:26:55

标签: c++

我正在使用具有继承的类。我的学生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

还有一个主要包括students.h 和underGrads。但除了那些电话,它并不重要。 我包括了学生。 h在实现文件中,它完美地工作 所以我在想我的第二课定义有问题 或以某种方式包括。有人能指出我正确的方向吗?

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

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

  1. 保留包含警卫。
  2. 确保将实现studentstudent.cpp?)的文件链接到您的可执行文件中。