0

I always get this errors when I try to compile and run program:

undefined reference to 'vtable for Currency'
undefined reference to 'WinMain'
[Error] ld returned 1 exit status

My program should convert values ​​from one currency to another (from Euro or Dollars to hryvnias). I am using Dev C++, if that matters.

Class Currency is an abstract base class, and classes Euro and Dollar are child classes.

File currency.h:

#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <iostream>
#include <locale.h>

using namespace std;

class Currency
{
protected:
    double amount; //amount of money in some currency
    float rate; //exchange rate
    double amount_grivni; //amount of money in hryvnias
public:
    virtual void Convert() = 0;
    virtual void print();
    virtual void Setamount(double);
    virtual void Setrate(float);
    virtual ~Currency();
};

class Euro: public Currency
{
public:
    Euro();
    Euro(double, float);
    virtual void Convert();
    virtual void print();
    virtual void Setamount(double);
    virtual void Setrate(float);
    ~Euro();
};

class Dollar:public Currency
{
public:
    Dollar();
    Dollar(double, float);
    virtual void Convert();
    virtual void print();
    virtual void Setamount(double);
    virtual void Setrate(float);
    ~Dollar();
};

File currency.cpp:

#include "currency.h"

Currency::~Currency()
{
}

//----------------------------------------------------------------
Dollar::Dollar()
{
    amount=0;
    rate=0;
    amount_grivni=0;
}

Dollar::Dollar(double cur_amount, float cur_rate)
{
    amount=cur_amount;
    rate=cur_rate;
    amount_grivni=0;
}

void Dollar::Convert()
{ 
    amount_grivni=amount*rate;
}

void Dollar::print()
{
    cout<<"Amount of money in dollars = "<<amount;
    cout<<"\nCurrent rate of dollar to hryvnia = "<<rate;
    cout<<"\nAmount of money transferred in hryvnias = "<<amount_grivni<<"\n\n";
}

void Dollar::Setamount(double amount_dm)
{
    amount=amount_dm;   
}   
    
void Dollar::Setrate(float rate_dm)
{
    rate=rate_dm;
}

Dollar::~Dollar()
{
}


//----------------------------------------------------------------
Euro::Euro()
{
    amount=0;
    rate=0;
    amount_grivni=0;
}

Euro::Euro(double cur_amount, float cur_rate)
{
    amount=cur_amount;
    rate=cur_rate;
    amount_grivni=0;
}

void Euro::Convert()
{
    amount_grivni=amount*rate;
}

void Euro::Setamount(double amount_d)
{
    amount=amount_d;    
}   
    
void Euro::Setrate(float rate_d)
{
    rate=rate_d;
}

void Euro::print()
{
    cout<<"Amount of money in euros = "<<amount; 
    cout<<"\nCurrent rate of euro to hryvnia = "<<rate;
    cout<<"\nAmount of money transferred in hryvnias = "<<amount_grivni<<"\n\n"; 
}

Euro::~Euro()
{
}

File currency_main.cpp:

#include "currency.cpp"

int main()
{
    int i, n, choice; 
    double cu_amount; 
    float cu_rate;

    cout<<"Enter the number of currencies to process";
    cin>>n;

    Currency **arr=new Currency*[n];

    for(i=0; i<n; i++)
    {
        cout<<"\nType number 1 for Dollar and 2 for Euro:";
        cin>>choice;

        switch(choice)
        {
            case 1:
            {
                cout<<"\nEnter amount of money in dollars = ";
                cin>>cu_amount;
                cout<<"\nEnter the exchange rate (1 dollar in hryvnia) = ";
                cin>>cu_rate;
                arr[i]=new Dollar(cu_amount, cu_rate);
                break;
            }
            case 2:
            {
                cout<<"\nEnter amount of money in euro = ";
                cin>>cu_amount;
                cout<<"\nEnter the exchange rate (1 euro in hryvnia) = ";
                cin>>cu_rate;
                arr[i]=new Euro(cu_amount, cu_rate);
                break;
            }
        }
    }

    for(i=0; i<n; i++)
    {
        arr[i]->Convert();
        arr[i]->print();
    }

    for (i=0; i<n; i++)
        delete arr[i];

    delete[] arr;

    return 0;
}

I tried to run this program in Visual Studio, but it didn't help. I created new files and copied code from old ones, but it also didn't work. I even reinstalled Dev C++ and rebooted the computer. The code looks simple, but I can't see what the problem is.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Merlin
  • 1
  • 1
    [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Jesper Juhl Nov 29 '22 at 01:04
  • "I even reinstalled Dev C++ and rebooted the computer" - That's unlikely to do *anything*. Why would you think that would resolve the problem? – Jesper Juhl Nov 29 '22 at 01:06
  • 4
    `#include "currency.cpp"` never include a cpp file. – drescherjm Nov 29 '22 at 01:07
  • `Currency **arr=new Currency*[n];` - Ouch, my eyes, soo many stars - *why*? – Jesper Juhl Nov 29 '22 at 01:08
  • 3
    somewhere in the dev++ settings must a switch saying you are compiling a windows ui app. You medd to change that to a console app. (WinMain is the entry point for win gui apps) – pm100 Nov 29 '22 at 01:08
  • 2
    You are attempting to compile as a Windows executable instead of a console application. The entry function name for a windows executable is `WinMain`, for a console application `main`. You need to change your compiler settings to build a console application. – David C. Rankin Nov 29 '22 at 01:08
  • 1
    It could be that the actual problem is some of your cpp files are not part of your project. In many IDEs you need to add files to a project not just put the files in the same folder. With MinGW if `int main()` is not found it looks for `WinMain()` if that is not found it gives up with a linker error. This is in the case that you or your IDE did not force it to be a windows GUI application using the `mwindows` compiler flag. – drescherjm Nov 29 '22 at 01:08
  • @JesperJuhl https://www.youtube.com/watch?v=-1KIMiKTKPw – user4581301 Nov 29 '22 at 01:15

0 Answers0