1
votes

OK so I am going through a c++ tutorial on youtube as I am a relatively new programmer. I am trying to learn how to use header files and such when creating classes. In this tutorial we learn how to use the arrow member selection operator.

https://www.youtube.com/watch?v=2RP4f9beidc&list=PLAE85DE8440AA6B83&index=42

I am not using codeblocks which is the IDE he is using. I am writing my code on gedit and compiling and running it using Cygwin.

Any help would be greatly appreciated!

Here is the simple code:

Main.cpp

#include <iostream>
#include "Sally.h"

using namespace std;



int main(){


     Sally sallyObject;
     Sally *sallyPointer = &sallyObject;



     sallyObject.printCrap();
     sallyPointer->printCrap();

}

Sally.cpp

#include "Sally.h"
#include <iostream>

using namespace std;



Sally::Sally()

{

}



void Sally::printCrap(){

    cout << "did someone say steak?" << endl;

}

Sally.h

#ifndef SALLY_H

#define SALLY_H

#include <iostream>



class Sally

{

    public:

        Sally();

        void printCrap();

    protected:

    private:

};



#endif // SALLY_H

Error code:

/tmp/ccfOEJJF.o:Main.cpp:(.text+0x15): undefined reference to Sally::Sally()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbolSally::Sally()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x29): undefined reference to Sally::printCrap()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x29): relocation truncated to fit: R_X86_64_PC32 against undefined symbolSally::printCrap()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x35): undefined reference to Sally::printCrap()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbolSally::printCrap()' collect2: error: ld returned 1 exit status

2
It seems that when you created the project you did not link the object file with function definitions of class Sally.Vlad from Moscow

2 Answers

3
votes

Your command that you enter into cygwin for this must be

g++ Sally.cpp Main.cpp

You don't include the header files on the command line. As a piece of advice, lookup how to use makefiles.

0
votes

in testsuite/Makefile I changed the rules for prog to:

prog.o: prog.c
    $(CC) -c -o $@ $<

prog.exe: prog.o
    $(CC) -o $@ $<

This moved things along - I'm not sure if it's correct - but it continued to give issues in protos.h as it there was already system header definitions for Elf_[ESP]hdr types... I had to change them to pelf_[ESP]hdr for all the prototypes, definitions, and references in:

protos.h
chrpath.c
elf.c
killrpath.c