【发布时间】:2014-04-07 04:41:45
【问题描述】:
每次编译时都会收到 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 函数只是为了确保 Alumni 不会成为像 ColMbr 那样的抽象类,它是从它派生的。其次,Alumni 中的所有参数都在 Alumni 或 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;
}
【问题讨论】:
-
你有这些函数的声明,但你还没有实现它们。你需要这样做。
-
谢谢,帮了大忙。所以,我复制了 Alumni::addClass 并在函数外部声明了它。这摆脱了LNK2001。但是,您不能将构造函数从类定义中拉出来。你必须从构造函数中实现什么来绕过错误?
-
你的ctor的实现在哪里?
-
我不太确定您所说的实施是什么意思。 ColMbr 类和构造函数只是作为 Student 和 Alumni 的基类。 ColMbr 是一个抽象类,它简单地将其变量和函数传递给派生类。添加括号后 VS 说我的 const int 需要初始化,这对我来说没有任何意义,因为它是通过 cpp 文件中的构造函数的输入来初始化的。
-
我的意思是,如果我初始化 const,那么它就不能改变,而且我不会有每个对象的单独 ID #s。
标签: c++ visual-c++ constructor