0

I am new to C++ . I am trying to develop a C++ application but there is one error that is keep bothering me.

Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)"

I think I have referenced all of my functions in another file

This is the code of my cpp file:

#include "stdafx.h"




int console::main()
{
    system("cls");
    cout << "-----------ABC Homestay Management System-----------" << endl;
    cout << "------------------------------------------------------------" << endl;

system("color 0f");
cout << "Please enter your choice" << endl;
cout << "1.Upload listings" << endl;
cout << "2.Find listings" << endl;
cout << "3.View listings" << endl;
cout << "4.Exit" << endl;

int choice;
cin >> choice;

switch (choice) {
case 1:
    //renter();
    break;
case 2:
    //finder();
    break;
case 3:
    //listings();
    break;
case 4:
    exit(0);
    break;
case 8:
    //staff();//secret key -> 8 for staff
    break;
}

    system("pause");
main();
return 0;
 }

    void console::finder() {
        system("cls");
            cout << "test" << endl; 

    }

This is the "stdafx.h" header file that I have referenced in the cpp file :

    #pragma once

#include "targetver.h"
#include <iomanip>
#include <ctime>
#include <time.h>

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <regex>
#include <stdlib.h>
#include <string.h>
#include<algorithm>
#include<iterator>
#include <chrono>
#include <thread>

using namespace std;



class console {

public:
    console() {}
    ~console() {}
    int main();
    void renter();
    void finder();
    void listings();
    void staff();



};
cklai
  • 41
  • 1
  • 1
  • 10

3 Answers3

4

Your program doesn't have a main function which is the entry point of a C++ program. The console::main() function cannot serve for this purpose, BTW you don't have any variable of type console at all in your program, therefore you never can call any of the methods of the console class. I think you should start reading your C++ text book from the beginning.

You want this:

...
int main()
{
  console myconsole;
  myconsole.main();
}

and BTW, this is fishy:

system("pause");
main();          // you probably want to remove this
return 0;

you probably want a loop here.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

In C++, entry point of program is main function and it has to be outside of any class. In your code, you declared int main() inside console class.

Rhathin
  • 1,176
  • 2
  • 14
  • 20
1

All c++ programs must have an entry function called main that is not part of any class.

Correct routine would be a main function that creates your object that represents the bulk of your program and then run that's main function:

int main()
{
  console myconsole;
  myconsole.main();
}
Prodigle
  • 1,757
  • 12
  • 23
  • an object that "represents the bulk of your program" ? I guess it is just the wording that disturbs me a bit, but imho representing the bulk of a program is far too much to put in a single class, a class should be responsible for only one thing – 463035818_is_not_an_ai Sep 04 '18 at 10:10
  • For example if you were making a game, it's common form to have a "Game" class that contains references to your higher level manager classes and contains a Game.Run function. and then related program functions for exiting etc. Probably better to call it a manager class or a program management class. It's your entry point in relation to the type of program you're making. – Prodigle Sep 04 '18 at 10:14