error LNK2019 and fatal error LNK1120 - lnk2019

I encouneter a problem when compiling my projet. When I set this line:
boost::shared_ptr mySwap;
I have no problem but when I set this one:
boost::shared_ptr mySwap(new OvernightVsLiborBasisSwap(OvernightVsLiborBasisSwap::PayerOvernight,
1.0,
scheduleOis,
indexOis,
dayCountOis,
1.0,
scheduleLibor,
indexLibor,
dayCountLibor));
I have the following error message
excelFunctions.obj : error LNK2019: unresolved external symbol "public: __thiscall ModLibNY::OvernightVsLiborBasisSwap::OvernightVsLiborBasisSwap(enum ModLibNY::OvernightVsLiborBasisSwap::Type,double,class QuantLib::Schedule const &,class boost::shared_ptr const &,class QuantLib::DayCounter const &,double,class QuantLib::Schedule const &,class boost::shared_ptr const &,class QuantLib::DayCounter const &,double,double,bool,bool,class boost::optional,class boost::optional)" (??0OvernightVsLiborBasisSwap#ModLibNY##QAE#W4Type#01#NABVSchedule#QuantLib##ABV?$shared_ptr#VOvernightIndex#QuantLib###boost##ABVDayCounter#4#N1ABV?$shared_ptr#VIborIndex#QuantLib###6#3NN_N5V?$optional#W4BusinessDayConvention#QuantLib###6#6#Z) referenced in function "class xlw::NCMatrix __cdecl getOisLiborSwapCurve(class xlw::CellMatrix const &,class xlw::CellMatrix const &,class xlw::CellMatrix const &,class xlw::CellMatrix const &,double const &)" (?getOisLiborSwapCurve##YA?AVNCMatrix#xlw##ABVCellMatrix#2#000ABN#Z)
3>.\Debug\ExplainPnL.xll : fatal error LNK1120: 1 unresolved externals
The object OvernightVsLiborBasisSwap is part of my own-made static library ModLibNY which is included. The first lines of my code constain:
#include <ql/quantlib.hpp>
#include <ml/modlibny.hpp>
#include <ml/swaps/overnightvsliborbasisswap.hpp>
using namespace std;
using namespace xlw;
using namespace QuantLib;
using namespace ModLibNY;
What is very wierd is that I ve the error when I use the constructor. For information it is declared in the .hpp file as
#ifndef overnight_vs_libor_basis_swap_hpp
#define overnight_vs_libor_basis_swap_hpp
#include <ql/quantlib.hpp>
using namespace std;
using namespace QuantLib;
namespace ModLibNY {
class InterestRateIndex;
class OvernightVsLiborBasisSwap : public Swap {
public:
enum Type { ReceiverOvernight = -1, PayerOvernight = 1 };
class arguments;
class results;
class engine;
OvernightVsLiborBasisSwap(
const OvernightVsLiborBasisSwap::Type type,
const Real nominal1,
const Schedule &schedule1,
const boost::shared_ptr<OvernightIndex> &index1,
const DayCounter &dayCount1,
const Real nominal2,
const Schedule &schedule2,
const boost::shared_ptr<IborIndex> &index2,
const DayCounter &dayCount2,
const Real spread1 = 0.0,
const Real spread2 = 0.0,
const bool intermediateCapitalExchange = false,
const bool finalCapitalExchange = false,
boost::optional<BusinessDayConvention> paymentConvention1 =
boost::none,
boost::optional<BusinessDayConvention> paymentConvention2 =
boost::none);
and defined in the .cpp file:
#include "StdAfx.h"
#include "overnightvsliborbasisswap.hpp"
#include <ql/quantlib.hpp>
using namespace QuantLib;
namespace ModLibNY {
OvernightVsLiborBasisSwap::OvernightVsLiborBasisSwap(
const OvernightVsLiborBasisSwap::Type type,
const Real nominal1,
const Schedule &schedule1,
const boost::shared_ptr<OvernightIndex> &index1,
const DayCounter &dayCount1,
const Real nominal2,
const Schedule &schedule2,
const boost::shared_ptr<IborIndex> &index2,
const DayCounter &dayCount2,
const Real spread1,
const Real spread2,
const bool intermediateCapitalExchange,
const bool finalCapitalExchange,
boost::optional<BusinessDayConvention> paymentConvention1,
boost::optional<BusinessDayConvention> paymentConvention2)
: Swap(2),
type_(type),
nominal1_(std::vector<Real>(schedule1.size() - 1, nominal1)),
nominal2_(std::vector<Real>(schedule2.size() - 1, nominal2)),
schedule1_(schedule1),
schedule2_(schedule2),
index1_(index1),
index2_(index2),
dayCount1_(dayCount1),
dayCount2_(dayCount2),
intermediateCapitalExchange_(intermediateCapitalExchange),
finalCapitalExchange_(finalCapitalExchange)
{
init(paymentConvention1, paymentConvention2);
}
... If someone have an idea of the problem origin, please let me know!
Thanks

Related

Error invalid conversion from 'const char*' to 'char*'

I Expect to get the name which I sent to the "class stud" which has a class variable CHAR name
The issue appears specifically at this line this->name->set_name_send(OBJ.name->get_name());
and I get this error " -- invalid conversion from 'const char*' to 'char*' -- "
Any suggestions?
enter image description here
If I make the function set_name as a const and delete the pointers in the destructor code runs but return value is garbage and it has to be 0
Maybe the class *name doesn't have to be pointer ?
All pointer and in OOP
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
class CHAR
{
char *name;
public:
CHAR(char *mes = "\0") {(this)->name = new char[100]; strcpy((this)->name,mes);}
CHAR(const CHAR&OBJ) { (this)->name = new char[100]; strcpy((this)->name,OBJ.name); }
~CHAR(){cout<<"CHAR Destructor!"<<endl; delete name;}
void set_name(char *mes){(this)->name = new char[100]; strcpy((this)->name,mes);}
const char* get_name() const {return (this)->name;}
};
class stud
{
CHAR *name;
public:
stud(char *mes)
{
this->name->set_name(mes);
}
stud( stud&OBJ)
{
this->name->set_name(OBJ.name->get_name()); //!!!!!!!!!!!!!!! issue HERE!!
}
~stud(){cout<<"Destructor!"<<endl;}
const char* get_name() const {return this->name->get_name();}
};
int main()
{
stud S("John");
stud A(S);
cout<<"Name: "<<A.get_name()<<endl;
}

C++ / CLI linker error because of SHAssocEnumHandlers function

To reproduce the issue,
Create a project > VC++ > CLR > Class library, name the project "ClassLibrary2" and use below two files as is.
// ClassLibrary2.h
#pragma once
#include <msclr\marshal.h>
#include <ShObjIdl.h>
using namespace System;
namespace ClassLibrary2 {
public ref class Class1
{
public : void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext);
};
}
// ClassLibrary2.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "ClassLibrary2.h"
void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext)
{
msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
PCWSTR pszStr = context->marshal_as<const wchar_t*>(ext);
IEnumAssocHandlers *pEnumHandlers = NULL;
/* below line is producing problems */
SHAssocEnumHandlers(pszStr, ASSOC_FILTER_RECOMMENDED, &pEnumHandlers);
}
Now, if I build the project, I get following errors :
Error 2 error LNK2028: unresolved token (0A0000A2) "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
Error 3 error LNK2019: unresolved external symbol "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
How to remove those linker errors ?

LINK2019 still keeps going

I'm trying to resolve an unresolved external (link2019 error).There were already some posts on this issue and i try them, but I still could not figure it out.
The error is caused by my CreateFtdcMdApi function ( right?) but my understanding is that this is "resolved".
// testTraderApi.cpp : 定义控制台应用程序的入口点。
//
#include "MdSpi.h"
#include <iostream>
// UserApi对象
CThostFtdcMdApi* pUserApi = NULL;
// 配置参数
char FRONT_ADDR[] = "tcp://asp-sim2-md1.financial-trading-platform.com:26213"; // 前置地址
TThostFtdcBrokerIDType BROKER_ID = "***"; // 经纪公司代码
TThostFtdcInvestorIDType INVESTOR_ID = "0***"; // 投资者代码
TThostFtdcPasswordType PASSWORD = "*****"; // 用户密码
char *ppInstrumentID[] = { "***", "***" }; // 行情订阅列表
int iInstrumentID = 2; // 行情订阅数量
// 请求编号
int iRequestID = 0;
void main(void)
{
// 初始化UserApi
char file = 'F';
pUserApi = CThostFtdcMdApi::CreateFtdcMdApi(); // 创建UserApi
CThostFtdcMdSpi* pUserSpi = new CMdSpi();
pUserApi->RegisterSpi(pUserSpi); // 注册事件类
pUserApi->RegisterFront(FRONT_ADDR); // connect
pUserApi->Init();
pUserApi->Join();
// pUserApi->Release();
}
I truncated the code a bit, because it is verbose.
class MD_API_EXPORT CThostFtdcMdApi
{
public:
///创建MdApi
///#param pszFlowPath 存贮订阅信息文件的目录,默认为当前目录
///#return 创建出的UserApi
///modify for udp marketdata
static CThostFtdcMdApi *CreateFtdcMdApi(const char *pszFlowPath = "", const bool bIsUsingUdp=false, const bool bIsMulticast=false);
///获取API的版本信息
///#retrun 获取到的版本号
static const char *GetApiVersion();
}
Error:
1>------ Build started: Project: challenges, Configuration: Debug Win32 ------
1>challenges.obj : error LNK2019: unresolved external symbol "public: static class CThostFtdcMdApi * __cdecl CThostFtdcMdApi::CreateFtdcMdApi(char const *,bool,bool)" (?CreateFtdcMdApi#CThostFtdcMdApi##SAPAV1#PBD_N1#Z) referenced in function _main
1>d:\documents\visual studio 2013\Projects\challenges\Debug\challenges.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

googletest integrated in CLR/CLI Compilation error LNK2028

Im totally new to google testing in CLI. Just managed to set up and integrate google test with visual studio 2012. However, when I try to include the header file of my project with my tester.h file (because I want to test the functions in that headerfile), I encountered the following error:
Error 2 error LNK2028: unresolved token (0A0006FB) "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jacky\Desktop\EzXpns3\test_project\main.obj test_project
Error 3 error LNK2019: unresolved external symbol "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jia Wei\Desktop\EzXpns3\test_project\main.obj test_project
I have tried including dependencies gtestd.lib, kernel32.lib, user32.lib, advapi32.lib, Ws2_32.lib
Problem slightly resolved. Instead of having expenseTracker.h and ExpenseTracker.cpp, I placed all my implementation of ExpenseTracker.h in the header file itself and everything compiled nicely and test run. However, my entire project has been built on both header file and cpp file and its not very wise to redo everything for testing purposes right? Could anyone help with this?
Attached below are my header files.
//tester.h
#include "gtest/gtest.h" //include to use Google Unit test's stuff
#include "C:\Users\Jacky\Desktop\EzXpns3\Source - testing\EzXpns2\ExpenseTracker.h"
using namespace std;
class ExpenseTracker;
/************************************************************************/
/* We write test cases here */
/************************************************************************/
TEST(basic_test, add_simple_route)
{
ASSERT_EQ(1, 1);
}
TEST(enter_settings, user_login)
{
ExpenseTracker :: ExpenseTracker();
//loadUserInfo();
string username = "XXX_XXX";
string password = "12345";
//myTracker -> loadUserInfo();
//bool result = myTracker -> login(username, password);
//ASSERT_EQ(true, result);
}
void runTest(int argument_count, char** argument_vars)
{
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar(); //pause the program after all the testing
}
//main.cpp
#include "tester.h"
#include <cstdio>
using namespace System;
using namespace testing;
int main(int argument_count, char** argument_vars)
{
//int argc;
//char** argv;
//runTest(argc, argv);
//InitGoogleTest(argc, argv);
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar();
return 0;
}
//ExpenseTracker.h, the file which I want to include
#ifndef _EXPENSETRACKER_H
#define _EXPENSETRACKER_H
#include <string>
#include <vector>
#include "user2.h"
using namespace std;
class ExpenseTracker
{
private:
vector<User*> allMyUsers;
public:
ExpenseTracker(); //empty constructor
void addUser(User*);
int findUser(string);
bool login(string, string);
void loadUserInfo();
User* getUser(string);
int getUserSize();
};
#endif;
You need to provide an implementation for the constructor. In other words, you should change:
ExpenseTracker(); //empty constructor
into:
ExpenseTracker() {} // empty constructor
However, since the constructor is empty you might as well remove it entirely!

Singleton code linker errors in vc 9.0. Runs fine in linux compiled with gcc

I have a simple logger that is implemented as a singleton. It works like i want when I compile and run it with g++ in linux but when I compile in Visual Studio 9.0 with vc++ I get the following errors. Is there a way to fix this? I don't mind changing the logger class around, but I would like to avoid changing how it is called.
1>Linking...
1>loggerTest.obj : error LNK2005: "public: static class Logger * __cdecl Logger::getInstance(void)" (?getInstance#Logger##SAPAV1#XZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::log(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?log#Logger##QAEXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::closeLog(void)" (?closeLog#Logger##QAEXXZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "private: static class Logger * Logger::_instance" (?_instance#Logger##0PAV1#A) already defined in Logger.obj
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path#Logger##0V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path#Logger##0V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex#Logger##0Vmutex#boost##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex#Logger##0Vmutex#boost##A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log#Logger##0V?$basic_ofstream#DU?$char_traits#D#std###std##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log#Logger##0V?$basic_ofstream#DU?$char_traits#D#std###std##A)
The code, three files Logger.h Logger.cpp test.cpp
#ifndef __LOGGER_CPP__
#define __LOGGER_CPP__
#include "Logger.h"
Logger* Logger::_instance = 0;
//string Logger::_path = "log";
//ofstream Logger::_log;
//boost::mutex Logger::_mutex;
Logger* Logger::getInstance(){
{
boost::mutex::scoped_lock lock(_mutex);
if(_instance == 0) {
_instance = new Logger;
_path = "log";
}
} //mutex
return _instance;
}
void Logger::log(const std::string& msg){
{
boost::mutex::scoped_lock lock(_mutex);
if(!_log.is_open()){
_log.open(_path.c_str());
}
if(_log.is_open()){
_log << msg.c_str() << std::endl;
}
}
}
void Logger::closeLog(){
Logger::_log.close();
}
#endif
` ...
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include <iostream>
#include <string>
#include <fstream>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
using namespace std;
class Logger {
public:
static Logger* getInstance();
void log(const std::string& msg);
void closeLog();
protected:
Logger(){}
private:
static Logger* _instance;
static string _path;
static bool _logOpen;
static ofstream _log;
static boost::mutex _mutex; //check mutable
};
#endif
test.cpp
`
#include <iostream>
#include "Logger.cpp"
using namespace std;
int main(int argc, char *argv[])
{
Logger* log = Logger::getInstance();
log->log("hello world\n");
return 0;
}
This is a problem because you've defined the symbol multiple times by compiling the CPP then also including it in test.cpp... By convention you should only include the declaration, not the definition as you've done.
I am surprised that gcc would allow one to be so lax about this.
change
#include "Logger.cpp"
to
#include "Logger.h"
and give it a try.
The problem is because you have done #include "Logger.cpp" instead of #include "Logger.h" in test.cpp . Because of this, the symbols inside the Logger.cpp will be defined multiple times (once for the translation unit logger.cpp and once for test.cpp). Having multiple inclusion guard doesn't help because it works only within a translation unit.

Resources