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

时间:2014-04-07 04:41:46

标签: c++ visual-c++ constructor

我每次编译时都会收到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类与Alumni链接时会出现某种错误。 LNK2011说:

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

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

如果有什么我说LNK2019可能是我的const unsigned 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; 
} 

1 个答案:

答案 0 :(得分:0)

所以,这就是我编写代码并正确运行的方法。

我声明了构造函数和函数,这些函数在课外给我提问。之后,我不得不将GPA的GPA功能分配到Student::display(void)功能。最后,我无法弄清楚如何从const unsigned in idNbr构造函数初始化ColMbr类中的ColMbr(const unsigned int idNbr, std::string stdnt_name),所以我从变量中取出了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)
{}