gpt4 book ai didi

c++ - main.cpp ||对 `MyClass::loadDatas(std::basic_string, std::allocator>)' 的 undefined reference |

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:24 24 4
gpt4 key购买 nike

我在参数中使用 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:完整的源代码

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;
}

错误日志

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,但是在 personnage.cpp 中您定义了一个 < strong>免费功能 chargerPersonnage 代替。

您似乎知道正确的语法,就像您正确地执行了 Personnage 的构造函数和析构函数一样,但只是为了说明:更改 void chargerPersonnage(string nom) { } 到 personnage.cpp 中的 void Personnage::chargerPersonnage(string nom) { }

关于c++ - main.cpp ||对 `MyClass::loadDatas(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)' 的 undefined reference |,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5523562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com