Jaanus
Jaanus

Reputation: 16531

VC++ 2010 error LNK2019: unresolved external symbol

main cpp file:

#include <iostream>
#include <cstdio>

#include "table.h"

using namespace std;

int main() {

    Table test;

    int i;
    for(i = 0; i < 26; i++) {
        cout << test.start[2] << endl;
    }

    system("PAUSE");
    return 0;
}

header file:

#pragma once


class Table {
    public:
        char start[26];

        Table();
        Table(char key[26]);

        ~Table();
};

cpp file:

#include "table.h"


Table::Table() {
    char start[26] = "ABCDEFGHIJKLMNOPRSTUVWXYZ";
}

Table::Table(char key[26]) {

}

errors im getting :

1>playfair.obj : error LNK2019: unresolved external symbol "public: __thiscall Table::~Table(void)" (??1Table@@QAE@XZ) referenced in function _main

1>c:\Users\Jansu\Documents\Visual Studio 2010\Projects\playfair\Debug\playfair.exe : fatal error LNK1120: 1 unresolved externals

so basically i googled a lot and do not know what to do. i found some answers but i tried them and did not help

for example i tried to add additional dependencies, but i had all of them already added.

help me please, why does the error come?

Upvotes: 1

Views: 3570

Answers (5)

Karl Knechtel
Karl Knechtel

Reputation: 61479

unresolved external symbol "public: __thiscall Table::~Table(void)" (??1Table@@QAE@XZ) referenced in function _main

It means exactly what it says.

"unresolved" = "couldn't be found". "external symbol" = "definition of the function". It is looking for the destructor - the important part is "Table::~Table".

Your class definition refers to a destructor, but doesn't implement it. Neither does the implementation file. Because you referred to a destructor, the compiler will not make the usual automatic do-nothing one for you. The destructor is called in main() because you create an instance in main(), and the destructor is needed to clean it up at the end of main().

BTW, your constructor does not actually initialize the data in the table. It creates a local array named start which is then promptly thrown away, leaving the member alone.

You won't be able to just assign from the string to the array member, either. You'll need to use a copying function, such as std::copy:

Table::Table() {
    char* alphabet = "ABCDEFGHIJKLMNOPRSTUVWXYZ";
    std::copy(alphabet, alphabet + 26, start); // std::copy comes from <algorithm>.
}

Upvotes: 2

Cedekasme
Cedekasme

Reputation: 2037

You have to define the destructor in your cpp file :

Table::~Table()
{

}

Upvotes: 6

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385088

You declared the destructor, so you must define it, too.

The hint's in that the linker error refers to the destructor.

  • Simply remove the destructor declaration from your class definition: you don't have anything in it anyway.

And Jerry's right about your array initialisation issue: you in fact do not initialise that array at all, but create some local one in the constructor body that never gets used.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490018

Although the header defines Table as having a dtor, the cpp file only contains a couple of constructors -- not a destructor. Given that there seems to be nothing for your destructor to do (you haven't allocated any dynamic memory or anything like that) you probably just want to remove the declaration of Table::~Table(); and be done with it. While you're at it, you probably want to make Table::start private. I'd also change the parameter to a char const * instead of using array notation:

class Table {
        char start[26];
    public:
        Table();
        Table(char const *key);
};

Once you're done dealing with that, you'll need to deal with the fact that Table::Table() defines a local variable named start, and initializes it, but leaves Table::start uninitialized, which I doubt is what you wanted/intended.

Upvotes: 3

Praetorian
Praetorian

Reputation: 109079

You haven't defined the destructor, only declared it. Try changing the header to this:

#pragma once

class Table {
    public:
        char start[26];

        Table();
        Table(char key[26]);

        ~Table() {}
};

Upvotes: 1

Related Questions