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

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.

Related

I'm getting unresolved external symbol winrt_make_* when trying to compile C++/WinRT blank app from XamlApplication

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)"?.

C++ Wrapper for TFS API

I have 2 projects in VS 2012 trying to build a C++ wrapper for the Team Foundation Server API.
#pragma once
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::ObjectModel;
using namespace Microsoft::TeamFoundation::Client;
using namespace Microsoft::TeamFoundation::Framework::Common;
using namespace Microsoft::TeamFoundation::Framework::Client;
namespace ManagedDll {
public ref class TFSAUth
{
public:
TFSAUth();
~ TFSAUth();
void AuthUser()
{
System::String ^x = gcnew String( "http://myhost:8080/tfs");
System::String ^u = gcnew String( "user");
System::String ^p = gcnew String( "Pass");
System::Net::NetworkCredential ^c = gcnew System::Net::NetworkCredential(u, p);
TeamFoundationServer ^something = gcnew TeamFoundationServer(x, c);
something->Authenticate();
System::String ^col = gcnew String (something->TfsTeamProjectCollection->Name);
Console::WriteLine(col);
}
private:
};
TFSAUth:: TFSAUth()
{
}
TFSAUth::~ TFSAUth()
{
}
}
__declspec(dllexport) void Auth()
{
ManagedDll::TFSAUth work;
work.AuthUser();
}
This is the library project and I have a Test Application using this library.
#include "stdafx.h"
#include "conio.h"
#include <iostream>
#include <windows.h>
#include "ManagedDll.h"
_declspec(dllexport) void Auth();
int _tmain()
{
Auth();
system("pause");
return 0;
}
Was all working for a while, opened this project to see if I can get any further. Today it fails to compile, giving me linker errors.
TestApp.obj : error LNK2019: unresolved external symbol "void __cdecl
Auth(void)" (?Auth##YAXXZ) referenced in function _wmain
I am not sure why as nothing has changed. But more than that, I am wondering whether I am doing the right thing while creating the C++ wrapper for TFS API.
Any help/examples/suggestions is much appreciated as this is my first attempt at a wrapper.
In your native C++ project (Test Application), you have to include the lib file of the managed C++ project (ManagedDll).
The exported DLL function in the manage C++ DLL is treated as the same as that in the native C++ DLLs, you can follow this: Lib Files as Linker Input to make it work.
Also, you should remove
_declspec(dllexport) void Auth();
in the Test Application.

Error LNK2019 while implementing a constructor

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.

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 (simple code)

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.