Issue with constructor

i thought constructors were using the same name as the class, no idea why I am getting this issue
Error: Function definition for 'account' not found

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
  #include<iostream>
#include<cmath>
 
using namespace std;
const int account = 100;

class account {
	int acctNum;
	double balance;
public:
	account(int x, double b);
	
	void setAcct(int x) {
		acctNum = x;
	}
	int getAcct() {
		return acctNum;
	}
	void setBalance(double b) {
		balance = b;
	}
	double getBalance() {
		return balance;
	}
};
What you have there is just a declaration. account(int x, double b); just says that this function exists somewhere. You need a definition, the body of the function.
can i get an example
1
2
3
4
void setBalance(double b); // Declaration
void setBalance(double b) {
	balance = b;
} // Definition 


The code has no idea with just the declaration what setBalance does.
Last edited on
so your saying my constructor needs a body? because im not talking about methods in my class
Yes that's what I'm saying. I used one of the methods in your class as the example you asked for.
Last edited on
const int account = 100;

class account {};

Do you really think that's a good idea?

As for your constructor, it needs the definition because you just have the prototype.
Ex:
1
2
3
4
5
account::account(int x, double b)
{
    acctNum = x;
    balance = b;
}
Last edited on
your right i left that from a previous program, thanks.
but now I am being told i need a default constructor in class account? so i did that and the error went away, I was curious as to why do i need a default constructor along with constructor that does something else?

You only need a default constructor if you want to make your object without any arguments.
1
2
3
4
5
6
7
8
9
10
class A
{

public:
    A();
};

A::A()
{
}


Now I could create a object of type A with the following:
 
A objectA;


If my class had a different constructor like:
 
A(int x, int y);

Then to create an object of type A, I would have to do the following:
 
A objectA(50, 30);


this is confusing me, i would think
// not to add a default constructor but when i dont i get this error, is my syntax flawed?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class checking : public account {
public:
	double computeIntr(int years) {
		//i need balance, but balance is not accessible
	}
	checking(int acctNum, double balance) {//if i get rid of my default constructor why does it tell me Error: no default constructor for the base class
		acctNum = 0;
		balance = 0;
	}
	string toString() {
		stringstream ss;
		string ans;
		ss << " Account Number :" << acctNum << " Balance: " << balance << endl;
		ans = ss.str();//the above is now a string
		return ans;
	}

};
Last edited on
nvm i figured it out
Topic archived. No new replies allowed.