#ifndef MIGRATINGUSER_H
#define MIGRATINGUSER_H
#include <iostream>
using namespace std;
class MigratingUser
{
public:
void populateUidAndGuid(string line);
private:
string uid;
string guid;
};
#endif
那是 header 定义,下面是 cpp 定义:
#include "MigratingUser.h"
using namespace std;
void MigratingUser::populateUidAndGuid(string line)
{
//get the uid and guid
}
这是我遇到的错误。我该如何解决这个问题:
undefined reference to MigratingUser::populateUidAndGuid(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
为了完整起见,我是这样使用这个类的:
MigratingUser muser;
muser.populateUidAndGuid(temp);
最佳答案
首先,我真的怀疑这是你遇到的错误,因为它的格式不正确。也许你有一些关于 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
的话题,但除非您有一些……有趣的……代码,否则编译器不太可能吐出您发布的内容。以后,精确地复制粘贴,因为专家可以从您认为不重要的细节中获得数量惊人的信息。如果您编辑不当,可能会严重误导 StackOverflow 用户,使他们更难为您提供帮助。
现在,您的问题最可能的原因是您没有意识到编译 步骤和链接 步骤之间的区别。 “ undefined reference ”错误是一个链接器错误。
这意味着您的 header 是正确的 - 您的代码可以编译,但最后您没有将所有目标文件正确地放在一起 - 因此,g+ -c MigratingUser.cpp
的输出未包含在 g++ -c main.cpp
的输出中(或任何您命名为“使用”代码的名称)。
您可以像这样一次性完成编译和链接(对于简单项目):
g++ -o my_executable MigratingUser.cpp main.cpp
这将同时编译和链接这两个文件。
关于c++ - 对现有类的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9106501/