1

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)

Pivoman
  • 4,435
  • 5
  • 18
  • 30

1 Answers1

2

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.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • DoubleList::AccessActual(void) is declared in DoubleList.h and is defined in DoubleList.cpp (as you said). I delete using namespace in .h and .cpp and now i'm using SemestralWork::DoubleList::AccessActual(void)... You explained it to me really nice, but errors are still here and I'm still confused :-( – Pivoman Dec 24 '13 at 23:07
  • 2
    @Pivoman, I just noticed that `DoubleList` is a class template and not an actual class. Could it be that definitions for all the functions of `DoubleList` are in DoubleList.cpp file? That would be wrong place for them, and in fact file DoubleList.cpp should not exist. All function of class template `DoubleList` should stand in DoubleList.h. – Dialecticus Dec 25 '13 at 00:25
  • Ok, if I define all function in DoubleList.h, errors goes away. DoubleList.cpp I can delete, but I hope that teacher won't have problem with that... Thanks a lot :-) – Pivoman Dec 25 '13 at 10:21
  • @Pivoman, class templates and function templates are not really classes and functions. They are blueprints that are used to create actual classes and functions. Because they are "blueprints" their code must be visible to the compiler, and only way for that to work is to have template's code in header files. – Dialecticus Dec 25 '13 at 10:56