0

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!

OfficialAhmed
  • 41
  • 1
  • 8
  • 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. – 273K Mar 18 '20 at 15:17
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 273K Mar 18 '20 at 15:18

1 Answers1

0

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • @OfficialAhmed I added some more details to my answer. – anastaciu Mar 18 '20 at 15:09
  • 1
    I used code blocks instead of visual studio and everything works fine .thank you anyway – OfficialAhmed Mar 20 '20 at 12:55
  • 1
    @OfficialAhmed I'm glad you were able to solve it one way or the other, it's a strange thing since I ran your code in mine and it ran fine, maybe it's some configurantion isue or dependency absence, but if your satisfied with codeblocks there is no need to lose more time trying to figure out what the problem is. – anastaciu Mar 20 '20 at 14:59