Cannot convert from const TiXmlString to const std::string& - c++

I am willing to make a 2D game with SDL and I am following a Shaun Mitchell's book. But I faced serious difficulties with this user-defined type conversion error when trying to compile my project...
Also, I am unfamiliar with this topic. I've watched some tutorials and searched the web for a solution.
Do I need to add
operator std::string&() const { return ???;}
to the tinyxmlstr.h in TiXmlString class? If so, how to implement it? What should I return?
The Errors
If I don't define the STL (which I use) in tinyxml.h, the compiler then returns a linkage error.
Error 19 error LNK2019: unresolved external symbol "public: virtual __cdecl TiXmlNode::~TiXmlNode(void)" (??1TiXmlNode##UEAA#XZ) referenced in function "public: virtual __cdecl TiXmlDocument::~TiXmlDocument(void)" (??1TiXmlDocument##UEAA#XZ)
Without STL
StateParser class and implementation is the same as the one in the book.
Finally, if I have a mistake somewhere, how to debug it properly and where to look for it? Thank you, in advance!

The error is clear: non conversion available from const TiXmlString to const std::string &.
According to this page, if I'm not wrong, there isn't even a direct conversion from TiXmlString to a std::string
I suppose you can write a method like this (exactly like the value() that return a const char *)
std::string valueStr () cont
{ return value.c_str(); }
but in this way you return a copy of value (so return a const std::string, instead a plain std::string, is useless), not a reference. I don't know if this it's OK for you.

Related

Forward-declare struct which only has definition in a library cpp

I'm using the bullet 3 physics library, which has the following struct definition inside one of the cpps:
struct btSingleContactCallback : public btBroadphaseAabbCallback
{
btCollisionObject* m_collisionObject;
btCollisionWorld* m_world;
btCollisionWorld::ContactResultCallback& m_resultCallback;
btSingleContactCallback(btCollisionObject* collisionObject, btCollisionWorld* world,btCollisionWorld::ContactResultCallback& resultCallback)
:m_collisionObject(collisionObject),
m_world(world),
m_resultCallback(resultCallback)
{
}
virtual bool process(const btBroadphaseProxy* proxy)
{
btCollisionObject* collisionObject = (btCollisionObject*)proxy->m_clientObject;
if (collisionObject == m_collisionObject)
return true;
//only perform raycast if filterMask matches
if(m_resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
{
btCollisionObjectWrapper ob0(0,m_collisionObject->getCollisionShape(),m_collisionObject,m_collisionObject->getWorldTransform(),-1,-1);
btCollisionObjectWrapper ob1(0,collisionObject->getCollisionShape(),collisionObject,collisionObject->getWorldTransform(),-1,-1);
btCollisionAlgorithm* algorithm = m_world->getDispatcher()->findAlgorithm(&ob0,&ob1);
if (algorithm)
{
btBridgedManifoldResult contactPointResult(&ob0,&ob1, m_resultCallback);
//discrete collision detection query
algorithm->processCollision(&ob0,&ob1, m_world->getDispatchInfo(),&contactPointResult);
algorithm->~btCollisionAlgorithm();
m_world->getDispatcher()->freeCollisionAlgorithm(algorithm);
}
}
return true;
}
};
The problem is, the struct is never declared in any of the headers, I need to be able to create an object of this type however. The bullet libraries are statically linked, so I figured I should just be able to declare it myself in my main program as such:
struct btSingleContactCallback
: public btBroadphaseAabbCallback
{
btCollisionObject *m_collisionObject;
btCollisionWorld *m_world;
btCollisionWorld::ContactResultCallback &m_resultCallback;
btSingleContactCallback(btCollisionObject *collisionObject,btCollisionWorld *world,btCollisionWorld::ContactResultCallback &resultCallback);
virtual bool process(const btBroadphaseProxy *proxy);
};
This actually works fine, as long as I'm compiling in debug mode. However, when trying to compile in release mode, I'm getting an unresolved symbol error:
physenvironment.obj : error LNK2001: unresolved external symbol "public: __cdecl btSingleContactCallback::btSingleContactCallback(class btCollisionObject *,class btCollisionWorld *,struct btCollisionWorld::ContactResultCallback &)" (??0btSingleContactCallback##QEAA#PEAVbtCollisionObject##PEAVbtCollisionWorld##AEAUContactResultCallback#2##Z)
Could this have anything to do with c++'s name mangling? Is there a way to avoid it, without having to start making modifications in the library itself?
From a five minute look at the library code, you actually should use ContactResultCallback which is public, letting the implementation of btCollisionWorld::contactTest create and use the private btSingleContactCallback for you.
You did not implement the constructor.

Passing C++ vector to method results in unresolved externals

I've looked at a ton of examples and I don't know what I'm doing wrong. I can't pass a vector as a parameter or the compiler says unresolved externals.
Here's an example that doesn't work for me:
In the .h:
#include <vector>
void VectorTest(std::vector<std::string> & vect);
In the .cpp:
void VectorTest(std::vector<std::string> & vect)
{
}
Also in the .cpp, in a method where I'm trying to call it:
std::vector<std::string> test;
VectorTest(test);
When I compile I get the error:
error LNK1120: 1 unresolved externals
If I comment out
VectorTest(test);
It builds. I'm still learning C++ so it's probably something obvious. I'm using Visual Studio Express 2013 if that matters.
On a Yahoo! Answers someone posted this example and it does not work for me, same error:
void myfunc( std::vector<int>& vec )
{
}
std::vector<int> vec;
myfunc( vec );
Here's the error (trying to use the myfunc shown above):
error LNK2019: unresolved external symbol "public: void __thiscall trackManager::myfunc(class std::vector<int,class std::allocator<int> > &)" (?myfunc#trackManager##QAEXAAV?$vector#HV?$allocator#H#std###std###Z) referenced in function "public: __thiscall trackManager::trackManager(void)" (??0trackManager##QAE#XZ)
It looks like you declared VectorTest() as a trackManager member function in the header but then defined it as a free function in the cpp. This results in two unrelated functions with the same name. If you try to call VectorTest() without qualifications from inside a trackManager member function, the resolver will (sensibly) prefer the version that is also a trackManager member over the free function. But since that one doesn't have a definition, the linker can't figure out what to do with it and you get an error.
To fix this, either move the declaration out of the body of trackManager (if you want it to be a free function), or write the definition as void trackManager::VectorTest(...) (if you want it to be a member).
You're most likely missing the include file for string. Try to add the following line at the top of the .h file:
#include <string>

Unresolved external symbol on non-inline function body

These type of errors are well discussed on SO and many other places, yet, I couldnt find a solution to my particular case.
Basically, I have made a static library project in my solution to seperate some functionality, and reference it in my current project. When I try to call a function from the library, i got this notorious linker error. I think there are no problems in setting up the project (references, dependencies etc.) .
First, I will give some minimal example of what I am working on ( there are some third party classes )
//FileReader.h
class IBKFileReader{
public:
virtual ~IBKFileReader() {} ;
virtual void readFile(std::string fileName, pcl::PointCloud<pcl::PointXYZ>::Ptr pointCloud ) = 0 ;
};
class XYZFileReader : public IBKFileReader
{
public:
virtual void readFile(std::string fileName, pcl::PointCloud<pcl::PointXYZ>::Ptr pointCloud )
{
}
And in my code, I call readFile function on a IBKFileReader object polymorphically.
And, this part is ok, without any errors, it compiles, links and runs.
However, when I move the body of the readFile function to a .cpp file and implement it there
(with the same signature hopefully, i copy pasted) , I got the unresolved external symbol error. This is the header of the function in .cpp file.
void XYZFileReader::readFile(std::string fileName, pcl::PointCloud<pcl::PointXYZ>::Ptr pointCloud )
It seems that implementing the function body inline in the header file, and implementing in some seperate file makes a difference.
What do you think of this issue? Has anybody experienced something similar to this? I hope I am not missing out something obvious.
EDIT:
This is the error log i get:
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl XYZFileReader::readFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class boost::shared_ptr<class pcl::PointCloud<struct pcl::PointXYZ> >)" (?readFile#XYZFileReader##UEAAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$shared_ptr#V?$PointCloud#UPointXYZ#pcl###pcl###boost###Z)
EDIT:
I may have caused some misunderstanding.
FileReader.h and FileReader.cpp is in static library project. I didnt do more than just implementing the function in the cpp file, definitions and such are still there.
In the same solution, in another project I reference to this project.
I am not getting the linker error when compiling the static lib, but when I am using it.
Sorry for my bad English.
A member function that is defined inside its class member list is called an inline member function. Try moving the member definition out of the class definition.
class XYZFileReader : public IBKFileReader
{
public:
virtual void readFile(std::string fileName,
pcl::PointCloud<pcl::PointXYZ>::Ptr pointCloud);
};
void XYZFileReader::readFile(std::string fileName,
pcl::PointCloud<pcl::PointXYZ>::Ptr pointCloud) {}

Inherit from std::string without npos problems in DLLs

A lot of years ago i made the mistake to inherit from std::string in DLL and put some funktions like "MakeUpper" to the string. Converting from VS2008 to VS2012 the problem whith the npos pops up. With a workaround from microsoft * i get some other DLLs working, but not DLLs which are used late-binding and have no DLLMain() function.
*Microsoft Workaround
#if _MSC_VER >= 1600
#include <string>
const std::basic_string<char>::size_type std::basic_string<char>::npos = (std::basic_string<char>::size_type) -1;
#endif
The class is declared in a DLL like this:
class MYDLL_API CNyString : public std::string
the errorline is this:
error LNK2001: unresolved external symbol "public: static unsigned int const std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::npos" (?npos#?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##2IB)
Rewriting the MyString class with an std::string as attribute does not work because it is used in several other DLLs where MyString and string functions are mixed a lot, also in function parameters. Example:
void Foo(const CMyString &Param_) ..
...
Foo(std::string("Hallo World")..
Does anyone has an idea how to solve this problem ?
Thanks in advance.

Link error: unresolved operator << for std::basic_ostream with CStringT

I just reorganized some libraries of my Visual C++ (7.1) project and got trouble with the linker i cannot resolve.
The Project links MFC as well as Standard Windows Libraries, all MBCS
Somewhere, there is something like :
std::stringstream sstr;
sstr << m_MyCStringVar << std::endl;
(this line, as well as some others, needs << for basic_stream and CString)
Everything was fine until i merged 2 other libraries into 1 libraries (just moving the code/files from a to b without changing much)
Suddenly, all my exe's produce the linker error:
BasicFunctionsD.lib(CAccess.obj) : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class ATL::CStringT > > const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABV?$CStringT#DV?$StrTraitMFC_DLL#DV?$ChTraitsCRT#D#ATL#####ATL###Z) referenced in function "protected: void __thiscall CAccessor::CreateCategory(int,char const *,char const *)" (?CreateCategory#CAccessor##IAEXHPBD0#Z)
(above code wasn't affected from the library merge, at least not directly)
As far as i can recognize, the << operator for basic ostream and CString is not found.
Maybe the lib containing MFC versions of basic_ostream is not found?
But i have no idea how to fix it or even where to start searching for the real problem.
Any hints would be nice
arg...
Simple reason: there is no std::ostream operator with CString... it was my own code and i just did not remember... :(
During cleanup , the function went into a namespace and got lost
D'oh!
namesspace StupidcleanupIshouldNotHavedone
{
std::ostream & operator<<(std::ostream & s, const CString & str)
{
s << (LPCTSTR)str;
return s;
}
}
I'm probably wrong but IIRC i did experience a problem with unresolved links. The solution was get ready...... #include <string>. Turns out one of the headers defined string and i was able to use string normally and compile with no problem. However there was no static implementation since i forgot the header so maybe your missing one.