gpt4 book ai didi

c++ - 我在使用 ifndef 时遇到问题。我收到错误 : error undefined reference

转载 作者:行者123 更新时间:2023-11-30 04:20:55 26 4
gpt4 key购买 nike

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

我正在使用 header guards,我也尝试过使用 #pragma once 得到相同的结果。

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

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的main和本科生。 h 但除了那些电话之外,它并不重要。我已经包括了学生。 h 就像在一个实现文件中一样,它工作得很好所以我认为我的二等定义有问题或以某种方式包含。有人可以指出我正确的方向吗?

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

非常感谢任何帮助

最佳答案

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

关于c++ - 我在使用 ifndef 时遇到问题。我收到错误 : error undefined reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14951167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com