对现有类的未定义引用

时间:2012-02-02 02:48:25

标签: c++ linker-errors undefined-symbol

#ifndef MIGRATINGUSER_H
#define MIGRATINGUSER_H
#include <iostream>
using namespace std;

class MigratingUser
{
        public:
        void populateUidAndGuid(string line);
        private:
        string uid;
        string guid; 
};
#endif

s头定义及以下是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);

1 个答案:

答案 0 :(得分:1)

首先,我真的怀疑这是你得到的错误,因为它的格式不正确。也许你得到了一些关于std::basic_string<char, std::char_traits<char>, std::allocator<char> >的内容,但是除非你有一些......有趣的代码,否则编译器不太可能吐出你发布的内容。将来,复制并粘贴完全,因为专家可以从您认为不重要的详细信息中获得惊人的大量信息。如果你编辑错误,你可能会严重误导StackOverflow用户,使他们更难以帮助你。

现在,问题的最可能原因是您不知道编译步骤和链接步骤之间的区别。 “未定义的引用”错误是链接器错误。

这意味着你的标题是正确的 - 你的代码编译,但你没有正确地将所有目标文件放在一起 - 所以g+ -c MigratingUser.cpp的输出不包含在{的输出中{1}}(或者您命名为“使用”代码的任何内容)。

您可以一次性完成编译和链接(对于简单项目),如下所示:

g++ -c main.cpp

这将编译和链接两个文件在一起