0
votes

I get the following error trying to run C++ project in VS

My main.cpp (ATM machine.cpp)

#include <iostream>
#include <fstream>
#include <string>
#include "Account.h"
using namespace std;

class options
{
    private:
        char user_chose;
        int id;
        int pass;

    public: 

        void login()
        {
            // Get credentials
            cout << "Please enter your user id: ";
            cin >> id;
            cout << "Please enter your password: ";
            cin >> pass;

        }

        void quit()
        {
            cout << "quiting...";
        }

        void IntroMenu()
        {
            cout << "Please select an option from the menu below :" << endl;
            cout << "l -> Login" << endl;
            cout << "c -> Create New Account" << endl;
            cout << "q -> Quit" << endl;
            cout << "> ";
            cin >> user_chose;

            switch (user_chose)
            {
                case ('l'):
                case ('L'):
                    login();
                    break;

                case ('c'):
                case ('C'):

                    Account i;
                    i.createAccount();
                    break;

                case ('q'):
                case ('Q'):
                    quit();
                    break;

                default:
                {
                    cout << "\n***Invalid option***\n" << endl;
                    IntroMenu(); //Recall function
                }
            };
        };
};

int main()
{
    cout << "Hi!Welcome to the ATM Machine!" << endl;
    options start;
    start.IntroMenu();

    return 0;
}

My header (Account.h)

#ifndef ACCOUNT_H_INCLUDED
#define ACCOUNT_H_INCLUDED

class Account
{
    public:
        void createAccount();
};

#endif

(Account.cpp)

#include "Account.h"
using namespace std;

Account::createAccount();
void Account::createAccount() 
{
    //Save account on database(txt file)

    cout << "\nAccount created successfully\n" << endl;
}

Error 1

LNK2019 unresolved external symbol "public: void __thiscall Account::createAccount(void)" (?createAccount@Account@@QAEXXZ) referenced in function "public: void __thiscall options::IntroMenu(void)" (?IntroMenu@options@@QAEXXZ)

Error 2

LNK1120 1 unresolved externals

Thanks in advance!

1
Account::createAccount(); this is a syntax error. Since the compiler doesn't show it, your Account.cpp is not added to the VS project.S.M.

1 Answers

0
votes

In your cpp file:

#include "Account.h"
using namespace std;


Account::createAccount(); // remove this


void Account::createAccount() 
{
    cout << "\nAccount created successfully\n" << endl;
}

After removing that line your program should run.

EDIT

After that fix it works fine for me.

You can try to move

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

toAccount.h and removing the others using namespace std;, leaving only #include <Account.h> in main().

This hints to missing libraries, so check also if the Account class files are in the project root directory.

If that doesn't solve it take a look at C++ compile error (LNK1120 and LNK2019) with Visual Studio.