this is a pretty simple code that just is coming up with an error even though I have it written the same way other people doing the same code have it
1>assigntment5.obj : error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl promptForString(class std::basic_string,class std::allocator >)" (?promptForString##YA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V12##Z) referenced in function _main
1>c:\users\aweb\documents\visual studio 2010\Projects\Assignment5\Debug\Assignment5.exe : fatal error LNK1120: 1 unresolved externals
the .cpp file
#include <iostream>
#include <string>
#include "anw65_Library.h"
using namespace std;
string promptForString(string prompt);
int main()
{
string name = promptForString("What is the filename?: ");
system("pause");
return 0;
}
the .h file
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static string promptFromString(string prompt)
{
string filename;
cout << prompt;
cin >> filename;
return filename;
}
You never define prompt**For**String, you defined prompt**From**String. Spelling matters. Also:
Why are you defining functions in your .h file? Just declare them there and define them in the .cpp file (unless they're templates).
Don't put using namespace <whatever> in a header file. You're just mucking up the global namespace of whatever includes your header.
You don't need to mark that function as static.
This line:
string promptForString(string prompt);
In your .cpp file is causing issues. It's forward delcaring a function with external linkage. However, the function your header is:
static string promptFromString(string prompt)
{
...
The important part here is the static. static means it has internal linkage. Either get rid of the static, or get rid of the forward declaration, because a function can't have both internal and external linkage.
Edit: also, Ed S. made a good find with your typo.
you call promptForString() from your main function while you have promptFromString() defined in .h file.
You might want to change one of the definitions.
Related
I am trying to follow the tutorial here: UWP Xaml Hosting API.
I am at the part of the tutorial where I'm supposed to create a blank app that defines a XamlApplication application. I have defined it in my header (.h) as:
#pragma once
#include "App.xaml.g.h"
namespace winrt::UI_Host::implementation
{
struct App : Microsoft::Toolkit::Win32::UI::XamlHost::XamlApplicationT<App>
{
App();
~App();
};
}
My .cpp file has it defined as:
#include "pch.h"
#include "App.h"
using namespace winrt;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;
using namespace UI_Host;
using namespace UI_Host::implementation;
winrt::UI_Host::implementation::App::App()
{
Initialize();
}
winrt::UI_Host::implementation::App::~App()
{
Close();
}
If I leave my .IDL file as:
namespace UI_Host{}
It compiles fine, but I can't use the App class in my Win32 Program. So I changed the IDL file to this:
namespace UI_Host
{
[default_interface]
runtimeclass App : Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication
{
App();
}
}
But now it won't compile. The error I get when compiling is this:
>module.g.obj : error LNK2019: unresolved external symbol "void * __cdecl winrt_make_UI_Host_App(void)" (?winrt_make_UI_Host_App##YAPEAXXZ) referenced in function "void * __cdecl winrt_get_activation_factory(class std::basic_string_view<wchar_t,struct std::char_traits<wchar_t> > const &)" (?winrt_get_activation_factory##YAPEAXAEBV?$basic_string_view#_WU?$char_traits#_W#std###std###Z)
Does anyone know why?
C++/WinRT 2.0 introduced a breaking change in order to support Optimized Components. It is used when passing -optimize to cppwinrt.exe. This option is enabled by default for new projects.
The breaking change requires component authors to #include a generated implementation file into the compilation unit that implements that particular type. In your case, you need to #include "App.g.cpp" into App.cpp (make sure to #include it after the header file App.h).
To allow your code to compile with and without the -optimize flag, you can conditionally include App.g.cpp:
#include "App.h"
#if __has_include("App.g.cpp")
# include "App.g.cpp"
#endif
For easy to digest lessons you can read Raymond Chen's blog entry titled Why does my C++/WinRT project get errors of the form "Unresolved external symbol void* __cdecl winrt_make_YourNamespace_YourClass(void)"?.
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.
I am having some trouble with the implementation of a constructor and I cannot figure out what is the problem. I have spent a lot of time with this problem: it would be grateful some kind of advice.
The main code is:
#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
Rocket* rocket;
rocket=new Rocket();
//LLA* position=rocket->positionLLA;
return 0;
}
Rocket.h
#pragma once
#include "LLA.h"
class Rocket {
public:
Rocket(); // Default constructor
LLA* positionLLA;
};
Rocket.cpp
#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
using namespace std;
Rocket::Rocket() // Default constructor
{
// Initialise the position
positionLLA=new LLA();
}
The error says:
error LNK2019: unresolved external symbol "public: __thiscall Rocket::Rocket(void)" (??0Rocket##QAE#XZ) referenced in function _main.
I know the error has something to do with not having declared a variable, but I think I have declared all classes and constructors.
PS: I am using Visual Studio 2008 in order to add dependencies.
I am assuming LLA is correctly defined in your .h file
Are you compiling Rocket.cpp into Rocket.o and main.cpp into main.o and then linking the two object files together?
Your error seems to imply that the linker cannot obtain symbol information from Rocket.o, which usually means Rocket.o was not found
Oh, as a detail, since Rocket is using an LLA*, you don't need to include LLA.h in Rocket.h, you can simply forward-declare
class LLA;
You might want to add #ifndef, #define, and #endif to your header files. Having multiple includes of the same thing in different files can lead to complications.
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.
I have an implementation for printing out enum values in c++
If I put all the code in a .h file, everything works nicely. If I separate out the function implementation into .cpp files, I get a linker error.
Here is my main file
#include <iostream>
#include <vector>
#include "Day.h"
using namespace std;
int main(){
initializeDayNames();
Day a = Clubs;
cout << a;
}
Here is the .h file
#ifndef __Day__
#define __Day__
#include <iostream>
#include <vector>
#include <string>
using namespace std;
enum Day {Clubs, Hearts, Diamonds, Spades} ;
vector<string> DayNames = vector<string>();
ostream & operator<<(ostream & out, Day cs);
void initializeDayNames();
#endif
and the .cpp file
#include <iostream>
#include "Day.h"
#include<string>
#include<vector>
using namespace std;
void initializeDayNames(){
DayNames.push_back("Clubs");
DayNames.push_back("Hearts");
DayNames.push_back("Diamonds");
DayNames.push_back("Spades");
}
ostream & operator<<(ostream & out, Day cs){
out << DayNames[cs];
return out;
}
What am I doing wrong here
The specific error is
Error 1 error LNK2005: "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > DayNames" (?DayNames##3V?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##std##A) already defined in Day.obj main.obj
and
Error 2 fatal error LNK1169: one or more multiply defined symbols found
You are initializing two copies of
vector<string> DayNames = vector<string>();
because you included the header twice.
You should replace it with
extern vector<string> DayNames;
in the h file and
vector<string> DayNames = vector<string>();
in the cpp file.
Also you seem to have two copies of
ostream & operator<<(ostream & out, Day cs);
The reason your header guards isn't helping this case is that when you include the headers, you basically duplicate the definitions in all your files that you have included the header.
In C, when you declare a variable, you basically instantiate it/allocate static space for it. When you put the variable declaration in the header, what you are effectively doing is allocating static storage space for the variable in two different objects, giving you your error.
You're missing extern on the DayNames declaration.