C++ main.cpp | | |对` MyClass::loadDatas'的未定义引用(std::basic_string<;char,std::char_traits<;char>;,std::allocator<;char>;)&x27|
在参数中使用C++ main.cpp | | |对` MyClass::loadDatas'的未定义引用(std::basic_string<;char,std::char_traits<;char>;,std::allocator<;char>;)&x27|,c++,string,C++,String,在参数中使用std::string时遇到问题: MyClass有这样一个方法 public: void loadDatas(string filename); 在mymain.cpp中,我有以下简单代码: #include <iostream> #include <string> #include "myclass.hpp"; using namespace std; int main() { string foo = "test.txt";
std::string
时遇到问题:
MyClass
有这样一个方法
public:
void loadDatas(string filename);
在mymain.cpp
中,我有以下简单代码:
#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;
}
personnage.cpp
#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
#include "personnage.hpp"
Personnage::Personnage() : Carte("Sans titre")
{
}
void chargerPersonnage(string nom)
{
}
Personnage::~Personnage()
{
//dtor
}
#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;
}
main.cpp
#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
#include "personnage.hpp"
Personnage::Personnage() : Carte("Sans titre")
{
}
void chargerPersonnage(string nom)
{
}
Personnage::~Personnage()
{
//dtor
}
#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;
}
#包括
#包括
#包括
#包括“personnage.hpp”
使用名称空间sf;
使用名称空间std;
int main()
{
字符串测试=“Hello Kitty”;
库特
语法错误。我想你想写:
i.loadDatas(foo);
但是,如果loadData
是一个static
成员函数,那么您必须编写它:
MyClass::loadDatas(foo);
//^^ note the difference!
回复您的编辑:
我认为在没有看到更多代码的情况下指出代码中的错误是不可能的。可能是因为您没有定义函数loadDatas
。请确保您已经定义了它。另外,请检查拼写、语法和所有内容。我发现您的代码中存在两个问题:
loaddata
缺少返回类型
- 我想你是指
I.loadDatas(foo);
而不是MyClass.loadDatas(foo);
只是一个猜测:如果您不在头文件中使用名称空间std;
(您不应该这样做,因为它可能是邪恶的),您需要使用std::string filename
来定义您的方法。问题是您将类Personnage
定义为具有成员函数chargerPersonnage
,但在Personnage.cpp中定义了自由函数chargerPersonnage
您似乎知道正确的语法,就像您正确地理解了Personnage
的构造函数和析构函数一样,但只是想说明一下:将void chargerPersonnage(string nom){}
更改为void Personnage::chargerPersonnage(string nom){}
在personnage.cpp.中,如果看不到myclass.hpp
和myclass.cpp
,很难说。此外,我们还需要看看您是如何调用编译器/链接器的。@Oli Charlesworth:myclass.cpp中的函数是空的(以避免其他可能的错误),和myclass.hpp只声明构造函数、析构函数和此方法。包含的库有:iostream和string(有libxml/tree.h,但在源代码中没有使用)。编译器由code::Blocks调用(它只使用g++和-lxml2链接(我尝试使用-llibstdc,但我没有更改任何内容))。请将这两个文件的代码添加到您的问题中。如果看不到导致问题的确切代码,就很难得出任何结论。@Oli Charlesworth我添加了完整的源代码(它只缺少一个类(Carte),但暂时不做任何事情。如果需要,我可以删除继承).是的,写入和返回类型为void(参见编辑注释)C++ 1;不是解决这个问题的方法,而是一般的好建议。只是一个提示:我在OP之前把答案写在了OP的完整源上。哦,我知道,我看到了时间戳。我只是想特别注意你的答案,即使它不是被接受的答案。————非常感谢!5年没有C++编程的人已经有了B。广告对我的影响…我觉得自己很愚蠢…再次感谢你,伊尔贾恩。