main.cpp|| undefined reference to `MyClass::loadDatas(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|
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 Personnage
as having a member function chargerPersonnage
, but in personnage.cpp you define a free function chargerPersonnage
instead.
You appear to know the correct syntax, as you did Personnage
's constructor and destructor correctly, but just to make it clear: Change void chargerPersonnage(string nom) { }
to void Personnage::chargerPersonnage(string nom) { }
in personnage.cpp.
MyClass i;
MyClass.loadDatas(foo);
//^this syntax is wrong. Dot is used with "instance" of class!
Wrong syntax. I think you wanted to write:
i.loadDatas(foo);
However, if loadData
is a static
member function, then you've to write it:
MyClass::loadDatas(foo);
//^^ note the difference!
REPLY TO YOUR EDIT:
I don't think it's possible to point out the error in your code without seeing more code. Maybe, you've not defined the function loadDatas
. Make sure you've defined it. Also, check out the spelling, syntax and all.
Just a guess: if you do not use use namespace std;
in your header file (which you shouldn't because it can be evil), you need to define your method with std::string filename
.
I see two problems in your code:
loadDatas
lacks a return type- I think you mean
i.loadDatas(foo);
and notMyClass.loadDatas(foo);
精彩评论