undefined references to 'vtable' and `WinMain' - c++

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.

Related

c++ this is insertion sorting for food and price and calorie but the sorting function doesn't work

#include <iostream>
#include <string>
#ifndef FOOD_H
#define FOOD_H
using namespace std;
this class for insertion sorting for food's price or food's calorie
class Food
{
private:
string Name;
float Calorie;
float Price;
public:
void setName(string n);
void setCalorie(float c);
void setPrice(float p);
string getName();
float getCalorie();
float getPrice();
Food();
Food(string n,float c,float p);
void Print(Food FoodArray[],int);
void Sort(Food FoodArray[], float);
void Sort2(Food FoodArray[], float);
};
#endif
#include <iostream>
#include <string>
#include "Food.h"
#ifndef IMPLEMENTER_CPP
#define IMPLEMENTER_CPP
using namespace std;
void Food::setName(string n){Name=n;}
void Food::setPrice(float p){Price=p;}
void Food::setCalorie(float c){Calorie=c;}
string Food::getName(){return Name;}
float Food::getPrice(){ return Price;}
float Food::getCalorie(){ return Calorie;}
Food::Food(string n,float c,float p)
{
Food::Name=n;
Food::Calorie=c;
Food::Price=p;
}
Food::Food()
{
Food::Name="";
Food::Calorie=0.00;
Food::Price=0.00;
}
void Food::Print(Food FoodArray[],int num)
{
for (int i=0;i>num;i++)
{
cout<<"-------------------------------------------------"<<endl;
cout<<"NAME: "<<FoodArray[i].getName()<<" CALORIE: "<<FoodArray[i].getCalorie()<<" PRICE: "<<FoodArray[i].getPrice()<<endl;
cout<<"-------------------------------------------------"<<endl;
}
}
void Food::Sort(Food FoodArray[], float pri)
{
int i,j;
float tmp;
string t;
for(i=1;i<=pri-1;i++)
{
tmp=FoodArray[i].getPrice();
t=FoodArray[i].getName();
j=i-1;
while((tmp<FoodArray[j].getPrice())&&(j>=0))
{
FoodArray[j+1]=FoodArray[j];
j--;
}
FoodArray[j+1].setName(t);
FoodArray[j+1].setPrice(tmp);
}
cout<<endl<<"Sorted list is shown below: "<<endl;
}
void Food::Sort2(Food FoodArray[], float cal)
{
int i, j;
float tmp;
string t;
for(i=1;i<=cal-1;i++)
{
tmp=FoodArray[i].getCalorie();
t=FoodArray[i].getName();
j=i-1;
while((tmp<FoodArray[j].getCalorie())&&(j>=0))
{
FoodArray[j+1]=FoodArray[j];
j=j-1;
}
FoodArray[j+1].setCalorie(tmp);
FoodArray[j+1].setName(t);
}
cout<<endl<<"Sorted list is shown below: "<<endl;
}
#endif
#include <iostream>
#include <string>
#include "Food.h"
#include "Implementer.cpp"
using namespace std;
contains the main method. From this class an array of food will be created and the sorting function
will be called to sort the array.
class client
{
public:
Food *foodArray[];
void info()
{int num;
string name,answer;
float calorie,price;
loop2:
cout<<"how many do you want ? ";
cin>>num;
if (num>0)
{
for (int i=0; i<num; i++)
{
cout<<i+1<<"- ";
cout<<"Enter Food name: ";
cin>>name;
cout<<"Enter Calories: ";
cin>>calorie;
cout<<"Enter Price: ";
cin>>price;
Food food=Food(name,calorie,price);
*foodArray[i]=food;
}
foodArray[num]->Print(*foodArray,num);
loop:
cout<<"Would you like to sort the foods based on Price or Calories? P/C"<<endl;
cin>>answer;
if (answer=="P")
{
foodArray[num]->Sort(*foodArray,price);
foodArray[num]->Print(*foodArray,num);
}
else if (answer=="C")
{
foodArray[num]->Sort2(*foodArray,calorie);
foodArray[num]->Print(*foodArray,num);
}
else
{
cout<<"please choose P or C."<<endl;
goto loop;
}
}
else
{
goto loop2;
}
}
};
int main()
{
client C;
C.info();
return 0;
}

Array of pointers to an object in C++

I am trying to write a super basic program which creates an array of objects under class Receipt. The class includes an int price, string good (name), and a simple function that adds an item to the list. I am stuck because every time I compile it seg faults before it even gets to the add function, meaning something is wrong with my default constructor.
I am still really new to C++ and pointers are probably my biggest struggle. I have looked online and at my lecture notes trying to figure out what I am doing wrong. I feel like it's something small but I cannot figure it out.
Here is my program:
#include <iostream>
#include <string>
using namespace std;
class Receipt {
private:
int price;
string good;
Receipt* goods[500]; //partially filled array
public:
Receipt();
void add(string name, int cost);
string getName();
int getPrice();
void setName(string name_in);
void setPrice(int price_in);
void displayList();
};
Receipt::Receipt()
{
for (int i=0; i < 500; i++)
{
goods[i]->setName("Empty");
goods[i]->setPrice(-1);
}
}
void Receipt::add(string name, int cost)
{
int place=0;
for (int i=0; i <500; i++)
{
if (goods[i]->getName()=="Empty" && goods[i]->getPrice()==-1)
{
place = i;
break;
}
}
goods[place]->setName(name);
goods[place]->setPrice(cost);
}
int Receipt::getPrice()
{
return price;
}
string Receipt::getName()
{
return good;
}
void Receipt::setName(string name_in)
{
good = name_in;
}
void Receipt::setPrice(int price_in)
{
price = price_in;
}
void Receipt::displayList()
{
//just displaying first item in list for debugging purposes
cout << goods[0]->getName() << endl << goods[0]->getPrice();
}
int main()
{
Receipt mine; //seg faults here
mine.add("banana", 50);
mine.displayList();
return 0;
}
your design is wrong, you have array of Receipt inside Receipt so when you initialize the object, it create 500 where each of them create another 500 endlessly. I think you want to create something like this instead
#include <iostream>
#include <string>
using namespace std;
class Receipt {
private:
int price;
string good;
public:
void setName(string name_in);
void setPrice(int price_in);
string getName();
int getPrice();
};
class Receipts {
private:
Receipt* goods[500]; //partially filled array
public:
Receipts();
void add(string name, int cost);
void displayList();
};
Receipts::Receipts()
{
for (int i = 0; i < 500; i++)
{
goods[i] = new Receipt();
goods[i]->setName("Empty");
goods[i]->setPrice(-1);
}
}
void Receipts::add(string name, int cost)
{
int place = 0;
for (int i = 0; i <500; i++)
{
if (goods[i]->getName() == "Empty" && goods[i]->getPrice() == -1)
{
place = i;
break;
}
}
goods[place]->setName(name);
goods[place]->setPrice(cost);
}
int Receipt::getPrice()
{
return price;
}
string Receipt::getName()
{
return good;
}
void Receipt::setName(string name_in)
{
good = name_in;
}
void Receipt::setPrice(int price_in)
{
price = price_in;
}
void Receipts::displayList()
{
//just displaying first item in list for debugging purposes
cout << goods[0]->getName() << endl << goods[0]->getPrice();
}
int main()
{
Receipts mine; //seg faults here
mine.add("banana", 50);
mine.displayList();
return 0;
}

Program exit without input and it has base,drived class properties initialization issues

It has two issues
1). program is not taking input it exits without getting value of variable option.
2).Also my base and derived class are not initializing they are displaying garbage value.
Here is complete code.
#include<iostream>
using namespace std;
class BeverageItem
{
protected:
string name;
double price;
public:
void set_name(string n);
string get_name();
void set_price(double pr);
double get_price();
};
void BeverageItem::set_name(string n)
{
name=n;
}
string BeverageItem::get_name()
{
return(name);
}
void BeverageItem::set_price(double pr)
{
price=pr;
}
double BeverageItem::get_price()
{
return(price);
}
class HotBeverage:public BeverageItem
{
private:
int tea_bags;
int whiteners;
public:
//HotBeverage(int bg,int wht);
void set_teabags(int t_bags);
int get_teabags();
int getwhiteners();
void set_whiteners(int wht);
double basePrice();
double computeTax();
double totalCost();
void print();
};
double HotBeverage::computeTax()
{
return (0.16*price);
}
double HotBeverage::totalCost()
{
return(price+computeTax());
}
double HotBeverage::basePrice()
{
double pr;
if((tea_bags==1)&& (whiteners==1))
{
pr=20;
}
else if((tea_bags>1)&& (whiteners>1))
{
tea_bags=tea_bags-1;
pr=20+(5*tea_bags);
}
set_price(pr);
return(pr);
}
void HotBeverage::set_teabags(int t_bags)
{
tea_bags=t_bags;
}
int HotBeverage::get_teabags()
{
return(tea_bags);
}
int HotBeverage::HotBeverage::getwhiteners()
{
return(whiteners);
}
void HotBeverage::set_whiteners(int wht)
{
whiteners=wht;
}
void HotBeverage::print()
{
cout<<"Name: "<<name<<endl;
cout<<"Tax:"<<computeTax()<<endl;
cout<<"Total Cost: "<<totalCost()<<endl;
}
class ColdBeverage:public BeverageItem
{
private:
int drinkSize;
public:
//ColdBeverage(int drinkSize);
void setDrinkSsize(int sz);
int getDrinkSize();
double basePrice();
double computeTax();
double totalCost();
void print();
};
void ColdBeverage::print()
{
cout<<"Name: "<<name<<endl;
cout<<"Tax:"<<computeTax()<<endl;
cout<<"Total Cost: "<<totalCost()<<endl;
}
void ColdBeverage::setDrinkSsize(int sz)
{
drinkSize=sz;
}
int ColdBeverage::getDrinkSize()
{
return(drinkSize);
}
double ColdBeverage::computeTax()
{
return (0.16*price);
}
double ColdBeverage::totalCost()
{
return(price+computeTax());
}
double ColdBeverage::basePrice()
{
double pr;
double regularPr=30;
switch(drinkSize)
{
case 1: //regular,
pr=regularPr;
break;
case 2: //large.
price=1.5*regularPr;
break;
case 3: //extra large.
price=2*regularPr;
break;
}
set_price(pr);
return(pr);
}
int main()
{
string name;
int option;
cout<<"Enter The Beverage Name=";
cin>>name;
cout<<"1. For Hot Beverage\n\n";
cout<<"2. For Cold Beverage\n\n";
cout<<"Select Your Choice(1,2)=";
cin>>option;
BeverageItem bi;
bi.set_name(name);
**//some other code here.**
return 0;
}
1) I don't really see a problem with the compiler i tried with
http://cpp.sh/4p5mw
2) POD's in member variables are not default initialised.
https://stackoverflow.com/a/15212447/4669663
Here was the issue
cin>>name;
cin stops when whitespaces are entered in a string so I would have to use
getline(cin,name);
Second issue was because i was not calling the basePrice() before print()
thanks to everyone

Passing pointer to array of objects into function

When I run this code it crashes with out any errors. I have tried everything i know and done searches but i can figure this one out. It builds and runs fine and goes all the way to the cout in FleetCapacity and something about that line is making the code crash . When i commented out that line the code ran fine so im not sure as to why that line of code is causing my program to crash and burn.
#include <iostream>
#include <string>
using namespace std;
class Ship{
private:
string shipName;
string shipYear;
public:
Ship(string sN,string sY){shipName=sN; shipYear=sY;}
virtual void printInfo();
void setShipName(string);
virtual string getShipName();
void setShipYear(string);
string getShipYear();
};
class CruiseShip:public Ship{
private:
int maxPass;
int maxCrew;
public:
CruiseShip(string sN,string sY, int mP,int mC):Ship(sN,sY){setShipName(sN);maxPass=mP; maxCrew=mC; }
void printInfo();
void setMaxPass(int);
int getMaxPass();
void setMaxCrew(int);
int getMaxCrew();
};
class CargoShip:public Ship{
private:
int cargoCap;
public:
CargoShip(string sN,string sY,int cC):Ship(sN,sY){setShipName(sN);cargoCap=cC;}
void printInfo();
void setCargoCap(int);
int getCargoCap();
};
void Ship::setShipName(string sN){shipName=sN;}
string Ship::getShipName(){return shipName;}
void Ship::setShipYear(string sN){shipYear=sN;}
string Ship::getShipYear(){return shipYear;}
void Ship::printInfo(){
cout<<"The ships name is "<<shipName<<endl;
cout<<"The ships year is "<<shipYear<<endl;
}
void CruiseShip::printInfo(){
cout<<"The ships name is "<<getShipName()<<endl;
cout<<"The ships maximum passangers is "<<maxPass<<endl;
}
void CruiseShip::setMaxPass(int mP){maxPass=mP;}
int CruiseShip::getMaxPass(){return maxPass;}
void CruiseShip::setMaxCrew(int mC){maxCrew=mC;}
int CruiseShip::getMaxCrew(){return maxCrew;}
void CargoShip::printInfo(){
cout<<"The ships name is "<<getShipName()<<endl;
cout<<"The ships cargo capacity is "<<cargoCap<<endl;
}
int CargoShip::getCargoCap(){return cargoCap;}
void CargoShip::setCargoCap(int cC){cargoCap=cC;}
void fleetCapacity(Ship** s ,int e){
cout << "Name of ship: " << s[e]->getShipName() << endl;
}
int main()
{
const int NUMSHIPS = 3;
//int aSize = NUMSHIPS;
// array of ship pointers initialized with addresses of dynamically allocated class objects.
Ship *ships[NUMSHIPS] = {
new Ship("The Dinghy Berry", "1982"),
new CruiseShip("Disney Adventure Tours"," ",500,100),
new CargoShip("The Sea Trucker"," ", 50)
};
for (int i = 0; i < NUMSHIPS; i++ )
{
ships[i]->printInfo();
}
cout << "The entire fleet capacity is: ";
fleetCapacity(ships, NUMSHIPS);
cout << " tons." << endl;
return 0;
}
you are calling fleetCapacity(ships, NUMSHIPS); which is then accessing s[e] (ships[NUMSHIPS]) in the function. The valid indices are 0 through NUMSHIPS-1.

3 (related I believe) errors in my c++ program

I am receiving the following errors with my code:
Error 3 error LNK1120: 1 unresolved
externals c:\users\toking\documents\visual studio
2012\Projects\11.9\Debug\11.9.exe 11.9
Error 2 error LNK2001: unresolved external symbol "public: double
__thiscall Package::calculateCost(float,double)" (?calculateCost#Package##QAENMN#Z) c:\Users\toking\documents\visual
studio 2012\Projects\11.9\11.9\11.9_main.obj 11.9
Error 1 error LNK2019: unresolved external symbol "public: double
__thiscall Package::calculateCost(float,double)" (?calculateCost#Package##QAENMN#Z) referenced in function "public:
double __thiscall
overnightPackage::calcCostOvernight(float,double,double)"
(?calcCostOvernight#overnightPackage##QAENMNN#Z) c:\Users\toking\documents\visual
studio 2012\Projects\11.9\11.9\11.9.obj 11.9
This is my second semester of C++ and my first time using inheritance. I tried calling the functions similarly to how my book calls them, but all of the errors seem to be related to my derived classes functions. Any help is greatly appreciated.
// Main
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int main()
{
int i;
string customerName,customerAddress,city,state,senderAddress,recipientAddress;
float packageWeight;
string customerCity;
double costPerOunce;
double flatFee;
double additionalCost;
string customerState;
int customerZipcode;
Package base;
twoDayPackage twoday;
overnightPackage overnight;
cout<<" *****Welcome To The American Package Delievery Services*****"<<endl<<endl;
cout<<"Please Fill In The Requested Data Follow: "<<endl<<"-----------------------------------------"<<endl<<endl;;
cout<<"Enter Customer Name "<<endl<<endl;
cin>>customerName;
cout<<endl;
base.setName(customerName);
cout<<"Enter Customer Address"<<endl<<endl;
cin>>customerAddress;
cout<<endl;
base.setAddress(customerAddress);
cout<<"Enter Customer City"<<endl<<endl;
cin>>customerCity;
cout<<endl;
base.setCity(customerCity);
cout<<"Enter Customer State"<<endl<<endl;
cin>>customerState;
cout<<endl;
base.setState(customerState);
cout<<"Enter Customer ZIP code"<<endl<<endl;
cin>>customerZipcode;
cout<<endl;
base.setZip(customerZipcode);
cout<<"Enter Weight"<<endl;
cin>>packageWeight;
cout<<endl;
cout<<"Enter Cost Per Ounce"<<endl;
cin>>costPerOunce;
cout<<endl;
cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl;
cout<<" 1- Calculate Base Cost "<<endl<<endl;
cout<<" 2- Calculate Two Day Cost "<<endl<<endl;
cout<<" 3- Calculate Over Night Cost"<<endl<<endl;
cin>>i;
cout<<endl;
switch (i)
{
case 1 :
base.calculateCost(packageWeight,costPerOunce);
break;
case 2 :
cout<<"Enter Flat Cost"<<endl<<endl;
cin >> flatFee;
twoday.calcShippingCost(packageWeight,costPerOunce,flatFee);
break;
case 3 :
cout<<"Enter The Additional Cost"<<endl<<endl;
cin >> additionalCost;
overnight.calcCostOvernight(packageWeight,costPerOunce,additionalCost);
break;
default:
cout << "INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;
}
cout<<"Enter sender address "<<endl<<endl;
cin>>senderAddress;
cout<<endl;
base.setSender( senderAddress);
cout<<"Enter ricipent address"<<endl<<endl;
cin>>recipientAddress;
cout<<endl;
base.setRecipient(recipientAddress);
cout<<"address from:"<< senderAddress<<endl;
cout<<"To:"<<recipientAddress<<endl;
system ("pause");
return 0;
}
// Header
#include <iostream>
#include <string>
using namespace std;
class Package // Base Class
{
private:
string name, city, state, sender, recipient;
int zip;
string address;
float weight;
double cost;
public:
void setName(string);
string getName();
void setCity(string);
string getCity();
void setState(string);
string getState();
void setZip(int);
int getZip();
void setAddress(string);
string getAddress();
void setSender(string);
string getSender();
void setRecipient(string);
string getRecipient();
double calculateCost(float , double);
};
class twoDayPackage: public Package
{
public:
double calcShippingCost(float, double, double);
private:
double flatFee;
};
class overnightPackage: public Package
{
public:
double calcCostOvernight(float, double, double);
private:
double overnightCost;
};
// Cpp
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
void Package::setName(string n)
{
name = n;
}
void Package::setCity(string c)
{
city = c;
}
void Package::setState(string s)
{
state = s;
}
void Package::setZip (int zp)
{
zip = zp;
}
void Package::setAddress(string adr)
{
address = adr;
}
void Package::setSender(string sen)
{
sender = sen;
}
void Package::setRecipient(string rec)
{
recipient = rec;
}
string Package::getName()
{
return name;
}
string Package::getCity()
{
return city;
}
string Package::getState()
{
return state;
}
int Package::getZip()
{
return zip;
}
string Package::getAddress()
{
return address;
}
string Package::getSender()
{
return sender;
}
string Package::getRecipient()
{
return recipient;
}
double calculateCost(float weight,double costPerOunce)
{
double z;
z = weight * costPerOunce;
cout<< "The Base Cost = " <<z << endl<<endl;
return z;
}
double twoDayPackage::calcShippingCost(float weight, double costPerOunce, double flatFee)
{
double z;
z = calculateCost(weight,costPerOunce) + flatFee;
cout << "The TwoDayPackage Cost = " << z << endl;
return z;
}
double overnightPackage::calcCostOvernight(float weight,double costPerOunce,double additionalCost )
{
double z;
z = calculateCost(weight, costPerOunce)+(additionalCost * weight);
cout<< "The OvernightPackage Cost = " <<z << endl;
return z;
}
Your code can't decide whether calculateCost is a member function of Package or not. Pick one way and stick to it.