Unresolved external symbol(LNK2019) [duplicate] - c++

This question already has answers here:
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
(20 answers)
Closed 4 years ago.
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();
};

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.

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.

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();
}

Related

"endl" causes "C1001" error

My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...

C++ Headers & Undefined Reference

So I'm just getting to grips with C++ and progressing onto using header files. Thing is, I'm totally confused. I've read a documentation but none to make me understand it well enough.
I'm just making a silly 'game' which is interactive, it will probably be discarded and thought I could practice use on header files. This is my file structure:
terminal_test
├── core.cpp
└── game
├── game.cpp
└── game.h
Now, here is my core.cpp:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include "game/game.h"
using namespace std;
void mainMenu();
void rootInterface() {
cout << "root#system:~# ";
}
int main() {
system("clear");
usleep(2000);
mainMenu();
return 0;
}
void mainMenu() {
int menuChoice = 0;
cout << "[1] - Start Game";
cout << "[2] - How To Play";
cout << endl;
rootInterface();
cin >> menuChoice;
if ( menuChoice == 1 ) {
startGame();
} else if ( menuChoice == 2 ) {
cout << "This worked.";
}
}
Everything else works fine but startGame(); under my menu choice. When I compile using g++ core.cpp game/game.cpp it bounces back with this error: undefined reference to startGame();. I firstly did some troubleshooting to see if it was properly finding game.h by changing the #include "game/game.h" to something like #include "game.h" without the directory listed inside and it gave me a game.h could not be found so I know it's recognising it, just not compiling at all.
Here is my game.h:
#ifndef GAME_H // Making sure not to include the header multiple times
#define GAME_H
#include "game.h"
void startGame();
#endif
game.cpp:
#include <iostream>
#include <stdio.h>
#include "game.h"
int main(int argc, char const *argv[]) {
void startGame() {
cout << "It worked.";
}
return 0;
}
My file structure isn't named properly either, I just threw it in because it was something to just get to grips with header files in C++.
So, here are my questions:
1) - What is this error specifically saying and what should I do to fix it?
2) - How do header files communicate and work with other files and is there clear documentation/guides out there that can help?
Local function definitions are not what you want here:
#include <iostream>
#include <stdio.h>
#include "game.h"
int main(int argc, char const *argv[]) {
// an attempt to define startGame() inside of main()
void startGame() {
cout << "It worked.";
}
return 0;
}
main is not needed in your game.cpp file. You should define startGame() outside of main, like this:
#include <iostream>
#include <stdio.h>
#include "game.h"
// definition of startGame
void startGame() {
cout << "It worked.";
}

Error 1 error LNK2019: unresolved external symbol "public: void __thiscall Login::loginMenu(void)

I searched through the website and there were answers about default constructor or using #pragma. but I'm using #pragma in my visual studio and I tried to debug but any of those methods didnt work. Please tell where I have made the mistake. Thank you
this is my main,
#include "stdafx.h"
#include<iostream>
#include "Login.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Login log;
log.loginMenu();
std::cin.get();
return 0;
}
login.h is as following,
#pragma once
class Login
{
public:
void loginMenu();
};
and the Login.cpp file,
#include "stdafx.h"
#include "Login.h"
#include<iostream>
#include<string>
using namespace std;
void loginMenu()
{
int userType;
do{
cout << "Select 1 for STAFF" << endl;
cout << "Select 2 for HR MANAGER" << endl;
cout << "Select 3 for ADMINISTRATOR" << endl;
cout << "Please select your usertype";
cin >> userType;
switch(userType){
case 1:
cout << "You have selected STAFF";
break;
case 2:
cout << "You have selected HR MANAGER";
break;
case 3:
cout << "You have selected ADMINISTRATOR";
break;
default:
cout << "Please make your choice by selecting from 1-3";
}
}while(userType==1,userType==2,userType==3);
}
This is a simple program I created to demonstrate "using classes in separate files.
You have declared, and are calling, a function Login::loginMenu - a member function of class Login - but you haven't implemented it. You have implemented a function ::loginMenu - a non-member stand-alone function - but you are not calling it.
Make it
void Login::loginMenu() {...}

Conflicting Errors in Visual Studio (C++)

I have a function "PrintHeader" for my project, defined in io.cpp. Even though io.h is included in my main file, I get the error
error C3861: 'PrintHeader': identifier not found.
When I copy the function for PrintHeader into my main file, I get the errors
error LNK2005: 'void _cdeci PrintHeader(void)" (?PrintHeader##YAXXZ) already defined in io.obj.
and
error LNK1169: one or more multiply defined symbols found.
I can understand the second error set, since I do have it defined twice, but I don't understand why it doesn't work when I just remove the duplicate definition. Any help is greatly appreciated.
Main file
#include "stdio.h"
#include <iostream>
#include "io.h"
void PrintHeader()
{
cout << endl;
cout << "Month\tPrincipal\t Interest\t Balance" << endl;
cout << "-----\t---------\t---------\t---------" << endl;
}
int main()
{
cout << "Hello World\n";
PrintHeader();
getchar();
return 0;
}
io.cpp
#include <iostream>
#include <iomanip>
#include "io.h"
void PrintHeader (void)
{
cout << endl;
cout << "Month\tPrincipal\t Interest\t Balance" << endl;
cout << "-----\t---------\t---------\t---------" << endl;
}
io.h
#ifndef __IO_H__
#define __IO_H__
#include <string>
using namespace std;
void PrintHeader (void);
#endif
You are most likely including the wrong file in main.cpp. You can make sure it is the right file by right clicking on the include "io.h" and choosing open file.

Making C++ pause

Is there a C++ equivalent to Python's time.sleep()?
Use boost::this_thread::sleep
// sleep for 5 seconds
boost::this_thread::sleep(boost::posix_time::seconds(5));
The following code will sleep for 10 milliseconds.
boost::this_thread::sleep(boost::posix_time::milliseconds(10))
Refer to boost::posix_time::time_duration for more ways to construct the duration.
I'm not aware of any portable function, but mainstream OSes have usleep for *nix and Sleep for Windows.
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch