Account Class Link errors

My class recently switched from C to C++ and I have been having some trouble with class "permission" for lack of a better work. I keep getting a LNK2019 error relating to the isEmpty and LNK1120 for an unresolved external.

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
 class Account
{
public:
	Account(); //default constructor
	Account(double inBalance,int accountNum ,string newNname ,string date);
	~Account();

	double getBalance() const;
	int getAccount() const;
	string getName() const;
	string getDate() const;

	void setBalance(double balance);
	void setAccount(int account);
	void setName(string name);
	void setDate(string date);

	void isEmpty(Account test);
	

private:
	double balance;
	int accountNumber;
	string name;
	string startDate;

};


Equations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int Account::getBalance() const
{
	return balance;
}

void Account::isEmpty(Account test)
{
	double balance=0;

	balance = test.getBalance();
	
	if(balance <= 0)
	{
		balance = 0;
		cout << "No funds in account" << endl; 
	}
	
}


update.
Last edited on
Why does your isEmpty function take a parameter?

Line 10 of your source file is useless, you already have access to the balance member variable in this function.

The link error is because you call getBalance() but you have not provided any implementation of that function. This would happen in C too.
> class "permission"
i believe the proper term is scope?
Sorry, I copied the getAccount() instead of getBalance(). Isn't that the proper way to use a getter?
You don't need to use a getter inside the same class' member functions, because the member variable you are getting is already in scope.

I would also agree with MiiNiPaa below, but I think you are having confusion about instance variables and the this pointer.
Last edited on
On the other hand, using public interface even inside your own class is a good way to minimise need to fix stuff if you ever change internal implementation.
I even get a LNK error when I am just trying to initialize an Account in main. The only way I can get it to go away is to do the following.

gives error
 
Account test;


No error but I can't get to anything in the account class
 
Account test();
Again, the only two functions you have defined are getBalance and isEmpty - therefore all other functions, including your constructor, are undefined. The same would happen in C.

By the way:
Account test; defines a variable named test that holds an Account object instance, whereas
Account test(); declares a function named test that takes no parameters and returns an Account object by value.
Last edited on
I have two "types" for Account to initialize too in a secondary cpp file

account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Account::Account ()
{
	cout << "created" <<endl;
	balance=0;
	name='\n';
	accountNumber=0;
	startDate="00/00/00";
}

Account::Account (double newinBalance,int newaccountNum ,string newName ,string newdate)
{
	balance=newinBalance;
	accountNumber=newaccountNum;
	name=newName;
	startDate=newdate;
}


Wouldn't the following code allow me to initialize and Account variable in main either way?
You also need to define the destructor - the compiler needs to know how to destroy the object when it goes out of scope.

By the way, typically all class member function definitions are in the same file.
Last edited on
Yeah, I not creating the destructor was the source of my problems! Thanks for the help!
Topic archived. No new replies allowed.