C ++ "undefined reference to 'Foo :: Foo (std :: string)'"

I am not very familiar with C ++ and how instance objects work, so this is probably a very simple task. When I compile with g ++, I get the error message "undefined reference to" Foo :: Foo (std :: string) ". I want to create an instance of the Foo class 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;
}
+3
source share
2 answers

You probably don't include Foo.cpp in your compilation line. It should look something like this:

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

, , :

  • const. const, , .

  • ++ , , id . id-, . ( "id (s)" ) .

    Foo:: Foo (const string & s): id (s)
    {
    }

+2

Source: https://habr.com/ru/post/1763554/


All Articles