link 2019 Error For Simple Class C++ - 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!

Related

How to pass object as parameter into function from .h and .cpp

I am relatively new to C++, but have come from Python and C.
I am using an SDK for a lidar sensor. I have 5 main files that are involved; SDK.h, SDK.cpp, setup.h, setup.cpp and main.cpp.
A class is defined within the SDK.
rplidar.h
class RPlidarDriver{
public:
static RPlidarDriver * CreateDriver(_u32 drivertype = DRIVER_TYPE_SERIALPORT);
// more code
}
main.cpp
#include <iostream>
#include "rplidar.h"
#include "setup.h"
using namespace std;
using namespace rp::standalone::rplidar;
int main()
{
//code
RPlidarDriver* lidar = RPlidarDriver::CreateDriver(DRIVER_TYPE_SERIALPORT);
start_reading(lidar, scanMode);
//code
}
setup.cpp
#include "setup.h"
#include "rplidar.h"
using namespace std;
using namespace rp::standalone::rplidar;
void start_reading(RPlidarDriver* driver, const char* scanMode)
{
//start motor
driver->startMotor();
//more code...
}
setup.h
#include "rplidar.h"
using namespace rp::standalone::rplidar;
namespace setup
{
void start_reading(RPlidarDriver* driver, const char* scanMode);
}
However I get this error
main.obj : error LNK2019: unresolved external symbol "void __cdecl setup::start_reading(class rp::standalone::rplidar::RPlidarDriver *,char const *)" (?start_reading#setup##YAXPAVRPlidarDriver#rplidar#standalone#rp##PBD#Z) referenced in function _main
I also get the same error for other functions that I try to use the object as a parameter.
If I put the function in setup.cpp into main.cpp, it compiles easily. I tried to implement & and use the parameter as a reference instead, but not luck.
main.cpp and setup.cpp both need to be compiled and the resulting object files need to be linked together along with the SDK libraries. The error you're getting is telling you you're trying to link the final binary using just main.o and the SDK without setup.o That's why the linker is failing to find the symbol with the implementation of your start_reading function.

VS is giving me another solution to my constructor but I want to know why mine wont work [duplicate]

This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 3 years ago.
My constructor has a green line underneath it saying "function definition not found".
Visual Studio has given me a fix, but I want to know why mine doesn't work.
#pragma once
#include "class_dayType.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
dayType day;
string d;
cout << "Enter day of week: ";
cin >> d;
day.set_day(d);
}
#include <iostream>
#include<string>
using namespace std;
class dayType {
public:
string day;
dayType(); //constructor with green line
void set_day(string day_of_week) {
string day = day_of_week;
}
};
Visual Studio created this in another file and it worked. What is the difference between this and my constructor?
dayType::dayType()
{
}
Errors:
LNK2019 unresolved external symbol "public: __thiscall dayType::dayType(void)" (??0dayType##QAE#XZ) referenced in function _main Day_of_Week
LNK1120 1 unresolved externals Day_of_Week
dayType();
This is not a definition, it's just a declaration. It indicates that a constructor (or any function) will be present somewhere in the code later on.
You would need
dayType()
{
}
Read more here and here.

lcr game c++ LINK 2019 error in Visual Studio

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.

VS 2012 Error LNK2019

I'm here so I can find a solution for my problem. I know this is too simple, but somehow I can't figure out where's the error in my code!
Here you have it:
AulaData.h
#ifndef AULADATA_H_
#define AULADATA_H_
#include <string>
using std::string;
class AulaData
{
private:
int dia;
public:
AulaData(int dia);
};
#endif
AulaData.cpp
#include "AulaData.h"
AulaData::AulaData(int dia)
{
}
And finally, my Main.cpp:
#include <vector>
#include "AulaData.h"
using namespace std;
int main(int argc, char* argv[])
{
AulaData a(12);
getchar();
return 0;
}
The error I'm getting is the following(something that never happened to me):
1>ConsoleApplication1.obj : error LNK2019: unresolved external symbol
"public: __thiscall AulaData::AulaData(int)" (??0AulaData##QAE#XZ)
referenced in function _main
Although if I define the class constructor without arguments, it'll work.
I'd appreciate a lot if someone could help me! :) I'm trully getting frustrated because all seems ok.
Thanks in advance!
I think that you must provide a default constructor as well if you create one with arguments.

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.