Unresolved external symbol errors
Apr 10, 2013 at 3:06pm UTC
I'm doing a homework assignment where I have to calculate monthly interest from a starting balance. I've pulled several sections of the code from various parts of the book. So my problem may be that I'm not combining them correctly.
I'm getting the following error:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall SavingsAccount::SavingsAccount(void)" (??0SavingsAccount@@QAE@XZ) referenced in function _main
The ios flags are new to me and that may be part of the problem. As soon as I can get a working program, I'm going to make the starting balance and interest rate user inputs. But I get seem to get past this error.
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
// Savings.cpp
// Chapter 10_Problem 10.7
#include <iostream>
#include <iomanip>
#include "Savings.h"
using namespace std;
double SavingsAccount::annualInterestRate=0; // initialize static data member
void SavingsAccount::calculateMonthlyInterest(void )
{
savingsBalance += savingsBalance * (annualInterestRate/12);
}
void SavingsAccount::modifyInterestRate(double i)
{
annualInterestRate = (i >= 0 && i <= 1) ? i : .03;
}
void SavingsAccount::printBalance(void ) const
{
cout << setiosflags(ios::fixed | ios::showpoint)
<< '$' << setprecision (2) << savingsBalance
<< resetiosflags(ios::fixed | ios::showpoint);
}
void SavingsAccount::StartProgram() // program starts here
{
SavingsAccount saver1(2000), saver2(3000);
SavingsAccount::modifyInterestRate(.03);
cout << "\nOutput monthly balances for one year at 3%"
<< "\nBalances: Saver 1 " ;
saver1.printBalance();
cout << "\tSaver 2 " ;
saver2.printBalance();
for (int month = 1; month <= 12; ++month)
{
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << "\nMonth" << setw(3) << month << ": Saver 1 " ;
saver1.printBalance();
cout << "\tSaver 2 " ;
saver2.printBalance();
}
SavingsAccount::modifyInterestRate(.04);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << "After setting interest rate to 4%"
<< "\nBalance: Saver 1" ;
saver1.printBalance();
cout << "\tSaver 2 " ;
saver2.printBalance();
cout << endl;
// return 0;
}
Apr 10, 2013 at 3:09pm UTC
Can you show the Main function? Or even the whole main.cpp file you have if that is what you have it named as.
Last edited on Apr 10, 2013 at 3:10pm UTC
Apr 10, 2013 at 3:11pm UTC
Main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Savings_main.cpp
// Chapter 10_Problem 10.7
#include <iostream>
#include "Savings.h"
using namespace std;
int main()
{
SavingsAccount SavingsAccount;
SavingsAccount.StartProgram();
return 0;
}
Header
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
// Savings.h
// Chapter 10_Problem 10.7
#include <iostream>
using namespace std;
// #ifndef SAVINGS_H
// #define SAVINGS_H
class SavingsAccount // savingsAccount class
{
public :
SavingsAccount();
void StartProgram();
SavingsAccount(double b) {savingsBalance = b >= 0 ? b : 0;}
void calculateMonthlyInterest(void );
static void modifyInterestRate(double );
void printBalance(void ) const ;
private :
static double annualInterestRate;
double savingsBalance;
};
// #endif
Apr 10, 2013 at 3:13pm UTC
The linker warning is telling you that you haven't defined SavingsAccount::SavingsAccount(void )
. In other words, it is expecting you to have defined a default constructor for SavingsAccount, and you haven't.
Apr 10, 2013 at 3:16pm UTC
I thought line 12 in the header file defined the default constructor.
Apr 10, 2013 at 3:17pm UTC
Just comment out line 12 of Savings.h file, the compiler will generate a default constructor for you.
Also, is this allowed ???? It seems ambigous to me ....
1 2 3 4 5 6 7
int main()
{
SavingsAccount SavingsAccount;
SavingsAccount.StartProgram();
return 0;
}
Apr 10, 2013 at 3:19pm UTC
I thought line 12 in the header file defined the default constructor.
Line 12 declares that the class
has a default constructor. But you haven't actually defined what it does anywhere.
Edit:
Also, is this allowed ???? It seems ambigous to me ....
It's legal to have an object with the same name as its type - but it's not very sensible, as it leads to confusing code.
Last edited on Apr 10, 2013 at 3:20pm UTC
Apr 10, 2013 at 3:19pm UTC
now I get the following"
Error 1 error C2512: 'SavingsAccount' : no appropriate default constructor available
Apr 10, 2013 at 3:21pm UTC
modoran:
That's one of the requirements of the assignment. Divide the code into 3 files. The test file should contain minimal code. That's how I've always done it and it's worked so far.
Apr 10, 2013 at 3:21pm UTC
So the problem is still that you need a default constructor and you haven't defined one.
Apr 10, 2013 at 3:23pm UTC
That's one of the requirements of the assignment. Divide the code into 3 files. The test file should contain minimal code. That's how I've always done it and it's worked so far.
modoran is commenting on the fact that you've used the name SavingsAccount both for a type and for an object:
SavingsAccount SavingsAccount;
Apr 10, 2013 at 3:32pm UTC
Okay. Let me go back to the book.
Apr 10, 2013 at 4:29pm UTC
I think I got it. Thanks for steering me in the right direction.
Topic archived. No new replies allowed.