Why does my C++ code not work?

Posted on 2014-08-07
Last Modified: 2014-08-07

I have created a simple program consisting of a number of header files into which I have placed various functions. I have tried to run it but get an error message. Could someone please have a look?

Question by:f19l
    1 Comment
    LVL 86

    Accepted Solution

    Your header files are included as

    #include "bankacct.h"
    #include "bankacdef.h"

    Open in new window

    yet they seem to be named with a '.hpp' suffix - change that to '.h'.

    Then, you are using 'account[]' in "bankacdef.h", which is declared in your 'main()'. To get that to run, make it a global variable in the -cpp file and be sure to declare it before including "bankacdef.h", i.e.

        //preprocessor calls...
    #include "bankacct.h"
    BankAccount account[MAX]; // <-- !!!
    #include "bankacdef.h"
    int main()
        //instantiate three objects of bank accounts...
        //set the account details...
        account[0].setAccHolder("Charles Darwin");
        account[1].setAccHolder("Michael Faraday");
        account[2].setAccHolder("Charles Babbage");
        //display the account details...
        cout<<"\n Account Number 1: "
        cout<<" Account Holder 1: "    
        cout<<" Account Balance 1: "
        cout<<"\n Account Number 2: "
        cout<<" Account Holder 2: "    
        cout<<" Account Balance 2: "
        cout<<"\n Account Number 3: "
        cout<<" Account Holder 3: "    
        cout<<" Account Balance 3: "
        cout<<"\n\t Press any key to close.\n";
        return 0;

    Open in new window

    Are you sure you don't want to turn "bankacdef.h" into a "bankacdef.cpp"? That would make sense, since it contains code.

    BTW, with the above changes, it compiles for me.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now