Question

I'm having this weird problem - i have functions defined and declared, no typos, no external libraries, no namespace failing, no templates, no anything other threads mention - but i still get 'undefined symbols' for the function calls.

i have the following code:

in one .cpp file:

string print(const FanBookPost& post) {
    std::stringstream ss;
    // notice! post.getOwner.getId! owner needs to be fan
    ss << post.getOwner().getId() << ": " << post.getContent() << "(" << post.getNumLikes()
                    << " likes)";
    return ss.str();
}

that file includes FanBookPost.h.

then i have in FanBookPost.h:

class FanBookPost {

    private:

        Fan owner;
        std::string content;
        int numOfLikes;
        int seqNum;

    public:

        // constructors


        int getNumLikes();
        std::string getContent();
        Fan getOwner();

        int getNumLikes() const;
        std::string getContent() const;
        Fan getOwner() const;

    };

as you can see, i have the const and regular versions just to be prepared. in the first .cpp file it the "post" function gets is const.

i have those functions implemented here, in FanBookPost.cpp:

class FanBookPost {

private:

    Fan owner;
    std::string content;
    int numOfLikes;
    int seqNum;

public:

    //constructors

    int getNumLikes() {
         // code
    }

    std::string getContent() {
        // code
    }

    Fan getOwner() {
        // code
    }


    int getNumLikes() const {
        // code
    }

    std::string getContent() const {
        // code

    }

    Fan getOwner() const {
        // code
    }

};

i tried to google answers and search stackoverflow threads, but as i said, nothing of the obvious problems can be found. please help me resolve this 'undefined symbols' matter as it is driving me crazy already.

Was it helpful?

Solution

i have those functions implemented here, in FanBookPost.cpp

No you don't! You've redefined the class, rather than just defining the functions. That breaks the One Definition Rule, so all manner of things could go wrong. In particular, the functions are inline, so they won't be available to other source files; hence your errors.

The source file should look more like

#include "FanBookPost.h" // include the class definition

// Define the member functions
int FanBookPost::getNumLikes() {
    // code
}

// and so on

Alternatively, you could define the functions within the class definition in the header, or after the class definition with an inline specifier; this might be appropriate if they are very small, since it gives the compiler better optimisation opportunities. But in either case, you must only define the class once, in the header.

OTHER TIPS

As you are using a const reference to FanBookPost, you'll need getOwner() to be const:

Fan getOwner() const;

(which it should be anyway, if it's a simple getter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top