0

After searching for a solution to this for about a half an hour I have made no progress. The errors are as follows:

s\My Workspace\Project\main.cpp - Line 7 - undefined reference to 'Sally::Sally()'

s\My Workspace\Project\main.cpp - Line 9 - undefined reference to 'Sally::printCrap()'

main.cpp

#include <iostream>
using namespace std;
#include "Sally.h"

int main()
{
    Sally sallyObject;
    sallyObject.printCrap();
}

Sally.h

#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
        void printCrap();
};

#endif // SALLY_H

Sally.cpp

#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally(){
}

void Sally::printCrap(){
    cout << "Did someone say steak?" << endl;
}

Thank you in advance!

5
  • 2
    The linker can't find definitions to the mentioned functions. Is your .cpp file included in the project you're using? Commented Jan 24, 2016 at 22:33
  • 2
    It looks like Sally.cpp is not part of the project that your are building. Commented Jan 24, 2016 at 22:33
  • please show us your command line for compiling and linking Commented Jan 24, 2016 at 22:35
  • How are you building this..? Makefile..? directly using gcc..? Commented Jan 24, 2016 at 22:53
  • @Tas how can i tell? The files are all under the same project in code::blocks Commented Jan 24, 2016 at 23:15

2 Answers 2

2

I know this is a pretty old question but maybe it could help someone.

So, when adding any additional files (headers, source, etc.) follow this (if using Eclipse or similar IDE):

New file -> File... -> C/C++ header (source, etc.) -> next, next -> give it a name and make sure it's in the same path with your project, then check "Add file to active project", in build target(s): check all -> Finish.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Your linker doesn't find Sally.cpp. (Quick intro to linker)

To compile your code type:

g++ -o main main.cpp Sally.cpp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.