Linking problem

D

DJ.precario

Hi everyone,

I am having a linking problem, and I'd appreciate any help with this.
The code showing the problem:

class base {
public:
virtual void f(int);
};


#include <iostream>
#include "base.h"
using namespace std;
class derived : public base
{
public:
derived() { cout << "Object of the class derived created\n"; };
virtual void f(int);
};


#include <iostream>
#include "derived.h"
using namespace std;
void derived::f(int numb)
{
cout << "number received is: " << numb << endl;
}


#include <iostream>
#include "derived.h"
#include "base.h"
using namespace std;
int main()
{
base *b;
b = new derived;
int numb = 33;
b->f(numb);
delete b;
return 0;
}



I get this linker error:
Linking...
main.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall base::f(int)" (?f@base@@UAEXH@Z)
..\Debug/Tests.exe : fatal error LNK1120: 1 unresolved externals

Can anyone give me a pointer towards what's happening? (I am using
Visual Studio .NET 2003)
 
K

Karl Heinz Buchegger

I get this linker error:
Linking...
main.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall base::f(int)" (?f@base@@UAEXH@Z)
.\Debug/Tests.exe : fatal error LNK1120: 1 unresolved externals

Can anyone give me a pointer towards what's happening? (I am using
Visual Studio .NET 2003)

As the linker tells you:
There is no implementation of the function base::f

That is f() in the class named 'base' !
 
R

Ron Natalie

I get this linker error:
Linking...
main.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall base::f(int)" (?f@base@@UAEXH@Z)
.\Debug/Tests.exe : fatal error LNK1120: 1 unresolved externals
You don't define base::f(int) as the compielr notes. You "use"
base::f(int) in main() even though it eventually resolves to
a derived call. You can either define it or make base::f(int)
pure virtual.
 
D

DJ.precario

My aim was to do base pure virtual, i.e. without an implementation, and
then use the implementation in the derived class, but I'm afraid that I
haven't learnt how to do that yet. Any help?

Thanks
 
V

Victor Bazarov

My aim was to do base pure virtual, i.e. without an implementation, and
then use the implementation in the derived class, but I'm afraid that I
haven't learnt how to do that yet. Any help?

To declare a virtual function pure, you need the "= 0" right before
the semicolon that closes the declaration statement:

class Foo {
virtual void purefunction() = 0;
};

Now you don't have to provide its definition (unless it's used).

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top