#pragma once

class Account
{
public:
	//default constructors
	Account();
	//constructor
	Account(double newSavingsBalance);		
	//Methods
	
	//double MonthlyInterestRate();
	//double newBalance();
	void getMonthlyInterestRate();
	static double AnnualInterestRate();
	static void ModifyInterestRate(double newAnnualInterestRate);
	void setSavingsBalance(double newSavingsBalance);
	double getSavingsBalance();
	double getAnnualInterestRate();

public:
	~Account(void);

private:
	double SavingsBalance;
	//static double AnnualInterestRate;	
};
#include "StdAfx.h"
#include "Account.h"

Account::Account(void)
{
	
}
Account::Account(double newSavingsBalance)
{
	setSavingsBalance(newSavingsBalance);
}
Account::~Account(void)
{
}

void Account::getMonthlyInterestRate()
{
	setSavingsBalance(getSavingsBalance() + (getSavingsBalance() * (AnnualInterestRate / 12)));
}
//gets the current balance in account
double Account::getSavingsBalance()
{
      return SavingsBalance; 
}

void Account::setSavingsBalance(double newSavingsBalance)
{
	SavingsBalance = newSavingsBalance;
}
void Account::ModifyInterestRate(double newAnnualInterestRate)
{
	AnnualInterestRate = newAnnualInterestRate;
}
// SavingsAccount.cpp : main project file.

#include "stdafx.h"
#include "Account.h"
#include <iostream>
using namespace System;
using namespace std;


int main(array<System::String ^> ^args)
{
	Account Eddie(2000.00);
	Account Amy(3000.00);

	Eddie.ModifyInterestRate(0.03);
	Eddie.getMonthlyInterestRate();
	Amy.getMonthlyInterestRate();

	cout<<Eddie.getSavingsBalance()<<endl;



	
	return 0;
}

Before I commented out the private: static double AnnualInterestRate I was getting a link error - unresolved token private static double Account::AnnualInterestRate

so I made it public and I got the same error.

Dani AI

Generated

Short answer: the linker error came from not giving the class a real storage location for the static member. A static data member declared inside a class is only a declaration — you must provide a single out‑of‑class definition (in one .cpp) so the linker can find the object. pointed this out and that is the correct fix.

A few clarifications from the code shown here:

  • The header contains static double AnnualInterestRate(); — that declares a function, not the static variable you are trying to use. If you want a static variable, declare it (once) in the class and then provide exactly one definition in a .cpp file so storage is allocated.
  • If you put the definition in a header (without C++17 inline semantics) you get multiple definitions, which also produces link errors. If you are using C++17 or newer you can instead use an inline static data member to initialize it in-class; otherwise keep a single definition in a .cpp.

Other small improvements to make the class clearer and safer:

  • Rename getMonthlyInterestRate() if it changes the balance (e.g., applyMonthlyInterest) so the name matches behaviour.
  • Make getters const (e.g., getSavingsBalance() const) and consider returning the monthly interest instead of having a void method mutate state.
  • Static methods like ModifyInterestRate can be called on the class (Account::ModifyInterestRate(...)) which makes intent clearer.

For the formal rules and the C++17 inline option, see the C++ reference on static data members and inline variables:

This explains why ’s out‑of‑class definition solved the unresolved token error for .

Recommended Answers

All 2 Replies

>>//static double AnnualInterestRate
static class variables are just like global variables with a class scope. They have to be declared at the top of a *.cpp file just as if they were normal global variables.

// account.cpp file
#include "StdAfx.h"
#include "Account.h"

double Account::AnnualInterestRate = 0;
// rest of code goes here

Seriously Wow!!!, thank you so much. From all my reading and all my classes I never got that.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.