2 个链接错误,1 个来自虚函数,1 个来自派生类

2 Link Errors, 1 from a virtual functions, 1 from a derived class

本文关键字:派生 函数 错误 链接      更新时间:2023-10-16
每次

编译时都会收到LNK 2019和2001错误。 LNK2019指出:

public: __thiscall ColMbr::ColMbr(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0ColMbr@@QAE@IV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Alumni::Alumni(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Alumni@@QAE@IV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHH0@Z

因此,将ColMbr类与校友联系起来存在某种错误。 LNK2011 说:

"public: virtual void __thiscall Alumni::addClass(unsigned int,unsigned int)" (?addClass@Alumni@@UAEXII@Z)

所以我的虚函数调用有问题。 我知道LNK错误意味着需要声明一个我错过的变量,但我看不到它。 首先,Alumni::addClass 函数只是为了确保 Alumni 不会像 ColMbr 那样成为抽象类,它就是从 ColMbr 派生出来的。 其次,校友中的所有论点都是在校友或ColMbr中定义和声明的。

如果这是我想说的任何事情LNK2019可能是我的常量无符号int idNbr的问题。 我不知道LNK2001有什么问题。 也许我需要给函数一个垃圾目的或其他东西。

这是我的头文件,后跟 cpp。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#ifndef _ColMbr
#define _ColMbr
class ColMbr
{
protected:
    const unsigned int idNbr;
    std::string name;
public:
    ColMbr();
    ColMbr(const unsigned int idNbr, std::string stdnt_name);
    std::string setName(std::string stdnt_name);
    virtual void display(void);
    virtual void addClass(unsigned int credits, unsigned int gradePoint) = 0;
};
#endif // !1
std::string ColMbr::setName(std::string stdnt_name)
        {
            name = stdnt_name;
            return name;
        }
class Student : public ColMbr
{
private:
    unsigned int credHrs, qualPts;
    std::string degSought;
    double GPA;
public:
    Student(unsigned int idNbr, std::string name) : ColMbr (idNbr, name)
    {
        credHrs = 0;
        qualPts = 0;
        degSought = "Unspecified";
    }
    Student(const unsigned int idNbr, std::string stdnt_name, unsigned int credHrs1, unsigned int qualPts1, std::string newDegree) : ColMbr(idNbr,stdnt_name)
    {
        credHrs = credHrs1;
        qualPts = qualPts1;
        degSought = newDegree;
    }
    void setDegree(std:: string newDegree);
    double getGPA(unsigned int credHrs, unsigned int qualPts);
    void addClass(unsigned int newClass, unsigned int newQualPts)
    {
        credHrs = credHrs + newClass;
        qualPts = qualPts + newQualPts;
    }
    void display(void)
    {
        std::cout << "This student is active." << std::endl <<"ID #: "
            << idNbr << std::endl << "Name: " << name << std::endl
            << "GPA: " << GPA << std::endl << "Major: " << degSought
            << std::endl;
    }
};
void Student::setDegree(std:: string newDegree)
{
    degSought = newDegree;
}
double Student::getGPA(unsigned int credHrs, unsigned int qualPts)
{
    double credHrs1, qualPts1;
    std::istringstream input(credHrs);
    input >> credHrs1;
    std::istringstream input1(qualPts);
    input >> qualPts1;
    GPA = qualPts1 / credHrs1;
    return GPA;
}
#ifndef _Alumni
#define _Alumni
class Alumni : public ColMbr
{
private:
    std::string degree;
    int month, day, year;
public:
    Alumni(const unsigned int idNbr, std::string stdnt_name, int gradMonth, int gradDay, int gradYear, std::string newDegree) : ColMbr(idNbr, stdnt_name)
    {
        degree = newDegree;
        month = gradMonth;
        day = gradDay;
        year = gradYear;
    }
    void addClass(unsigned int credits, unsigned int gradePoint);
    void display (void)
    {
        std::cout << "This student is an Alumni." << std::endl <<"ID #: "
                << idNbr << std::endl << "Name: " << name << std::endl
                << "Graduation Date: " << month << "/" << day << "/" << year 
                << std::endl << "Major: " << degree << std::endl;
    }
};
#endif

/***

#include <iostream> 
#include <cstdlib> 
#include "ColMbrs.h" 

int main() 
{ 
    ColMbr *cMbr[4]; // array of base-class pointers 
    int i; // index to array 
 // Create some college members 
    cMbr[0] = new Student( 12345, "Steven DiFranco", 15, 33, "AA" ); 
    cMbr[1] = new Alumni( 98765, "Don Green", 12, 15, 1978, "AAS" ); 
    cMbr[2] = new Alumni( 24680, "Henry Thoreau", 5, 22, 1846, "AA" ); 
    cMbr[3] = new Student( 13579, "Millenia Best" ); 
 // display the array 
    cout << "All college members:n"; 
    for( i = 0; i < 4; ++i ) 
    { 
        cMbr[i]->display(); // no need to check type field or cast 
        cout << endl; 
    } 
 // test addClass for student 
    cMbr[3]->addClass( 3, 12 ); 
    cMbr[3]->display(); 
    cout << endl; 
    system("PAUSE"); 
    return 0; 
} 

所以,这就是我如何让我的代码正确编译和运行。

我声明了构造函数和函数,它们在类外给我带来了问题。 之后,我不得不将 GPA 函数中的 GPA 赋值放入Student::display(void)函数中。最后,我不知道如何从 ColMbr(const unsigned int idNbr, std::string stdnt_name) 构造函数初始化 ColMbr 类中的const unsigned in idNbr,所以我从变量中删除了 const 标签。

下面是头文件的代码。 .cpp保持不变。

编辑以反映我如何解决const unsigned int idNbr问题。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#ifndef _ColMbr
#define _ColMbr
class ColMbr
{
protected:
    unsigned const int idNbr;
    std::string name;
public:
    //A default and initial constructor are called.
    ColMbr();
    ColMbr(const unsigned int id, std::string stdnt_name) : idNbr(id)
    {
        name = setName(stdnt_name);
    }
    virtual ~ColMbr();
    std::string setName(std::string stdnt_name);
    //ColMbr is made into an abstract class.  It can not be called except with
    //a pointer.
    virtual void display(void);
    virtual void addClass(unsigned int credits, unsigned int gradePoint) = 0;
};
#endif // !1
ColMbr::~ColMbr(){}
void ColMbr::display(void)
{
}
std::string ColMbr::setName(std::string stdnt_name)
        {
            name = stdnt_name;
            return name;
        }
class Student : public ColMbr
{
private:
    unsigned int credHrs, qualPts;
    std::string degSought;
    double GPA;
public:
    //A constructor with no additional Student attributes is created.
    Student(unsigned int idNbr, std::string name) : ColMbr (idNbr, name)
    {
        credHrs = 0;
        qualPts = 0;
        degSought = "Unspecified";
    }
    //A constructor that adds Student attributes is created.
    Student(const unsigned int idNbr, std::string stdnt_name, unsigned int credHrs1, unsigned int qualPts1, std::string newDegree) : ColMbr(idNbr,stdnt_name)
    {
        credHrs = credHrs1;
        qualPts = qualPts1;
        degSought = newDegree;
    }
    ~Student();
    //Functions for initializing the variables that will be output.
    void setDegree(std:: string newDegree);
    static double getGPA(unsigned int credHrs, unsigned int qualPts);
    //The virtual functions are overwritten.  Addclass allows for a change in GPA.
    void addClass(unsigned int newClass, unsigned int newQualPts)
    {       
        credHrs = credHrs + newClass;
        qualPts = qualPts + newQualPts;
    }
    void display(void)
    {
        GPA = getGPA(credHrs, qualPts);
        std::cout << "This student is active." << std::endl <<"ID #: "
            << idNbr << std::endl << "Name: " << name << std::endl
            << "GPA: " << GPA << std::endl << "Major: " << degSought
            << std::endl;
    }
};
Student::~Student(){}
void Student::setDegree(std:: string newDegree)
{
    degSought = newDegree;
}
//getGPA is declared and new students receive a 0
double Student::getGPA(unsigned int credHrs, unsigned int qualPts)
{
    double GPA;
    double credHrs1, qualPts1;
    if (credHrs == 0)
    {
        GPA = 0;
    }
    else
    {
        credHrs1 = static_cast<double>(credHrs);
        qualPts1 = static_cast<double>(qualPts);
        GPA = qualPts1 / credHrs1;
    }
    return GPA;
}
#ifndef _Alumni
#define _Alumni
class Alumni : public ColMbr
{
private:
    std::string degree;
    int month, day, year;
public:
    //An alumni constructor is defined.
    Alumni(const unsigned int idNbr, std::string stdnt_name, int gradMonth, int gradDay, int gradYear, std::string newDegree) : ColMbr(idNbr, stdnt_name)
    {
        degree = newDegree;
        month = gradMonth;
        day = gradDay;
        year = gradYear;
    }
    ~Alumni();
    //The virtual classes are overwritted.
    //Even though addClass has no function here it is overwritten to ensure Alumni is not an abstract class.
    void addClass(unsigned int credits, unsigned int gradePoint);
    void display (void)
    {
        std::cout << "This student is an Alumni." << std::endl <<"ID #: "
                << idNbr << std::endl << "Name: " << name << std::endl
                << "Graduation Date: " << month << "/" << day << "/" << year 
                << std::endl << "Major: " << degree << std::endl;
    }
};
#endif
Alumni::~Alumni(){}
void Alumni::addClass(unsigned int credits, unsigned int gradePoint)
{}