Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2020: unresolved token (06000002) in Visual C++

I am creating a new abstract class in C++/CLI and have run into a strange error. There are many questions similar to this one but none of the answers could help me.

In this new class, I get the following error:

error LNK2020: unresolved token (06000002) Foo::execute

This is the h-file:

#pragma once
using namespace System::IO::Ports;
using namespace System;

public ref class Foo
{
protected:
    SerialPort^ port;
public:
    Foo(SerialPort^ sp);
    virtual array<Byte>^ execute();
};

This is the cpp-file:

#include "StdAfx.h"
#include "Foo.h"

Foo::Foo(SerialPort^ sp)
{
    this->port = sp;
}

Note that when I comment out the virtual array<Byte>^ execute(); line, everything compiles perfectly. Also, when I remove the virtual modifier and add an implementation of execute() in the cpp-file, it works as well.

like image 810
Lee White Avatar asked Jan 14 '23 19:01

Lee White


2 Answers

You already gave the answer yourself:

Also, when I remove the virtual modifier and add an implementation of execute() in the cpp-file, it works as well.

You declared the method execute in the header, but it's implementation is missing. That's exactly what the linker error is telling you. In this case the declaration as virtual does not matter.

If you want to create an abstract class, you can find further details in numerous articles online (e.g. Wikibooks: Abstract Classes)

like image 65
Peopleware Avatar answered Jan 16 '23 09:01

Peopleware


You have to either implement the method or remove the declaration from the header. (virtual keyword doesn't matter in this case)

Please, ask a question, if you have any.

like image 33
V-X Avatar answered Jan 16 '23 07:01

V-X