I encounter a problem using std::string in parameters :
MyClass has a method like this
public:
void loadDatas(string filename);
In my main.cpp I have the following simple code :
#include <iostream>
#include <string>
#include "myclass.hpp";
using namespace std;
int main()
{
string foo = "test.txt";
cout << foo << endl; // Print Hello Kitty, no problems
MyClass i;
// The following line raise :
// obj/Release/src/main.o||In function `main':|
// main.cpp|| undefined reference to `MyClass::loadDatas(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|
// ||=== Build finished: 1 errors, 0 warnings ===|
i.loadDatas(foo);
return EXIT_SUCCESS;
}
So it looks like libstdc++ is well linked (because I’m able to print the text using cout), but passing a string in parameters raise an error and I don’t understand why.
Could someone help me please ?
EDIT : I made a mistake, in fact it is i.loadDatas(foo); (I corrected it). My source code isn’t in english so I tryed to made a simple version for you in english. I really call an instance method and not a static method.
EDIT 2 : Complete source code
personnage.hpp
#ifndef PERSONNAGE_H
#define PERSONNAGE_H
#include <iostream>
#include <string>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include "carte.hpp"
using namespace std;
class Personnage : public Carte
{
public:
Personnage();
void chargerPersonnage(string nom);
virtual ~Personnage();
private:
//! Le texte d'accroche de la carte
string _accroche;
};
#endif // PERSONNAGE_H
personnage.cpp
#include "personnage.hpp"
Personnage::Personnage() : Carte("Sans titre")
{
}
void chargerPersonnage(string nom)
{
}
Personnage::~Personnage()
{
//dtor
}
main.cpp
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
#include "personnage.hpp"
using namespace sf;
using namespace std;
int main()
{
string test = "Hello Kitty";
cout << test << endl;
Personnage test2;
test2.chargerPersonnage(test);
return EXIT_SUCCESS;
}
error log
obj/Release/src/main.o||In function `main':|
main.cpp|| undefined reference to `Personnage::chargerPersonnage(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|
||=== Build finished: 1 errors, 0 warnings ===|
The problem is that you’ve defined
class Personnageas having a member functionchargerPersonnage, but in personnage.cpp you define a free functionchargerPersonnageinstead.You appear to know the correct syntax, as you did
Personnage‘s constructor and destructor correctly, but just to make it clear: Changevoid chargerPersonnage(string nom) { }tovoid Personnage::chargerPersonnage(string nom) { }in personnage.cpp.