The formatting for C++ lnk2019 and lnk2020 is correct

Seeking assistance with my code and the errors I'm encountering - any guidance would be highly valued.

Currently facing an issue with getNextAcountNumber. I attempted to assign the account ID value, but encountered a problem as it is a private member.

Error 1 error LNK2019: unresolved external symbol "private: int __thiscall Account::getNextAccountNumber(void)const " (?getNextAccountNumber@Account@@ABEHXZ) referenced in function "public: __thiscall Account::Account(class std::basic_string,class::allocator >,double)" (??0Account@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@N@Z) H:\programming\AccountClass\AccountClass.obj AccountClass

H:\programming\AccountClass\Debug\AccountClass.exe : fatal error LNK1120: 1 unresolved externals

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include "Account.h"

using namespace std;

int main()
{
Accountacct01;
Accountacct02("Harold M. Ferguson", 2000);
Accountacct03("Elise Janet Simmons", 3500);
Accountacct04("James Holder", 0);

cout << endl << "Account information - initial" << endl;
acct01.displayAccountInfo();// show account information - initial values
acct02.displayAccountInfo();
acct03.displayAccountInfo();
acct04.displayAccountInfo();

acct01.setAccountHolder("Mary A. Tarleton");
acct01.setBalance(542.39);
acct04.setAccountHolder("James Ellis Holder");
acct04.setBalance(1990.75);

cout << endl << "Account information after changes" << endl;
acct01.displayAccountInfo();// show account information after changes
acct02.displayAccountInfo();
acct03.displayAccountInfo();
acct04.displayAccountInfo();

acct01.depositAmount(455);// make deposits
acct02.depositAmount(-19.95);// negative deposit not allowed - set to zero
acct03.depositAmount(4365.27);
acct04.depositAmount(95.63);

cout << endl << "Account information after deposits" << endl;
acct01.displayAccountInfo();// show account information after deposits
acct02.displayAccountInfo();
acct03.displayAccountInfo();
acct04.displayAccountInfo();

acct01.withdrawAmount(37.39);
acct02.withdrawAmount(-475.25);// withdrawal may be positive or negative (absolute value)
acct03.withdrawAmount(0.25);
acct04.withdrawAmount(50.00);

cout << endl << "Account information after withdrawals" << endl;
acct01.displayAccountInfo();// show account information after withdrawals
acct02.displayAccountInfo();
acct03.displayAccountInfo();
acct04.displayAccountInfo();

cout << endl;
system("pause");
return 0;
}

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "Account.h"
#include <string>


using namespace std;

  

Account::Account()
{

accountID = getNextAccountNumber();
accountHolder = "no name";
balance = 0;
}

Account::Account(string name, double amount)
{

accountID = getNextAccountNumber();
accountHolder = name;
balance = amount;

}



intAccount::getAccountID()const
{

return accountID;
}
stringAccount::getAccountHolder()const
{
return accountHolder;
}
doubleAccount::getBalance()const
{
return balance;
}

voidAccount::setAccountHolder(string name)
{
accountHolder = name;
}
voidAccount::setBalance(double amt)
{
amt = balance;
}
voidAccount::depositAmount(double amt)
{
if (amt > 0)
{
balance = +amt;
}
else;
{
balance = balance;
}
}
voidAccount::withdrawAmount(double amt)
{
balance = balance - amt;
}

voidAccount::displayAccountInfo()const
{
cout << accountID << accountHolder << balance;
}

#ifndef ACCOUNT_H
#define ACCOUNT_H

#include <string>

using namespace std;

staticintaccountNumber = 100000;// starting value for account#

class Account
{
public:
Account();// default constructor
Account(string name, double amount);// constructor with two parameters

intgetAccountID()const;// ACCESSOR member functions
stringgetAccountHolder()const;// return name of account holder
doublegetBalance()const;// return account balance

voidsetAccountHolder(string name);// MUTATOR member functions
voidsetBalance(double amt);// assign amount to balance
voiddepositAmount(double amt);// add amount to balance
voidwithdrawAmount(double amt);// subtract absolute value of amount from balance
// HELPER member functions
voiddisplayAccountInfo()const;// display account information

private:
intgetNextAccountNumber()const;// get next account# (pre-increment account number)
// private DATA members
intaccountID;// account# identifier
stringaccountHolder;// name of account holder
doublebalance;// account balance
};


// accountID = ++accountNumber    
#endif

Answer №1

The issue at hand does not stem from the function being private; rather, it is being invoked by the constructor(s), which have the authority to access the private elements of the class.

Both @DrewDormann and @PaulMcKenzie appear to be on point in their remarks - there is no implementation provided (or at least not clearly stated) for Account::getNextAccountNumber()

This should ideally be included in the second code snippet that you shared (most likely in Account.cpp, or a similar location)

You might want to consider incorporating something like

int Account::getNextAccountNumber()
{
    return accountNumber++;
}

However, this function seems to be unrelated to any specific Account entity, so you may want to consider making it a standalone function rather than a member of the Account class

In this scenario, you would only have

int getNextAccountNumber()
{
    return accountNumber++;
}

However, you will need to modify the header file (potentially Account.h?) to remove the member function. Otherwise, you will continue to encounter the linker error that you are currently facing.

Edit: The function is declared as const, so you must include the const specifier.

int Account::getNextAccountNumber() const
{
    return accountNumber++;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for notifying a user about leaving a page without saving their information

I created a small table where users can input product names and numbers. My question is, how can I notify the user if they try to leave the page without saving the entered information? <form action="index.php" method="post"> <input type="text" ...

Use custom styles or CSS for the file input element: <input type="file">

Can CSS be used to achieve consistent styling for <input type="file"> in all browsers? ...

Tips for designing scrollable overlay content:

I am currently in the process of building a page inspired by the design of Hello Monday. Right now, I have added static content before implementing the parallax effect and auto-scroll. Here is my progress so far: Check out the Sandbox Link One challenge ...

Selecting CSS tag excluding the first-child element

My goal is to use CSS to target the li elements in a list that do not have a description and are not the first-child with a b element. I want to apply padding-left to these specific li elements that do not have a title. <ul> <li><b>t ...

Tips for implementing text-overflow ellipsis in an HTML input box?

On my website, I have input fields where I've added the following CSS styling: .ellip { white-space: nowrap; width: 200px; overflow: hidden; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis; text-overflow: ellipsis; } H ...

How to apply custom styling to a specific element within an Angular Material 2 component using CSS based on its Id or

My EntryComponent features a material button menu where I attempted to customize the default style using ::ng-deep. However, the changes affected all button components in the parent Component as well. <style> ::ng-deep .mat-button{ max-width: 50 ...

The div element is failing to adjust its height correctly to match its parent container

I'm struggling to make the red border that wraps around the text extend all the way to the bottom of its parent div. Setting the height to 100% isn't achieving the desired effect, as it needs to match the height of the image. Tip: Try resizing y ...

Is it possible to conceal any spans that are in close proximity to the cursor when hovered over?

Currently, I am working on a project that involves multiple spans placed side by side, with each span containing a letter of the text. My aim is to create a functionality where hovering over one of these spans will not only hide that particular span but al ...

Can a Python script be used to compile a C/C++ program?

Right now, I am working on a Python script to build a C/C++ Linux kernel using the following commands: subprocess.check_call(["make", "-j5"]) subprocess.check_call(["make", "-j5", "modules_install"]) subprocess.check_call(["make", "-j5", "install"]) Curr ...

Is there a way to incorporate the ::after or ::before pseudo-elements into an image tag?

While working on my project, I attempted to incorporate the ::after tag (I had used ::before as well) for an image within an img tag. However, I am facing difficulties as it doesn't seem to be working. Any advice or guidance would be greatly appreciat ...

Is there a way to quickly read and dispose of data in C++ without the need to store it?

While working on a competitive programming problem, I encountered a situation where I needed to input a value but did not need to use it at all. My goal was to input the value and immediately discard it without storing it in any way. In C, this could be a ...

Creating a responsive design in HTML and CSS to organize images into two columns that stack

<html> <head> <style> .container { position: relative; width: 50%; } .image { opacity: 1; display: block; width: 100%; height: auto; transition: .5s ease; backface-visibility: hidden; } .middle { transition: .5s ea ...

Margin isn't functioning properly

My section is not centered and adding margin-left doesn't seem to be working. Any suggestions on how I can solve this? Take a look at how it appears. Here's the code: .margins { margin-left: 100px; } <link href="https://cdn.jsdelivr.net ...

Master the art of displaying complete text when zooming in and elegantly truncating it when zooming out

I am currently working on a data visualization project using d3.js. The tree chart that I have created is functioning well, but I would like the text to react dynamically when zooming in and out. You can find the code for my project on this JSFiddle page. ...

Assigning Memory on Arduino Board

Just starting out in the world of low-level programming, I've decided to delve into the fascinating realm of Arduino. Armed with an Arduino Mega 2560, I've been immersing myself in memory management as part of my course curriculum. While I consid ...

Adjust image size to maintain consistent dimensions

Is there a way to resize the images so they maintain the same aspect ratio? Currently, all the images are appearing distorted. https://i.sstatic.net/czpto.png Here is the HTML code for the card: <div class="container"> <div class="flex- ...

Create a uniform Bootstrap 5 album grid showcasing various text lengths for a visually cohesive display

Currently, I have designed a bootstrap album which can be viewed here: https://getbootstrap.com/docs/5.0/examples/album/ However, the text displayed in my album varies in length, resulting in an uneven layout like this: https://i.sstatic.net/Qwrnx.png ...

What could be causing the Runtime Error(SIGABRT) to appear when I try to submit this code on codechef?

I am facing an issue with my code - it executes successfully on my local system but throws a runtime error (Runtime Error(SIGABRT)) when I try to submit it on CodeChef. For more context, here is the question: Could someone please help me understand what ...

Is there a way to completely remove all styling from a specific child element?

Struggling with a drop-down menu issue - the parent element has a border, but I don't want that style to be inherited by the child sub-menu. Despite my attempts to remove the border using border: 0px, it's not working as expected. Is there any wa ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...