Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

Foo.h

#include <string>
using namespace std;

class Foo
{
    public:
        Foo(string s);

    private:
        string id;
};

Foo.cpp

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

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

main.cpp

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

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

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
399 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...