I am trying to implement a linked list using a class template, however each of my classes that I want in the list all inherit from an Account class. I have attempted to template the linked list class as account however Ive come into errors I can't resolve. Any ideas as to how I can go about this?
The error exists within the customer.cpp class where it says
14 IntelliSense: a value of type "Account *" cannot be assigned to an entity of type "CurrentAccount *" f:\Further C++\Assignment with Templates\Assignment\Customer.cpp 22 9 Assignment
Customer class:
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include <string>
#include "Account.h"
#include "AccountLinkedList.h"
using namespace std;
class Customer
{
private:
string name;
string telNo;
int doorNum;
string street;
string postcode;
string sex;
string dob;
AccountLinkedList <Account> accountList; //Here is the linkedList templated as Account class
Account * head;
Account * aNode;
public:
int customerID;
Customer * next;
//Customers personal details
Customer(int id, string customerName, string gender, int doorNumber, string customerPostcode, string dateOfBirth)
: customerID(id), name(customerName), sex(gender), doorNum(doorNumber), postcode(customerPostcode), dob(dateOfBirth)
{
};
string getName();
void addAccount(int choice);
void showPersonDetails();
void updatePersonDetails();
};
#endif
Here is the Customer.cpp file:
#include "Customer.h"
#include "Account.h"
#include "CurrentAccount.h"
#include "JuniorCurrentAccount.h"
#include "SavingsAccount.h"
#include "CorporateSavingsAccount.h"
#include "StudentSavingsAccount.h"
#include <string>
using namespace std;
void Customer::addAccount(int choice)
{
int id; // temp account ID
switch(choice)
{
/*Current account + JuniourCurrentAccount both inherit from Account class*/
case 0: CurrentAccount * aNode;
CurrentAccount * head;
/*the two lines below give the error on the = sign*/
aNode = accountList.CreateNode(id/*newaccountID*/);
head = accountList.InsertFirst(head, aNode);
break;
case 1: JuniorCurrentAccount * aNode;
JuniorCurrentAccount * head;
aNode = accountList.CreateNode(id);
head = accountList.InsertFirst(head, aNode);
}
}
string Customer::getName()
{
return name;
}
void Customer::showPersonDetails()
{
cout << name << " details" << endl;
cout << "===============================" << endl;
cout << "sex: " << sex << endl;
cout << "dob: " << dob << endl;
cout << "doorNum: " << doorNum << endl;
cout << "postcode: " << postcode << endl;
cout << "===============================" << endl;
}
Here is the current Account class that inherits from account:
#ifndef CURRENTACCOUNT_H
#define CURRENTACCOUNT_H
#include <iostream>
#include <string>
#include "Account.h"
using namespace std;
class CurrentAccount : Account
{
private:
double intRate;
double balance;
protected:
public:
CurrentAccount * next;
CurrentAccount(int accountNumber, double interestRate, double setBalance) : Account(accountNumber)
{
accountType = "Current Account";
intRate = interestRate;
balance = setBalance;
}
};
#endif
Related
I am working on a project to show statistics for a generic vehicle, then for a car and a truck.
All of the objects have a Manufacturer and Model Year, but the Car has a number of doors, and the truck has a towing capacity.
I have my vehicle class and class constructors all working properly in the code.
Now I want to call the same method of data storage from the base class Vehicle, and use it for the Car Class.
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
private:
string manu;
int year;
public:
// Default constructor
Vehicle() {
manu = "";
year = 0;
}
// Constructor
Vehicle(string autoManu, int autoYear) {
manu = autoManu;
year = autoYear;
}
// Accessors
string getManu() const { return manu; }
int getModel() const { return year; }
void storeInfo(string autoManu, int autoYear);
void displayInfo();
};
This is my Vehicle.h file ^
#include "Vehicle.h"
void Vehicle::storeInfo(string autoManu, int autoYear) {
manu = autoManu;
year = autoYear;
}
void Vehicle::displayInfo() {
cout << "Manufacturer- " << manu << endl;
cout << "Make Year- " << year << endl;
}
This is my Vehicle.cpp file ^
These both work perfectly, now I want to use the same kind of displayInfo for the Car class.
#pragma once
#include <iostream>
#include "Vehicle.h"
#include <string>
using namespace std;
// The Car class represents a car.
class Car : public Vehicle {
private:
int doors;
public:
// Default constructor
Car() : Vehicle() { doors = 0; }
// Constructor #2
Car(string carManu, int carYear, int carDoors) : Vehicle(carManu, carYear) {
doors = carDoors;
}
// Accessor for doors attribute
int getDoors() { return doors; }
void storeInfo(string autoManu, int autoYear, int carDoors);
void displayInfo();
};
This is my Car.h file ^
#include "Car.h"
void Car::storeInfo(string autoManu, int autoYear, int carDoors) {
manu = autoManu;
year = autoYear;
doors = carDoors;
}
void Car::displayInfo() {
cout << "Manufacturer- " << manu << endl;
cout << "Make Year- " << year << endl;
cout << "Doors on the Car" << doors << endl;
}
This is the Car.cpp file.
The issue I am encountering is that the menu and year variables, the variables defined in the base class, are saying "Vehicle::manu is inaccessible"
What would be causing this, and what is the fix?
Thanks!
I have a banking project and I am trying to set up the bank name, address, and working hours. My getlines are showing an error as well as my get functions.
Input exact error messages here please.
'getline': no matching overloaded function found
no suitable user-defined conversion from "Bank" to "std::string" exists
Here's the class for bank:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
class Bank {
public:
void setBankName(string bn) { bn = bankname; }
string getBankName() { return bankname; }
void setBankAdd(string ba) { ba = bankadd; }
string getBankAdd() { return bankadd; }
void setWorkingHours(string bwh) { bwh = bankworkinghours; };
string getWorkingHours() { return bankworkinghours; }
private:
string bankname, bankadd, bankworkinghours;
};
//and then this is in my main function
int main() {
Bank bankname, bankadd, bankworkinghours;
char userChoice; // numbers 1-9
int number=0;
system ("color 5f");
cout << "Name of bank: ";
getline(cin, bankname); **//all the get lines also show error**
cout << endl;
cout << "Bank address: ";
getline(cin, bankadd);
cout << endl;
cout << "Bank working hours: ";
getline(cin, bankworkinghours);
cout << endl;
bankname.setBankName(bankname); //the things in the parentheses show error
bankadd.setBankAdd(bankadd);
bankworkinghours.setWorkingHours(bankworkinghours);
The error is self explanatory. 2nd parameter of getline function is std:string so define bankname as std:string and then set the name of bank object by setBankName.
1- You did not created bank Object in the main to set class attributes.
You need an Object with reference to that object you will set the parameters of the class bank.
2- bankname, bankadd, bankworkinghours are string and you made them Bank
Here is updated code and working fine in VS 2019 without any error. Just a few changes in the first 2 and last three lines of main
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
class Bank {
public:
void setBankName(string bn) { bn = bankname; }
string getBankName() { return bankname; }
void setBankAdd(string ba) { ba = bankadd; }
string getBankAdd() { return bankadd; }
void setWorkingHours(string bwh) { bwh = bankworkinghours; };
string getWorkingHours() { return bankworkinghours; }
private:
string bankname, bankadd, bankworkinghours;
};
//and then this is in my main function
int main() {
Bank bankObj;
string bankname, bankadd, bankworkinghours;
char userChoice; // numbers 1-9
int number = 0;
system("color 5f");
cout << "Name of bank: ";
getline(cin, bankname);
cout << endl;
cout << "Bank address: ";
getline(cin, bankadd);
cout << endl;
cout << "Bank working hours: ";
getline(cin, bankworkinghours);
cout << endl;
bankObj.setBankName(bankname);
bankObj.setBankAdd(bankadd);
bankObj.setWorkingHours(bankworkinghours);
}
void setBankName(string bn) { bn = bankname; } is the wrong way around. try bankname = bn.
I am trying to practice object oriented programming structure in c++. I have written some piece of code. But I got an undefined reference error.
I've tried to build with clion ide but it gave me error. I've tried to compile it on linux terminal using g++ command but it gave me same error.
Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
public:
Account ();
Account(double init_balance);
double getBalance();
void deposit(double amt);
void withdraw(double amt);
private:
double balance;
};
#endif // ACCOUNT_H
Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
Account::Account(double init_balance)
:balance(init_balance)
{
}
double Account::getBalance(){
return balance;
}
void Account::deposit(double amt){
balance += amt;
}
void Account::withdraw(double amt){
balance -= amt;
}
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "Account.h"
#include <iostream>
#include <string>
using namespace std;
class Customer
{
public:
Customer(string first_Name, string last_Name) :
firstName(first_Name), lastName(last_Name)
{
}
//Customer(string first_Name, string last_Name);
string getFirstName();
string getLastName();
Account getAccount();
void setAccount(Account acc);
private:
string firstName;
string lastName;
Account account;
};
#endif // CUSTOMER_H
Customer.cpp
#include "Customer.h"
#include <iostream>
#include <string>
using namespace std;
string Customer::getFirstName(){
return firstName;
}
string Customer::getLastName(){
return lastName;
}
Account Customer::getAccount(){
return account;
}
void Customer::setAccount(Account acc)
{
account = acc;
}
main.cpp
#include "Account.h"
#include "Customer.h"
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
Customer *customer;
Account account(0.0);
// Create an account that can has a 500.00 balance.
cout << endl << "Creating the customer Jane Smith.";
customer = new Customer("Jane", "Smith");
cout << endl << "Creating her account with a 500.00 balance.";
customer->setAccount(Account(500.00));
account = customer->getAccount();
cout << endl << "Withdraw 150.00";
account.withdraw(150.00);
cout << endl << "Deposit 22.50";
account.deposit(22.50);
cout << endl << "Withdraw 47.62";
account.withdraw(47.62);
// Print out the final account balance
cout << endl
<< "Customer ["
<< customer->getLastName()
<< ", "
<< customer->getFirstName()
<< "] has a balance of "
<< account.getBalance()
<< endl;
delete customer;
Account acc1(100), acc2(200);
double suAcc = acc1.getBalance()+ acc2.getBalance();
Account sumAcc(suAcc);
cout << "Balance of sumAcc is " << sumAcc.getBalance() << endl;
return (EXIT_SUCCESS);
}
Here is the error:
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:9: undefined reference to `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:9:(.text+0x2f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15: undefined reference to `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15:(.text+0x12e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15: undefined reference to `Customer::setAccount(Account)
'
It goes on with every method.
I was able to compile this after making several changes.
Remove using namespace std from customer.h. It's bad practice to use "using namespace" here as it could cause name collisions when another file includes customer.h. As a result of removing "using namespace std" you'll need to qualify all of your strings as std::string instead of string.
You declared a default constructor for Account but there is no implementation provided. When a customer is created it tries to call the default constructor of account. If you are on a newer version of c++ you can change this line in your header file.
Account();
to
Account() = default;
If you're on an older version add this to Account.cpp
Account(){}
Edit: I had forgotten to explicitly define my class destructors in their respective .cpp files. I replaced *p with string *killList = new string[10];
and my code now compiles. Thanks for your replies!
I've tried to compile the following files using the command :
g++ -o hunter hunter_h.h hunter_h.cpp animal_h.h animal_h.cpp main.cpp
animal_h.h
#include <iostream>
#include <string>
#ifndef ANIMAL_H
#define ANIMAL_H
using namespace std;
// Animal class
class animal
{
friend class hunter;
// need a name, species, private ID
public:
animal();
animal(string aSpecies);
string name;
string species;
string getSpecies();
void setName(string aName);
string getName();
int getID();
~animal();
private:
static int uID;
};
#endif
animal_h.cpp
#include "animal_h.h"
//#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int animal:: uID = 0 ;
animal::animal()
{
cout << "Created an animal!" << endl;
name = "?";
species = "?";
uID++;
}
animal::animal(string aSpecies)
{
cout << "Created 1 "<< aSpecies << "!" << endl;
name= "?";
species = aSpecies;
uID++;
}
string animal::getSpecies()
{
cout << species << endl;
}
void animal::setName(string aName)
{
name = aName;
cout << "This " << species << " is now called " << name << endl;
}
string animal::getName()
{
cout << name << endl;
}
int animal:: getID()
{
cout << uID << endl;
}
hunter_h.h This is a derived class of the animal base class with unique behaviors.
#include "animal_h.h"
#include <iostream>
#ifndef ANIMAL_HUNTER
#define ANIMAL_HUNTER
class hunter : public animal
{
public:
hunter();
hunter(std::string aSpecies);
void recordKills(std::string kill);
static int tKills;
int totalKills();
static std::string *theKills();
static std::string *p;
static int clearTotal();
~hunter();
};
#endif
hunter_h.cpp
#include "animal_h.h"
#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int hunter:: tKills =0;
string killList[10];
hunter::hunter()
{
cout<<"created a hunter! "<<endl;
name= "? ";
species="? ";
string *p;
p = &killList[0];
}
hunter::hunter(string aSpecies)
{
name = "?";
species = aSpecies;
cout << "created a hunting "<<species <<endl;
}
string *theKills()
{
return hunter::p;
}
void hunter::recordKills(string kill)
{
cout << kill << " killed." << endl;
*(p+tKills) = kill;
tKills++;
cout << tKills << " total kills." << endl;
}
int hunter::totalKills()
{
cout << name << "'s " << "Total kills: " << tKills << endl;
}
int hunter::clearTotal()
{
delete[] killList;
return 0;
}
main.cpp
#include "animal_h.h"
#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
hunter *hunterC;
hunterC= new hunter("cheetah");
hunterC->recordKills("Mouse");
hunterC-> recordKills("Gazelle, Gazelle");
hunterC-> recordKills("Hyena");
hunterC-> recordKills("Rabbit, Rabbit");
hunterC->theKills;
hunterC->clearTotal;
}
Now, when I try to compile I get the following warning and errors:
hunter_h.cpp: In static member function ‘static int hunter::clearTotal()’:
hunter_h.cpp:49:11: warning: deleting array ‘killList’
delete[] killList;
^
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x71): undefined reference to `animal::~animal()'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x101): undefined reference to `animal::~animal()'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x119): undefined reference to `hunter::p'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x15a): undefined reference to `hunter::p'
/tmp/ccqCD1e7.o:main.cpp:(.text+0x1f4): undefined reference to `animal::~animal()'
/tmp/ccqCD1e7.o:main.cpp:(.text+0x20c): undefined reference to `animal::~animal()'
collect2: error: ld returned 1 exit status
I've been learning C++ for a couple months only so am not sure where the above code is going wrong. How can I get this to compile and run?
killList is not allocated using new[], then dont delete it with delete[]. It is not allocated runtime, then it does not have to be deallocated explicitly. It will release its memory when the program exits. With your code as it is you are running the chance of overrunning you killList array.
Try using std::vector instead.
std::vector<std::string> killList;
...
void recordKills(std::string s)
{
...
killList.push_back(s);
}
void clearTotal()
{
killList.clear();
}
When I compile this I get the error title before my name functions in Name.cpp. I've never seen this error before. What's confusing is that there's already a constructor before the three functions in Name.cpp, since that's what the error seems to be talking about.
main.cpp
#include <iostream>
#include <string>
#include "Name.h"
using namespace std;
int main()
{
}
Name.h
#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
using namespace std;
class Name
{
public:
Name();
string getFirst(string newFirst);
string getMiddle(string newMiddle);
string getLast(string newLast);
void setFirst();
void setLast();
void setMiddle();
private:
string First;
string Middle;
string Last;
};
#endif // NAME_H
Name.cpp
#include "Name.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name()
{
}
Name::setFirst(newFirst){
Name = newFirst;
cout << "You entered: " << Name << endl;
}
Name::setMiddle(newMiddle){
Middle = newMiddle;
cout << "You entered: " << Middle << endl;
}
Name::setLast(newLast){
Last = newLast;
cout<< "You entered: " << Last << endl;
}
You cannot omit type names of arguments. Write ones. Also function prototypes in declaration and definition have to be matched.
Your Name.h should be
#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
using namespace std;
class Name
{
public:
Name();
string getFirst();
string getMiddle();
string getLast();
void setFirst(string newFirst);
void setLast(string newLast);
void setMiddle(string newMiddle);
private:
string First;
string Middle;
string Last;
};
#endif // NAME_H
and Name.cpp should be
#include "Name.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name()
{
}
void Name::setFirst(string newFirst){
Name = newFirst;
cout << "You entered: " << Name << endl;
}
void Name::setMiddle(string newMiddle){
Middle = newMiddle;
cout << "You entered: " << Middle << endl;
}
void Name::setLast(string newLast){
Last = newLast;
cout<< "You entered: " << Last << endl;
}