首页 文章

添加类[duplicate]的对象后,每次都出现C LNK2019错误

提问于
浏览
1

这个问题在这里已有答案:

我正在处理自己的双链表,每次在 Evidence.cpp 中添加类 DoubleList 的对象后,我得到一个LNK2019错误:未解析的外部符号 . 我会很高兴每一个建议 . 这是我的代码:

StudentRecord.h

#pragma once
#include <string>
#include <iostream>
#include <algorithm>
#include "Student.h"
#include "DoubleList.h"
using namespace std;
using namespace SemestralWork;

class StudentRecord{
public:

    DoubleList<Student> *List;     //declared object (in StudentRecord.cpp is problem with that)

    StudentRecord(void);
    ~StudentRecord(void);
    Student &SearchStudent(const string &ID);
    void addStudent(const Student &student, Student::Position position = Student::Position::LAST);
    Student RemoveStudent(const string &ID);
    void WriteAllStudents(void);
    void Cancel(void);
};

StudentRecord.cpp (just part)

#include "StdAfx.h"
#include "StudentRecord.h"
using namespace SemestralWork;

StudentRecord::StudentRecord(void)
{
    List = new DoubleList<Student>();  // <----  here is first LNK2019 error
}

Student &StudentRecord::SearchStudent(const string &ID){
    Student * SearchedStudent;
    Student * EmptyStudent = NULL;

    //********** down here are next 4 LNK2019 errors. ************

    for(DoubleList<Student>::iterator it = List->begin(); it != List->end(); ++it){
        if(ID == List->AccessActual().getID()){
            SearchedStudent = &List->AccessActual();
            return *SearchedStudent;
        }
    }            // 5 unresolved externals
    return *EmptyStudent;
}
//...

DoubleList (just constructor)

template<typename T>
DoubleList<T>::DoubleList(void){
    NumberOfElements = 0;
    First= NULL;
    Last= NULL;
    Actual= NULL;
}

Student.h

#pragma once
#include <string>
using namespace std;

class Student
{
private:
    string firstName, lastName, ID;
public:
    Student(string, string, string);
    ~Student(void);
    string getFirstName();
    string getLastName();
    string getID();
    enum Position{ FIRST, LAST, ACTUAL, PREVIOUS, NEXT};
};

编辑:错误信息:

  • 错误5错误LNK2019:未解析的外部符号"public: class Student & __thiscall SemestralWork::DoubleList::AccessActual(void)"(?AccessActual @?$ DoubleList @ VStudent @@@ SemestralWork @@ QAEAAVStudent @@ XZ)在函数"public: class Student & __thiscall StudentRecord::SearchStudent(class std::basic_string,class std::allocator > const &)"中引用(?SearchStudent @ StudentRecord @@ QAEAAVStudent @@ ABV?$ basic_string @ DU ?$ @ char_traits @ d @@性病V'$ @分配器@ d @@ 2 STD @@@ Z)

(五个LNK错误之一)

1 回答

  • 2

    所以,你有一条消息 . 我会在几行中打破这个消息 . 该消息应如下所示:

    LNK2019: unresolved external symbol
    “编译器看到的一些函数或变量很好地声明了但是链接器找不到”的定义“
    (链接器看到的缺失符号的名称)
    referenced in function
    “正在使用缺失函数/变量的某些函数”
    (链接器看到的函数名称)

    在您的情况下,链接器需要在命名空间 SemestralWork 中使用函数 DoubleList::AccessActual(void) . 该函数在某处声明,很可能在DoubleList.h中 . 您可能需要将DoubleList.cpp文件添加到项目中 . 也许你忘了定义这个功能?在这种情况下,你必须写它 .

    另外,你真的需要从头文件中删除 using namespace 行 . 真!名称空间中存在的类必须像这样声明:

    namespace SomeNamespace {
    
    class SomeClass
    {
       void SomeFunction();
       ...
    }
    
    }
    

    并且应该在源文件中定义如下:

    void SomeNamespace::SomeClass::SomeFunction()
    {
        ...
    }
    

    在头文件中,来自任何其他命名空间的所有内容(包括 std 命名空间)应该与全名( std::string )一起使用 . 在源文件中,您可以使用 using namespace std AFTER all #include 指令 . 有些人不同意这最后一个,但这是一个风格问题 .

相关问题