c ++“未定义的引用“Foo :: Foo(std :: string)”& [英] c++ " undefined reference to 'Foo::Foo(std::string)' "

查看:193
本文介绍了c ++“未定义的引用“Foo :: Foo(std :: string)”&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太熟悉c ++和如何实例化对象的工作,所以这可能是一个很简单的事情来解决。当我编译g ++我得到错误未定义引用'Foo :: Foo(std :: string)'。我想创建一个类Foo的实例,在其构造函数中有一个字符串参数。以下是代码:

I'm not too familiar with c++ and how instantiating objects work, so this is probably a very simple thing to solve. When I compile with g++ I get the error " undefined reference to 'Foo::Foo(std::string)' ". I want to create an instance of the class Foo that has a string parameter in its constructor. Here is the code:

#include <string>
using namespace std;

class Foo
{
    public:
        Foo(string s);

    private:
        string id;
};



Foo.cpp



Foo.cpp

#include <string>
#include "Foo.h"
using namespace std;

Foo::Foo(string s)
{
    id = s;
}



main.cpp



main.cpp

#include <string>
#include "Foo.h"
using namespace std;

int main()
{
    Foo foo("bar");

    return 0;
}


推荐答案

Foo.cpp在你的编译行。它应该看起来像这样:

You're probably not including Foo.cpp in your compile line. It should look something like this:

g++ main.cpp Foo.cpp -o testFoo

这篇关于c ++“未定义的引用“Foo :: Foo(std :: string)”&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆