Question

UrzadzenieElektroniczne is my abstract Class and class Komputer inherits from it. I still get errors. I tried to fix them but none of the resolutions I found on the internet worked. Is there a problem with the definition and declaration of my virtual function or should I look for an error somewhere else?: Errors:

     1>CKomputer.obj : error LNK2001: unresolved external symbol "public: virtual void__thiscall Komputer::wlaczenieurz(void)" (?wlaczenieurz@Komputer@@UAEXXZ)

UrzadzenieElektroniczne.h

#pragma once

#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

class UrzadzenieElektroniczne
{
public:
int czy_wlaczony;
UrzadzenieElektroniczne();
~UrzadzenieElektroniczne();
virtual void wlaczenieurz() = 0;
private:

protected:
string producent;
};

CKomputer.h

#pragma once

#include <stdlib.h>
#include <iostream>
#include <string>

#include "UrzadzenieElektroniczne.h"
#include "Procesor.h"
#include "KartaDzwiekowa.h"


using namespace std;

class Komputer: public UrzadzenieElektroniczne
{
private:
Procesor procesor;
KartaDzwiekowa *karta_dzwiekowa;

string nazwa_komputera;
int ram;
int ile_kart_dzwiekowych;
public:

void wlaczenieurz();

CKomputer.cpp

#include <iostream>
#include <cstdio>
#include <string>


#include "CKomputer.h"
#include "Procesor.h"
#include "KartaDzwiekowa.h"

void UrzadzenieElektroniczne::wlaczenieurz()
{
if (czy_wlaczony == 0)
    cout<<"Komputer wylaczony"<<endl;
if (czy_wlaczony == 1)
    cout<<"Komputer wlaczony"<<endl;
}
Was it helpful?

Solution 2

Your function should probably be qualified as a member of Komputer:

void Komputer::wlaczenieurz()
//   ^^^^^^^^
{
if (czy_wlaczony == 0)
    cout<<"Komputer wylaczony"<<endl;
if (czy_wlaczony == 1)
    cout<<"Komputer wlaczony"<<endl;
}

OTHER TIPS

It's a typo. :)

void UrzadzenieElektroniczne::wlaczenieurz()

should be:

void Komputer::wlaczenieurz()

You need to define void UrzadzenieElektroniczne::wlaczenieurz() instead of void Komputer::wlaczenieurz(). The tricky part is that C++ actually allows to have definitions of pure virtal functions. Thru they can be accessed by non virtual calls only.
Your code declares 2 wlaczenieurz() one in base class, and it is pure virtual, thus it doesn't require to be defined, and one in derived class that must have definition.
While in your case it is clearly misprint, in general you may have some default implementation of pure virtual function, that can be used in derived class by adding using statement.
For example in your case using UrzadzenieElektroniczne::wlaczenieurz; in Komputer instead of wlaczenieurz declaration should fix error as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top