Needing some help with two errors (C++) - c++

Ive been up for a while coding and it is probably an easy mistake that im just overlooking from lack of sleep, but the problem happens with this segment of code.
do
{
aWithIntAcct.enterAccountData();
aWithIntAcct.getSavInfo();
aWithIntAcct.getCheckingInfo();
checkAcct.push_back(aWithIntAcct);
cout << "Would you like to enter another Checking Account with interest? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
it says that I have ambiguous access of 'enterAccountData' (Error C2385)
This is contradictory to the other (Error C3861) identifier not found.
My Classes are inherited so im not sure why this inst working for me. Any suggestions?
REST OF CODE:
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
template <class T>
void repeatValue(T val, int times)
{
for(int x = 0; x < times; x++)
{
cout << "-";
}
};
template <class T>
void produceReport(int tableRows, string title, T acctType)
{
cout << tableRows << endl;
cout << title << endl;
cout << acctType;
};
class BankAccount
{
friend ostream& operator<<(ostream&, const BankAccount&);
friend istream& operator>>(istream&, BankAccount&);
private:
int acctNum;
double acctBal;
public:
BankAccount(int = 0,double = 0.0);
double operator+=(BankAccount);
int operator+(BankAccount);
friend bool operator>(BankAccount &acctBalOne, BankAccount &acctBalTwo);
friend bool operator<(BankAccount &acctBalOne, BankAccount &acctBalTwo);
int operator==(const BankAccount acctNumOne);
void enterAccountData();
void displayAccount();
void setAcctNum(int);
void setAcctBal(double);
int getAcctNum();
double getAcctBal();
};
BankAccount::BankAccount(int num, double bal)
{
acctNum = num;
acctBal = bal;
}
double BankAccount::operator+=(BankAccount bankAcct)
{
acctBal += acctBal + bankAcct.acctBal;
return acctBal;
}
int BankAccount::operator+(BankAccount newAcctNum)
{
const int INCREMENT = 1;
newAcctNum.acctNum = acctNum + INCREMENT;
return newAcctNum.acctNum;
}
bool operator>(BankAccount &acctBalOne, BankAccount &acctBalTwo)
{
return acctBalOne.acctBal > acctBalTwo.acctBal;
}
bool operator<(BankAccount &acctBalOne, BankAccount &acctBalTwo)
{
return acctBalOne.acctBal < acctBalTwo.acctBal;
}
int BankAccount::operator== (const BankAccount acctNumOne)
{
int truth = 0;
if(acctNum == acctNumOne.acctNum)
truth = 1;
return truth;
}
ostream& operator<<(ostream& display, const BankAccount& aAcct)
{
display << "Account #" << aAcct.acctNum << endl;
display << "Account Balance $" << aAcct.acctBal << endl;
return display;
}
istream& operator>>(istream& dataIn, BankAccount& aAcct)
{
cout << endl << "Enter in an Account # ";
dataIn >> aAcct.acctNum;
cout << "Enter in an Account Balance $ ";
dataIn >> aAcct.acctBal;
return dataIn;
}
void BankAccount::enterAccountData()
{
cout << "Enter the Account:#";
cin >> acctNum;
cout << endl << "Enter the Account Balance:$";
cin >> acctBal;
}
void BankAccount::displayAccount()
{
cout << "The Account number is #" << acctNum << endl;
cout << "The Account Balance is $" << acctBal << endl;
}
void BankAccount::setAcctNum(int num)
{
acctNum = num;
}
void BankAccount::setAcctBal(double bal)
{
acctBal = bal;
}
int BankAccount::getAcctNum()
{
return acctNum;
}
double BankAccount::getAcctBal()
{
return acctBal;
}
class SavingsAccount : public BankAccount
{
friend ostream& operator<<(ostream&, const SavingsAccount&);
private:
double interest;
public:
SavingsAccount(double = 0.0);
void displaySavAccount();
void getSavInfo();
};
SavingsAccount::SavingsAccount(double intRate)
{
interest = intRate;
}
void SavingsAccount::getSavInfo()
{
cout << "Enter Interest Rate: ";
cin >> interest;
}
ostream& operator<<(ostream& display, SavingsAccount& aSavAcct)
{
aSavAcct.displaySavAccount();
return display;
}
void SavingsAccount::displaySavAccount()
{
cout << " Savings information. " << endl;
cout << "Interest Rate on the account is:" << interest << endl;
}
class CheckingAccount : public BankAccount
{
friend ostream& operator<<(ostream& display, const CheckingAccount& aCheckAcct);
private:
double monthlyFee;
int checksAllowed;
public:
CheckingAccount(double = 0,int = 0,int = 0,double = 0);
void displayCheckAccount();
void getCheckingInfo();
};
CheckingAccount::CheckingAccount(double fee, int allowed, int num, double bal) : BankAccount(num,bal), monthlyFee(fee), checksAllowed(allowed)
{
}
void CheckingAccount::getCheckingInfo()
{
cout << "Enter monthly fee of the Account for the checking account. $";
cin >> monthlyFee;
cout << endl << "How many checks are allowed?" << endl;
cin >> checksAllowed;
}
ostream& operator<<(ostream& display, CheckingAccount& aCheckAcct)
{
aCheckAcct.displayCheckAccount();
return display;
}
void CheckingAccount::displayCheckAccount()
{
cout << " Checking Information " << endl;
BankAccount::displayAccount();
cout << "Monthly fee on the account is:$" << monthlyFee << endl;
cout << "Checks allowed for this account is: " << checksAllowed << endl;
}
class CheckingWithInterest : public SavingsAccount, public CheckingAccount
{
private:
public:
CheckingWithInterest();
void displayWithInterest();
};
CheckingWithInterest::CheckingWithInterest() : CheckingAccount(0,0,9999,0), SavingsAccount(0.03)
{}
void CheckingWithInterest::displayWithInterest()
{
CheckingAccount::displayCheckAccount();
SavingsAccount::displaySavAccount();
}
int main()
{
cout << fixed << setprecision(2);
unsigned count;
vector<SavingsAccount>savAcct;
SavingsAccount aSavAcct;
vector<CheckingAccount>checkAcct;
CheckingAccount aCheckAcct;
vector<CheckingWithInterest>withIntAcct;
CheckingWithInterest aWithIntAcct;
const char QUIT = 'n';
char quitChar = 'y';
cout << "Do you want to enter Savings Information? y or n ";
cin >> quitChar;
do
{
aSavAcct.enterAccountData();
aSavAcct.getSavInfo();
savAcct.push_back(aSavAcct);
cout << "Would you like to enter another Savings Account? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
cout << "Do you want to enter Checking Information? y or n ";
cin >> quitChar;
do
{
aCheckAcct.enterAccountData();
aCheckAcct.getCheckingInfo();
checkAcct.push_back(aCheckAcct);
cout << "Would you like to enter another Checking Account? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
cout << "Do you want to enter Checking with interest account Information? y or n ";
cin >> quitChar;
do
{
aWithIntAcct.enterAccountData(); // error points here for both (Line 233)
aWithIntAcct.getSavInfo();
aWithIntAcct.getCheckingInfo();
checkAcct.push_back(aWithIntAcct);
cout << "Would you like to enter another Checking Account with interest? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
sort(savAcct.begin(), savAcct.end());
for(count = 0; count < savAcct.size(); ++count)
{
repeatValue(savAcct.at(count), count);
cout << endl;
produceReport(savAcct.size(), "Savings Account Information", savAcct.at(count));
}
sort(checkAcct.begin(), checkAcct.end());
for(count = 0; count < checkAcct.size(); ++count)
{
repeatValue(checkAcct.at(count), count);
cout << endl;
produceReport(checkAcct.size(), "Checking Account Information", checkAcct.at(count));
}
sort(withIntAcct.begin(), withIntAcct.end());
for(count = 0; count < withIntAcct.size(); ++count)
{
repeatValue(withIntAcct.at(count), count);
cout << endl;
produceReport(withIntAcct.size(), "Checking with interest Account Information", withIntAcct.at(count));
}
system("pause");
return 0;
}

class BankAccount
class SavingsAccount : public BankAccount
class CheckingAccount : public BankAccount
class CheckingWithInterest : public SavingsAccount, public CheckingAccount
This inheritance hierarchy is (probably) incorrect. Right now it looks like this:
BankAccount BankAccount
| |
SavingsAccount CheckingAccount
\ /
CheckingWithInterest
So, each CheckingWithInterest object actually has two BankAccount base class subobjects: one from its SavingsAccount base class subobject and one from its CheckingAccount base class subobject.
So, when you try to call a BankAccount member function on a CheckingWithInterest object, e.g.,
CheckingWithInterest account;
account.enterAccountData();
the compiler doesn't know whether you mean the enterAccountData() from the BankAccount that is part of the SavingsAccount base class or you mean the enterAccountData() from the BankAccount that is part of the CheckingAccount base class.
I'm not sure what, exactly, your requirements are, but if you only want to have a single BankAccount base class subobject, you probably need to use virtual inheritance when you derive CheckingAccount and SavingsAccount from BankAccount, so that the BankAccount subobject is shared between them when they are composed in CheckingWithInterest:
class BankAccount
class SavingsAccount : public virtual BankAccount
class CheckingAccount : public virtual BankAccount
class CheckingWithInterest : public SavingsAccount, public CheckingAccount
If you really do want there to be two BankAccount base class subobjects, then when you call enterAccountData(), you need to qualify on which base class subobject you want to call the member function:
account.CheckingAccount::enterAccountData();

Related

My c++ code gives the following error (error: no matching function for call to ‘Bank::Bank()’) [duplicate]

This question already has answers here:
Default constructor error no matching function for call to
(2 answers)
C++ array of a self-defined class, no matching function call
(3 answers)
no matching function to call for "constructor"
(1 answer)
Closed last year.
I'm a beginner in c++. Though the code is still incomplete, I would like to know why I'm not able to create an array to store my objects from the class. I have to store 5 bank accounts in an array and I was trying to do so by soring the objects but it keeps showing error.
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class Bank
{
string depositor;
int accno;
char type;
float balance;
public:
Bank(string depositor, int accno, char type, float balance); //to assign initial values
void deposit(); //to deposit amount
float withdraw(); //to withdraw amount
void show(); //to show name and balance
Bank(string depositor, int accno); //constructor function for name and account no.
Bank(float balance, int accno); //constructor function for balance and account no.
Bank(char type, int accno); //constructor function for type and account no.
Bank(const Bank&); //copy constructor
//getter and setter functions for all data members
void setname(string depositor);
void setacc(int accno);
void settype(char type);
void setbal(float balance);
string getname();
int getacc();
char gettype();
float getbal();
};
Bank::Bank(string depos, int acno, char typ, float bal)
{
depositor=depos;
accno = acno;
type = typ;
balance = bal ? bal : 0;
}
void Bank::deposit()
{
float damt1;
cout << "Enter deposit amount: ";
cin >> damt1;
if (damt1 < 0.0) {
cout << "Can't deposit negative amount." << endl;
damt1 = 0.0;
}
balance += damt1;
}
float Bank::withdraw()
{
int amount;
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount < 0.0) {
cout << "Negative amount can't be withdrawn" << endl;
amount = 0;
}
if (amount > balance - 1000.0) {
cout << "Not enough balance.";
}
balance -= amount;
return amount;
}
Bank::Bank(string name, int no)
{
depositor = name;
accno = no;
}
Bank::Bank(float bal, int no)
{
balance = bal;
accno = no;
}
Bank::Bank(char ty, int no)
{
type = ty;
accno = no;
}
Bank::Bank(const Bank& p)
{
balance = p.balance;
accno = p.accno;
}
void Bank::setname(string name)
{
depositor = name;
}
void Bank::setacc(int n)
{
accno = n;
}
void Bank::settype(char ty)
{
type = ty;
}
void Bank::setbal(float bal)
{
balance = bal?bal:0;
}
string Bank::getname()
{
return depositor;
}
int Bank::getacc()
{
return accno;
}
char Bank::gettype()
{
return type;
}
float Bank::getbal()
{
return balance;
}
void Bank::show()
{
cout << "Name: " << depositor<<endl;
cout << "Account number: " << accno<<endl;
cout << "Type: " << type<<endl;
cout << "Balance: " << balance<<endl;
}
int main()
{
Bank acct[5];//This is the line with error.I am unable to complete the code bcoz of this
int acno,i;
char ty;
string name;
float bal;
for (i=0;i<5;i++){
cout << "Enter details: \n";
cout << "name: ";
cin >> name;
cout << "\nEnter accno: ";
cin >> acno;
cout << "\nEnter type: ";
cin >> ty;
cout << "\nEnter balance: ";
cin >> bal;
Bank b1(name, acno, ty, bal);
}
return 0;
}
Can someone help me with what corrections I should make?

How to print object name in class member function c++?

Can I print object name in calss member function c++?
For example, I create an account1 object of the Account class in main, and I want to print the balance(member variable of Account class) like this.
class Account{
private:
int balance;
public:
void setBalance(){
//"Enter" << object_name << "'s balance : ";
cin >> balance;
}
void showBalance(){
//object_name << "'s balance : ";
cout << balance << endl;
}
};
int main()
{
Account account1;
account1.setBalance();
account1.showBalance();
}
Enter account1's balance : 50
account1's balance : 50
How Can I do?
class Account {
public:
int accountNumber;
float accountBalance;
Account() {
accountNumber = 0;
accountBalance = 0.0;
}
};
int main() {
Account user; //make a Account object
user.accountNumber = 14538; //assign
user.accountBalance = 100.55; //You can also use cin......for user user input
cout << "Amigo has $" << user.accountBalance << ". balance valaa...!\n";
return 0;
}

How to allow user input in objects and classes?

Consider the following code:
#include <iostream>
using namespace std;
class inventory
{
public:
~inventory()
{
cout << "This Object is being destroyed" << endl;
}
inventory()
{
itemNumber = 0;
quantity= 0;
cost= 0;
}
inventory(int itemNumber1, int quantity1, double cost1)
{
setItemNumber(itemNumber1);
setQuantity(quantity1);
setCost(cost1);
}
void setItemNumber(int itemNumber2)
{
itemNumber=itemNumber2;
}
bool setQuantity(int quantity2)
{
bool userTrue = true;
bool userFalse = false;
if (quantity2 < 0)
{
quantity = 0;
return userFalse;
}
else
{
quantity= quantity2;
return userTrue;
}
}
bool setCost(double cost2)
{
bool userTrue = true;
bool userFalse = false;
if (cost2 < 0.0)
{
cost = 0.0;
return userFalse;
}
else
{
cost= cost2;
return userTrue;
}
}
double getTotalCost(int quantity, double cost)
{
int total;
total = (quantity * cost);
return total;
}
private:
int itemNumber;
int quantity;
double cost;
};
int main()
{
int itemNumberInput;
int quantityInput;
double costInput;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput;
cout << "Enter the Cost : " << endl;
cin >> costInput;
inventory *pointerA, *pointerB;
pointerA = new inventory;
pointerB = new inventory(inventory(itemNumberInput , quantityInput , costInput));
inventory firstObject(itemNumberInput,quantityInput,costInput);
int itemNumberInput1;
int quantityInput1;
double costInput1;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput1;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput1;
cout << "Enter the Cost : " << endl;
cin >> costInput1;
inventory secondObject(itemNumberInput1,quantityInput1,costInput1); // not sure if thats correct
cout << secondObject.setItemNumber(); // not working
cout << secondObject.setQuantity(); // not working
cout << secondObject.setCost(); // not working
return 0;
}
The code above is supposed to take three user inputs, and send them to the classes, and the classes will do their job.
I'm currently stuck at the end where its giving me an error.
In the second object where the values are asked from the user, it should send these values to the classes.
Instead, I'm getting the error.
How can I resolve this problem?
Here is the fixed code:-
#include <iostream>
using namespace std;
class inventory
{
public:
~inventory()
{
cout << "This Object is being destroyed" << endl;
}
inventory()
{
itemNumber = 0;
quantity= 0;
cost= 0;
}
inventory(int itemNumber, int quantity, double cost)
{
this->itemNumber = itemNumber;
this->quantity = quantity;
this->cost = cost;
}
void setItemNumber(int itemNumber)
{
this->itemNumber=itemNumber;
}
bool setQuantity(int quantity)
{
bool userTrue = true;
bool userFalse = false;
if (quantity < 0)
{
this->quantity = 0;
return userFalse;
}
else
{
this->quantity= quantity;
return userTrue;
}
}
bool setCost(double cost)
{
bool userTrue = true;
bool userFalse = false;
if (cost < 0.0)
{
this->cost = 0.0;
return userFalse;
}
else
{
this->cost= cost;
return userTrue;
}
}
double getTotalCost(int quantity, double cost)
{
return quantity * cost;
}
private:
int itemNumber;
int quantity;
double cost;
};
int main()
{
int itemNumberInput;
int quantityInput;
double costInput;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput;
cout << "Enter the Cost : " << endl;
cin >> costInput;
inventory *pointerA, *pointerB;
pointerA = new inventory;
pointerB = new inventory(inventory(itemNumberInput , quantityInput , costInput));
inventory firstObject(itemNumberInput,quantityInput,costInput);
int itemNumberInput1;
int quantityInput1;
double costInput1;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput1;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput1;
cout << "Enter the Cost : " << endl;
cin >> costInput1;
// The below line is correct
// inventory secondObject(itemNumberInput1,quantityInput1,costInput1);
//Alternatively
inventory secondObject;
secondObject.setItemNumber(itemNumberInput1);
secondObject.setQuantity(quantityInput1);
secondObject.setCost(costInput1);
delete pointerA; // delete dynamically allocated memory to avoid memory leak
delete pointerB;
return 0;
}
Well you've constructed 'secondObject' object using the 3-arg constructor, using the user-entered values as parameters. Therefore, the member variables of this object are being set via the constructor and using the 'set' methods aren't really necessary. In your case, the set methods would be useful if you wanted to change the values later on. For example, lets pretend the user enters 10, 10, and 2.5 for the values. You're then using the constructor to construct the object with those values. The only difference is you're placing those values into variables first. But it works the same way. If you wanted to change the value of quantity later on, you could do secondObject.setQuantity(2); And the quantity for that object is now set to 2. The reason why your calls to .set aren't working is because you need to pass in parameters to these methods i.e. the value you want to set it to.
In regard to the destructor method being printed, objects are destroyed when they go out of scope so that the memory is released. Normally, nothing would happen in terms of output- the object would just go out of scope and the compiler would free up the memory and go about its' business. However, you've coded a custom destructor that prints out 'The Object is being destroyed', which it is at the end of the main. It's likely your constructor is working fine, I'm just not sure what you expect to be happening. I'd also suggest you read up on memory leaks in C++, especially in regard to the 'new' keyword.

Access a pointer member of an allocated object on the heap that points to another object

I'm not really sure if the title is correct but i have the following piece of code:
#include <iostream>
#include <string>
using namespace std;
class department
{
private:
string name;
int budget;
public:
int NoT; //Number of teachers
int NoS; //Number of students
void setName(string nam);
void setBudget(int budg);
int getBudget ();
string getName();
};
class school
{
private:
int YoE;
string name;
public:
department *dpt;
int NoDpts;
school();
};
void department::setName(string nam)
{
name=nam;
}
void department::setBudget(int budg)
{
budget=budg;
}
int department::getBudget()
{
return budget;
}
string department::getName()
{
return name;
}
school::school()
{
YoE=2000; // Year of estabilishment
name="Somename";
NoDpts=37;
}
int main()
{
string name;
int budget;
int budg;
school *sch;
sch=new school;
if (!sch)
throw("Out of memory");
sch->dpt=new department[37];
if (!sch->dpt)
throw("Out of memory");
for (int i=0;i<sch->NoDpts;i++)
{
cout << "Insert name for department#" << i+1 << ": ";
cin >> name;
sch->dpt->setName(name);
cout << "Insert budget for department " << name << ": ";
cin >> budget;
sch->dpt->setBudget(budget);
cin.ignore();
}
cout << "Insert a budget for school: ";
cin >> budget;
for (int i=0;i<sch->NoDpts;i++)
{
if (budg=sch->dpt->getBudget()>=budget)
{
cout << "Name of department is: " << sch->dpt->getName() << endl;
cout << "Budget of department amounts to: " << sch->dpt->getBudget() << endl;
cout << endl;
}
}
return 0;
}
As you can see, i allocated 37 departments, however i don't know how to access them.I mean that in the following line:
sch->dpt->setName(name);
I merely access the name of the first department (indirectly by using an accessor function),thus overwriting it and getting different results than expected.This happens to other members too such as
sch->dpt->setBudget(budget);
So i am simply asking how i can access the members of the rest of the departments.

Outputting Vector of Type Class

There is more code to this question in this previous question: C++ Trouble Inputting Data into Private Vector (invalid use)
I'm trying to output a vector of type "Account"
Account:
class Account
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;
private:
int depositAmount;
int withdrawAmount;
public:
static Account createAccount( int, float, string, string, string ); //creates new account
void deposit( int ); //deposits money into account
void withdraw(int); //withdrawals money from account
int retdeposit() const; //function to return balance amount
friend class BankingSystem;
}; //end of class Account
This is the way I'm declaring the vector:
std::vector<Account> accounts_;
And here's how I'm trying to print it to the screen:
for(int i=0; i < accounts_.size(); i++)
{ cout<< accounts_[i] <<endl; }
But I'm getting this error "invalid operands to binary expression".
Current code;
class BankingSystem
{
int accountID;
char fileName;
private:
std::vector<Account> accounts_;
public:
void addAccount();
void storeAccount( Account );
void deleteAccount();
void accountInquiry();
void saveAccounts();
void loadAccountsFromFile();
friend class Account;
friend std::ostream& operator << (std::ostream&, const Account&);
}; // end of class BankingSystem
#endif
std::ostream& operator << (std::ostream& os, const Account& acc)
{
// output members to os
return os;
}
void BankingSystem::addAccount()
{
int ID;
float balance;
std::string pass, first, last;
cout << "\n\t Enter the Account ID: ";
cin >> ID;
cout << "\n\t Enter the passcode: ";
cin >> pass;
cout << "\n\t Enter Client's first name: ";
cin >> first;
cout << "\n\t Enter Client's last name: ";
cin >> last;
cout << "\n\t Enter starting balance: ";
cin >> setw(6) >> balance;
storeAccount( Account::createAccount( ID, balance, pass, first, last ) );
return;
}
//function gets data from createAccount
void BankingSystem::storeAccount( Account newAccountToAdd )
{
//append to vector "accounts_"
accounts_.push_back(newAccountToAdd);
}
void BankingSystem::deleteAccount()
{
cout << "\nEnter The Account ID: ";
cin >> accountID;
}
void BankingSystem::accountInquiry()
{
int n;
cout << "\n\t Enter The Account ID (-1 for all): ";
cin >> n;
//cout << accounts_.size();
if (n == -1)
{
cout << "\n\t List of all Accounts; (" << accounts_.size() << ") TOTAL: ";
for(int i=0; i < accounts_.size(); i++)
{
cout<< accounts_[i] << endl;
}
}
else
{
cout << "\n\t Listing Account: " << n;
cout << "\n\t I should search the vector for the ID you input";
}
}
You need to provide the insertion operator:
std::ostream& operator<<( std::ostream& out, const Account& acct );
Then implement it internally by dumping each one of the fields with the appropriate format.
You should overload operator << for Account class. In class:
friend std::ostream& operator << (std::ostream&, const Account&);
In global (or yours, where Account is defined) namespace
std::ostream& operator << (std::ostream& os, const Account& acc)
{
// output members to os
return os;
}
or create some output function in class for example
void print(std::ostream& os) const { }
And then free-operator <<
std::ostream& operator << (std::ostream& os, const Account& acc)
{
acc.print(os);
return os;
}