Trouble with Class and Vector.
May 25, 2013 at 4:54pm UTC
Hello everyone:
this is a class project, so I am looking for some guidance. The following program is supposed to print out a bank statement listing all deposits, withdrawls, and balance and compute interest. All is well except I keep getting an error:
error LNK2019: unresolved external symbol "public: __thiscall BankTrans::BankTrans(void)" (??0BankTrans@@QAE@XZ) referenced in function "public: void __thiscall Statement::read(void)" (?read@Statement@@QAEXXZ)
I think my problem is in the line 40: void BankTrans::read() const{}
but I can't figure out what I am doing wrong. I've been at it for 2 days and was able to narrow it down to that line, but I am stuck. Any guidance will be surely appreciated.
Thank you
Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int MONTH_DAY = 30;
class BankTrans {
public :
BankTrans();
BankTrans( int i_day, double i_amount, string i_description);
void read() const ;
void print();
int get_day() const ;
double get_amount() const ;
private :
int input_day;
double input_amount;
string input_description;
};
BankTrans::BankTrans (int i_day, double i_amount, string i_description){
input_day = i_day;
input_amount = i_amount;
input_description = i_description;
}
int BankTrans::get_day() const {
return input_day;
}
double BankTrans::get_amount() const {
return input_amount;
}
void BankTrans::read() const {
}
void BankTrans::print() {
cout << "DAY " << "AMOUNT " << "DESCRIPTION " <<endl;
cout <<input_day<< " " << input_amount <<" " << input_description <<" " <<endl;
}
class Statement{
public :
Statement();
void read();
void print();
void compute_bal();
double get_avg_day_bal();
double get_min_day_bal();
private :
vector <BankTrans> transactions;
vector <double > day_bals;
};
Statement::Statement(){
}
void Statement::read(){
cout << "Use format DAY AMOUNT DESCRIPTION to enter your transaction: " << endl;
bool more = true ;
while (more);
{
BankTrans new_trans;
new_trans.read();
if (cin.fail())
more = false ;
else
transactions.push_back(new_trans);
}
compute_bal();
}
void Statement::compute_bal(){
int day;
int i = 0;
double balance = 0;
for (day = 1; day <= MONTH_DAY; day++) {
while (i < transactions.size() && transactions[i].get_day() == day)
{
balance = balance + transactions[i].get_amount();
i++;
}
day_bals.push_back(balance);
}
}
double Statement::get_avg_day_bal() {
int day;
double sum_of_bals = 0;
for (day = 0; day < day_bals.size(); day++)
sum_of_bals = sum_of_bals + day_bals[day];
return sum_of_bals / MONTH_DAY;
}
double Statement::get_min_day_bal() {
int day;
double min_bal = day_bals[0];
for (day = 1; day < day_bals.size(); day++)
if (day_bals[day] < min_bal)
min_bal = day_bals[day];
return min_bal;
}
void Statement::print() {
int day;
int i = 0;
for (day = 1; day <= MONTH_DAY; day++) {
cout << "DAY: " << day << "BALANCE: " << day_bals[i - 1] << endl;
while (i < transactions.size() && transactions[i].get_day() == day)
{
transactions[i].print();
i++;
}
}
const double INTEREST = 0.005;
cout << "The minimum daily interest balance is: " << get_min_day_bal() * INTEREST << endl;
cout << "The average daily balance with interest is: " << get_avg_day_bal() * INTEREST << endl;
}
int main()
{
system("Pause" );
return 0;
}
May 25, 2013 at 5:02pm UTC
The error diagnostic:
unresolved external symbol "... BankTrans::BankTrans(void)"
tells you what the problem is.
1 2 3 4 5
class BankTrans {
public :
BankTrans(); // *** this has not been defined
// ....
May 25, 2013 at 5:05pm UTC
You didn't implement
BankTrans::BankTrans()
function.
1 2 3 4 5 6
class BankTrans {
public :
BankTrans(){/* initialize variables here, if you need*/ };
// ...
}
should work
May 25, 2013 at 5:24pm UTC
there you got it, as did Null say, you just have to "define" the default constructor for the BankTrans class, just give it any definition and it should work like charm.
you can replace the BankTrans() declaration with this:
1 2 3 4 5 6 7 8 9 10
.
.
.
class BankTrans {
public :
BankTrans() {}
.
.
.
May 25, 2013 at 5:32pm UTC
Thank you,
I am new to C++ and struggling a bit with function and classes. I thought I implemented the BankTrans:BankTrans() function on line 26? Did I not do it correctly?
May 25, 2013 at 5:38pm UTC
These are two different constructors:
1 2 3 4 5 6
class BankTrans {
public :
BankTrans(); // constructor one
BankTrans( int i_day, double i_amount, string i_description); // constructor two
// ...
You had a definition for constructor two; but didn't have one for constructor one
See 'Overloaded functions' in
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on May 25, 2013 at 5:41pm UTC
May 25, 2013 at 6:07pm UTC
Thank you all, this was extremely helpful
Topic archived. No new replies allowed.