Question

I'm working on own double linked list and everytime after adding object of class DoubleList in Evidence.cpp I get a LNK2019 error: Unresolved external symbol. I will be glad for every advice. Here are my codes:

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};
};

EDIT: Error message here:

  • Error 5 error LNK2019: unresolved external symbol "public: class Student & __thiscall SemestralWork::DoubleList::AccessActual(void)" (?AccessActual@?$DoubleList@VStudent@@@SemestralWork@@QAEAAVStudent@@XZ) referenced in function "public: class Student & __thiscall StudentRecord::SearchStudent(class std::basic_string,class std::allocator > const &)" (?SearchStudent@StudentRecord@@QAEAAVStudent@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

(one of five LNK errors)

Was it helpful?

Solution

So, you have a message. I'll break the message in several lines. The message should be read like this:

LNK2019: unresolved external symbol
"some function or variable that compiler saw nicely declared but the linker can't find the definition of"
(name of the missing symbol as linker sees it)
referenced in function
"some function where the missing function/variable is being used"
(name of the function as linker sees it)

In your case linker needs function DoubleList::AccessActual(void) in namespace SemestralWork. The function is declared somewhere, most likely in DoubleList.h. You probably need to add DoubleList.cpp file to the project. Maybe you forgot to define the function? In that case you have to write it.

Also, you really, really need to remove using namespace lines from header files. Really! Classes that exist in namespaces MUST be declared like this:

namespace SomeNamespace {

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

}

And SHOULD be defined like this in source file:

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

In header files all stuff from any other namespaces, including std namespace, SHOULD be used with full name (std::string). In source files you MAY use using namespace std AFTER all #include directives. Some people disagree on this last one, but it's a matter of style.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top