LNK2019 errors in Driver.cpp

Pages: 12
Anyone have any ideas on how to call on the functions in ArrayDictionary without getting abstract errors.
You have a base class. Base class has some pure virtual functions.
You have a derived class that inherits the base class.
Your derived class has to implement each of those virtual functions.

See https://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm

We have not seen the definition of the base class, so we can't tell what your ArrayDictionary does miss.
I'm confused on what you are saying @keskiverto.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Foo {
  virtual int sample() const = 0;
};
// Foo is abstract

class Bar : public Foo {
};

class Gaz : public Foo {
  virtual int sample() const { return 42; };  
};

int main() {
  Foo f; // error, Foo is abstract
  Bar b; // error, Bar is abstract
  Gaz g; // ok
}

 In function 'int main()':
14:7: error: cannot declare variable 'f' to be of abstract type 'Foo'
1:7: note:   because the following virtual functions are pure within 'Foo':
2:15: note: 	virtual int Foo::sample() const
15:7: error: cannot declare variable 'b' to be of abstract type 'Bar'
6:7: note:   because the following virtual functions are pure within 'Bar':
2:15: note: 	virtual int Foo::sample() const
Last edited on
Okay understand what you are saying. But how am I suppose to create a new class that will implement ArrayDictionary or DictionaryInterface.
DictionaryInterface specifies these virtual functions:

1
2
3
4
5
6
7
8
virtual bool isEmpty() const = 0;
virtual int getNumberOfItems() const = 0;
virtual bool add(const KeyType& searchKey, const ValueType& newValue) = 0;
virtual bool remove(const KeyType& searchKey) = 0;
virtual void clear() = 0;
virtual ValueType getValue(const KeyType& searchKey) const throw  NotFoundException) = 0;
virtual bool contains(const KeyType& searchKey) const = 0;
virtual void traverse(void visit(ValueType&)) const = 0;


These are the definitions in ArrayDictionary:

1
2
3
4
5
6
7
8
bool isEmpty() const;
  int getNumberOfEntries() const;
  bool add( const KeyType&, const ValueType& ) throw (PrecondViolatedExcep);
  bool remove( const KeyType& );
  void clear();
  ValueType getValue( const KeyType& ) const throw (NotFoundException);
  bool contains( const KeyType& ) const;
  void traverse( void visit(ValueType&) ) const;


There are differences:
add in the base class doesn't specify throw

When you override a base class function, the specification the derived class must match exactly. It would be useful if you added override to the overriden functions in ArrayDictionary. eg

 
bool isEmpty() const override;


If the function names etc don't match then you'll get a compile error and you'll know which one(s) have the issue.

See https://en.cppreference.com/w/cpp/language/override

Also note that using throw() like was deprecated in C++11 and removed in C++17 - so you have code that doesn't conform to the current standard.
Last edited on
I fixed the issue with the ArrayDictionary and DictionaryInterface not matching. I'm using ArrayDictionary <int,int> dictionary to call on the functions. Now I have errors for Driver.cpp (on first page) for L36, 40, and 34 saying unresolved external symbol (LNK2019).

Basically for when I call on the functions
1
2
3
dictionary.add(key,value);
dictionary.getValue(key);
Last edited on
Well the first error is L34 and assuming the lines match, this is a cout statement - without the std:: There's no indication there's a using namespace std anywhere in driver, so I guess from L34 onwards the cout and cin statements need the preceeding std::
Here is my code now. I have all the cout and cin with std. The error says unresolves external symbol "public virtual bool_thiscall ArrayDictionary::add(int const&ints const &) referenced in function_main (LNK2019). Same error for getValue, ~ArrayDictoary, and ArrayDictoary. Says the error is occuring on line 1 of driver.cpp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include "ArrayDictionary.h"
#include "DictionaryInterface.h"
#include "Entry.h"

int main() {

    // Declare variables
    int userChoice;
    int key;
    int value;
    ArrayDictionary<int, int> dictionary;  // cannot instatiate abstract class
    //DictionaryInterface<int, int> dictionary;  // cannot instatiate abstract class
   

    while (1) {
        std::cout << "1.Insert element into the table" << std::endl;
        std::cout << "2.Search element from the key" << std::endl;
        std::cout << "3.Delete element at a key" << std::endl;
        std::cout << "4.Delete the entire dictionary" << std::endl;
        std::cout << "5.Exit" << std::endl;
        std::cout << "Enter your choice: ";
        std::cin >> userChoice;

    }

    switch (userChoice) {
    case 1:
        std::cout << "Enter element to be inserted: ";
        std::cin >> value;
        std::cout << "Enter key at which element to be inserted: ";
        std::cin >> key;
        dictionary.add(key, value);
        break;

    case 2:
        std::cout << "Enter key of the element to be searched: ";
        std::cin >> key;
        if (dictionary.getValue(key) == -1) {
            std::cout << "No element found at key " << key << std::endl;

        }
        else {
            std::cout << "Element at key " << key << " : ";
            std::cout << dictionary.getValue(key) << std::endl;
        }


    }
}
Last edited on
unresolved external symbol (LNK2019)

Note that error code has "LNK". This is a linker error.

You have compiled "Driver.cpp" into an object file.
"Driver.cpp" includes "ArrayDictionary.h", "DictionaryInterface.h", and "Entry.h".

Compiler should instantiate necessary members for type ArrayDictionary<int,int> when it compiles "Driver.cpp", because it sees all the templates, doesn't it? If it does, then the object code, the symbols, should be in Drivers.obj.

The error shows that this is not true.


Note: The "unresolved external symbol (LNK2019)" is not a complete and exact error message. Why do we have to forcefully extract every last useful piece of information from you?
Okay I have removed the included DictionaryInterface.h and Entry.h from Drive.cpp file.

the error message says unresolved external symbol public virtual bool_thiscall ArrayDictionary::add(int const&ints const &) referenced in function_main

How do I fix this issue. I have finished writing the code. This is the last errors I get before I can run the code successfully. Please can anyone help me fix this last error.
Last edited on
If the error only refers to ::add() and not also to ::getValue(), then there's something about the definition and implementation of add() which doesn't match what the compiler is expecting from its usage.

Note that the definition of DictionaryInterface and its implementation have to be in the same file. Same for ArrayDictionary. You can't have the definition(s) in one .h file and the implementations in another .cpp file as separate compilation units.
Topic archived. No new replies allowed.
Pages: 12