[英]undefined reference to `librarymanager::getBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
[英]main.cpp|| undefined reference to `MyClass::loadDatas(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|
我在參數中使用std::string
遇到問題:
MyClass
有這樣的方法
public:
void loadDatas(string filename);
在我的main.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;
}
看來libstdc++
鏈接良好(因為我可以使用cout
打印文本),但是在參數中傳遞string
引發錯誤,我也不明白為什么。
有人可以幫我嗎?
編輯:我犯了一個錯誤,實際上是i.loadDatas(foo);
(我糾正了)。 我的源代碼不是英語的,所以我嘗試用英語為您制作一個簡單的版本。 我真的叫實例方法,而不是靜態方法。
編輯2:完整的源代碼
人格
#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;
}
錯誤日志
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 ===|
問題在於您已經將class Personnage
定義為具有成員函數 chargerPersonnage
,但是在chargerPersonnage
,您定義了自由函數 chargerPersonnage
。
您似乎正確地使用了Personnage
的構造函數和析構函數,但是知道了正確的語法,只是為了清楚void chargerPersonnage(string nom) { }
:在void chargerPersonnage(string nom) { }
更改為void Personnage::chargerPersonnage(string nom) { }
。 cpp。
MyClass i;
MyClass.loadDatas(foo);
//^this syntax is wrong. Dot is used with "instance" of class!
語法錯誤。 我想你想寫:
i.loadDatas(foo);
但是,如果loadData
是static
成員函數,則必須編寫它:
MyClass::loadDatas(foo);
//^^ note the difference!
回復您的編輯:
我認為不可能在看不到更多代碼的情況下指出代碼中的錯誤。 也許您尚未定義函數loadDatas
。 確保已定義。 另外,請檢查拼寫,語法和所有內容。
只是一個猜測:如果不使用,請use namespace std;
在頭文件中(您不應這樣做,因為它可能是邪惡的),您需要使用std::string filename
定義您的方法。
我在您的代碼中看到兩個問題:
loadDatas
缺少返回類型 i.loadDatas(foo);
而不是MyClass.loadDatas(foo);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.