c++ - How to properly include Header and Implementation Files? -
i novice programmer in c++, , getting compiling error
undefined symbols architecture x86_64
supposedly originates how header files , implementation files included/coded.
below code generates compiling error receiving
main
//main.cpp #include <iostream> #include <string> #include "animal.hpp" using namespace std; int main(){ animal mypet; mypet.shout(); return 0; }
header
//animal.hpp #ifndef h_animal #define h_animal using namespace std; #include <string> class animal{ public: animal(); void shout(); private: string roar; }; #endif
implementation
//animal.cpp #include "animal.hpp" #include <string> animal::animal(){ roar = "..."; } void animal::shout(){ roar = "roar"; cout << roar; }
this code generates compiling issue. how issue resolved?
thanks time
edit
undefined symbols architecture x86_64: "animal::shout()", referenced from: _main in test-5f7f84.o "animal::animal()", referenced from: _main in test-5f7f84.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation)
maybe might want see alternative set of 3 files, things little more "sorted", know, things put @ places "really" belong to.
so here's "new" header file ..
//animal.hpp #ifndef h_animal #define h_animal #include <string> // suffices // interface. class animal { std::string roar; // private public: animal(); void shout(); }; #endif
then source file ..
//animal.cpp #include "animal.hpp" #include <iostream> // suffices // constructor. animal::animal() : roar("...") // data member initializer {} // member function. void animal::shout() { roar = "roar"; std::cout << roar; }
and main program ..
//main.cpp #include "animal.hpp" int main(){ animal thepet; thepet.shout(); // outputs: `roar' }
plus little gnu makefile ..
all: default run default: animal.cpp main.cpp g++ -o main.exe animal.cpp main.cpp run: ./main.exe clean: $(rm) *.o *.exe
kick-off things typing "make" in cmd-line. did it? -- regards, m.
Comments
Post a Comment