lcr game c++ LINK 2019 error in Visual Studio - c++

I am currently working on a school project due Sunday. It is an LCR Dice game in C++ using Visual Studio 2017. I cannot get past 2 link2019 errors. I have included code of all instances where these functions are defined and called. I tried to change the way that I call them and tried to remove the static Player::directions but no luck. Because it is a linker error, Visual Studio does not find it until compile time. I have tried to search for answers, but I cannot find anything specific to my problem. Any help would be appreciated. Thank you.
Here are the exact errors:
"LNK2019 unresolved external symbol "public: void __thiscall
Player::setName(void)" (?setName#Player##QAEXXZ) referenced in function
_main LCR Game C:\Users\docmo\Desktop\SNHU files\IT 312 C++
Programming\LCR Game\LCR Game\LCR Game.obj 1"
and
"LNK2019 unresolved external symbol "public: static void __cdecl
Player::directions(void)" (?directions#Player##SAXXZ) referenced in
function _main LCR Game C:\Users\docmo\Desktop\SNHU files\IT 312 C++
Programming\LCR Game\LCR Game\LCR Game.obj 1"
Here is the C++ code:
Player.h:
#pragma once
#include "stdafx.h"
#include <iostream>
#include "Dice.h"
class Player
{
public:
int chips;
int numPlayers;
std::string name = "";
Player() = default;
void setName();
void setChips();
int checkChips();
static void directions();
};
Player.cpp:
#include "stdafx.h"
#include "Player.h"
#include "Dice.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace System;
using namespace System::IO;
void Player::setName()
{
std::cin >> name;
}
...<more code>
void Player::directions() //display directions to player
...<more code>
LCR Game.cpp
// LCR Game.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
using namespace std;
...<more code>
for (int i = 0; i < numPlayers; i++) //loop to set names and chips for each
player
{
cout << "Enter player name: " << endl;
players[i].setName();
players[i].chips = 3;
}
Player::directions(); //display game rules to player
return 0;
}

I found my problem. It is a beginners foolish mistake. Even though I added the files in Microsoft Studio to my project, it did not include them. I had to select each file in the Server Explorer and then select Project then include in project. Thanks for all the help.

Related

Simple calculator should take 2 seconds to fix its like 10 lines

Trying to make a simple Calculator but I can't even get my first function to work. I've been trying to be as organized as I can with my headers and .cpps because I remember back in the day those were important. Haha. I'm under the impression the header file is included in the main.cpp and it has the header guards. These are just declarations for my functions correct? And then Calculation'sFunctions.cpp is where I write the code for the previous declaration function I made on my header file. I'm also not sure when to include iostream and stdafx.h and all that etc. Anyway thanks in advance for your help guys, here is my 3 files all I'm trying at this point is to get an integer from the user.
Calculator.cpp
#include "stdafx.h"
#include <iostream>
#include "CalculatorDeclarations.h"
int main()
{
int getFirstInteger(int userInput)
return 0;
}
CalculatorDeclarations.h
#ifndef ADD_H
#define ADD_H
#include "stdafx.h"
#include <iostream>
int getFirstInteger();
#endif
CalculationsFunctions.cpp
#include "stdafx.h"
#include <iostream>
int getFirstInteger(int userInput)
{
std::cout << "Please enter the first integer you would like to use." << std::endl;
std::cin >> userInput;
return userInput;
}
Errors:
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) ConsoleApplication1 c:\Users\Shane\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\MSVCRTD.lib(exe_main.obj) ‌​1
Error LNK1120 1 unresolved externals ConsoleApplication1 c:\users\shane\documents\visual studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe 1
Well your first problem is your 'main' method
int main()
{
int getFirstInteger(int userInput)
return 0;
}
The syntax is incorrect (should be lined ended with a semi-colon). And you should be passing an int. So it could be modified to this
int main()
{
int j = 1;
getFirstInteger(j)
return 0;
}
But it's worth looking at the implementation here. You're getting the user input within that method, so no need to pass in anything. And you're not using the output, so no need to return anything.
Also you've double included #include<iostream> This won't be a big issue in a small program but it's a waste and bad practice. Only include this where it's needed (which is in CalculationsFunctions.cpp). Don't think you need #include "stdafx.h" at all.

When i try to debug it cant find .exe file in VS 2013 with update 4 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
So this is my code,
//Precompiled Libaries
#include <iostream>
#include <string>
#include <string.h>
#include <Windows.h>
#include <conio.h>
#include <process.h>
#include <time.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
//Program To Run From
using namespace std;
int intNumber1;
string strName;
void Menu(void){
cout << "Please Select Your Choice " << strName << "!";
Sleep(2000);
}
void Name(void){
cout << "Please Enter Your Name" << endl;
cin >> strName;
Menu();
}
void Main(void){
system("COLOR C");
Name();
}
its just simple stuff, probably wrong as just learnt in college, but these are the errors when building,
1>------ Build started: Project: Revision Application, Configuration: Debug Win32 ------
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users------\Desktop\Revision Application\Debug\Revision Application.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Then when debugging,
Unable to start program 'C:\Users------\Desktop\Revision Application\Debug\Revision Application.exe'.
The system cannot find the file specified.
im probably being silly but i cant find a fix anywhere :( please help!
C++ is case-sensitive, and the entry function is called main, not Main. Also, it must return int, not void. Replace
void Main(void){
with
int main() {
to make your code compile and link. Then, once you built the .exe, you will be able to use the debugger on it.
Note that only in main, you do not explicitly have to return a value from it even though the return type is not void. main returns 0 by default. By convention, this indicates to the surrounding shell that the program ended without error.

Error: Class computer comp is already defined in computer.obj

I am currently playing with C++, and attempting to rebuild a Tic Tac Toe batch console game I made in C++, but have hit a wall, where I cannot figure out how to get rid of the error TicTacToe.obj : error LNK2005: "class computer comp" (?comp##3Vcomputer##A) already defined in computer.obj. I have tried removing the declaration of the function computer from the header, and the definition of the function in the C++, but that didn't fix the error. The only way I figured out how to remove this error was to remove the object name, which I kind of don't want to do. I used the example given on the website http://www.cplusplus.com/doc/tutorial/classes/ to set up the class computer. Any information you can provide on any errors that I currently have, or any functions I may not need are most definately welcome, as I am wanting to know much much more about C++.
CODE:
TicTacToe.cpp
// TicTacToe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include "computer.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
comp.Select();
Sleep(1000);
}
computer.cpp
#include "stdafx.h"
#include "computer.h"
#include <iostream>
using namespace std;
computer::computer()
{
}
computer::~computer()
{
}
void computer::Select()
{
}
computer.h
#pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
} comp;
EXTRA INFO:
I am using Microsoft Visual Studio Professional 2013 on a laptop running Windows 7.
As you included header "computer.h" in both modules computer.cpp and TicTacToe.cpp then the both modules contain the same definition of object comp
pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
} comp;
So the linker issues the error.
Define the object only in one cpp module. The header should contain only the class definition.
For example
computer.h
#pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
};
TicTacToe.cpp
// TicTacToe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include "computer.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
computer comp;
comp.Select();
Sleep(1000);
}
You have to remove comp from the header file. Create the object in a cpp file like this:
computer comp;
You said you don't want to do that. If that causes some other problem for you then post a new question about that problem.
You are defining comp in the header, and so in every .cpp that includes that header, so you are breaking the One Definition Rule.
Instead you can declare it in the header:
extern computer comp;
And then define it in exactly one .cpp:
computer comp;
Which will still allow you to access it from any .cpp that includes the header.

link 2019 Error For Simple Class C++

I can't seem to fix this LNK2019 Error that I keep getting on visual studio 2013.
I've been looking on stack exchange for a while, but I think my code is fine.
The error is a result of creating a ParkingMeter variable. I'm not sure how to fix this. Any help would be appreciated.
ParkingMeter.h:
#ifndef PARKINGMETER
#define PARKINGMETER
using namespace std;
class ParkingMeter{
private:
int minPurchased;
public:
ParkingMeter(int);
ParkingMeter();
int getMinutes();
};
#endif
ParkingMeter.cpp:
using namespace std;
#include "ParkingMeter.h"
ParkingMeter::ParkingMeter(int minutes)
{
minPurchased = minutes;
}
ParkingMeter::ParkingMeter(){
minPurchased = 0;
}
int ParkingMeter::getMinutes(){ return minPurchased; }
test.cpp:
#include <iostream>
#include "ParkingMeter.h"
using namespace std;
int main()
{
ParkingMeter meter(2);
}
Full error message:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall ParkingMeter::ParkingMeter(int)" (??0ParkingMeter##QAE#H#Z) referenced in function _main C:\Users\Max\Documents\Visual Studio 2013\Projects\Project3\Project3\test.obj
I don't see any problem with this code.
I have removed below code from your ParkingMeter.h and ParkingMeter.cpp. (keep in test.cpp file)
using namespace std;
Edit: It seems you have not added ParkingMeter.cpp in your project. Please right click on your project - > Add -> existing Item -> and provide cpp file. You are good to go!

error LNK2019: unresolved external symbol newbie needs tip

I am just probing some more advanced coding in C++, so imagine my frustration when I can't solve this:
1>Main.obj : error LNK2019: unresolved external symbol "int __cdecl playerAtk(void)" (? playerAtk##YAHXZ) referenced in function _main
1>C:\Users\Hezekiah Detinc\documents\visual studio 2010\Projects\Custom_1\Debug\Custom_1.exe : fatal error LNK1120: 1 unresolved externals
I had tried searching for an answer here, but the solutions I have found were too specific to be of any use for me.
If anyone can answer; What generally causes this problem? What are some solutions to fixing it?
This is my Main.cpp;
//Custom 1
//Include files that may be used. will cut out unused files in release.
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include "EnemyBaseClass.h"
#include "PlayerBaseClass.h"
using namespace std;
int main()
{
Enemy emy;
Player plr;
emy.enemyAtk();
emy.enemyDef();
emy.enemyHp();
plr.playerAtk();
plr.playerDef();
plr.playerHp();
int playerAtk();
cout << playerAtk();
cout << "You're attacked by a Monster!\n";
system(" pause ");
return 0;
}
If I need to post my headers or the other _.cpp files, let me know.
You declare and use playerAtk as a function (with no implementation, thus the undefined reference)
Change
int playerAtk();
...
cout << playerAtk();
to
int playerAtk;
...
cout << playerAtk;
(You probably want to initialize the variable)
The error is simple: You are using a function which you have declared, but you have not written any definition (Implementation). Check your source (.cpp) files.