C++ multiple definition first defined here -
i'm typing out example program absolute c++ , keeps giving me error:
/tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::hashtable()': hashtableimp.cpp:(.text+0x0): multiple definition of `hashtablesavitch::hashtable::hashtable()' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x0): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::hashtable()': hashtableimp.cpp:(.text+0x0): multiple definition of `hashtablesavitch::hashtable::hashtable()' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x0): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::~hashtable()': hashtableimp.cpp:(.text+0x16): multiple definition of `hashtablesavitch::hashtable::~hashtable()' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x16): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::~hashtable()': hashtableimp.cpp:(.text+0x16): multiple definition of `hashtablesavitch::hashtable::~hashtable()' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x16): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::~hashtable()': hashtableimp.cpp:(.text+0x44): multiple definition of `hashtablesavitch::hashtable::~hashtable()' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x44): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::computehash(std::string)': hashtableimp.cpp:(.text+0x6a): multiple definition of `hashtablesavitch::hashtable::computehash(std::string)' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x6a): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::containsstring(std::string) const': hashtableimp.cpp:(.text+0x7a): multiple definition of `hashtablesavitch::hashtable::containsstring(std::string) const' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x7a): first defined here /tmp/ccjfp4dm.o: in function `hashtablesavitch::hashtable::put(std::string)': hashtableimp.cpp:(.text+0x8e): multiple definition of `hashtablesavitch::hashtable::put(std::string)' /tmp/cchuzlfl.o:hashtable.cpp:(.text+0x8e): first defined here collect2: error: ld returned 1 exit status makefile:2: recipe target 'hashtable' failed make: *** [hashtable] error 1
i've reduced program down pure basics still can't figure out what's going on. can help?
makefile:
hashtable: listtools.h listtools.cpp hashtable.h hashtable.cpp hashtableimp.cpp g++ listtools.h listtools.cpp hashtable.h hashtable.cpp hashtableimp.cpp -o hashtable
hashtable.h
#ifndef hashtable_h #define hashtable_h #include <string> #include "listtools.h" using linkedlistsavitch::node; using std::string; namespace hashtablesavitch { const int size = 10; class hashtable { public: hashtable(); virtual ~hashtable(); bool containsstring(string target) const; void put(string s); private: node<string> *hasharray[size]; static int computehash(string s); }; } #endif
hashtable.cpp
#include <string> #include "listtools.h" #include "hashtable.h" using linkedlistsavitch::node; using linkedlistsavitch::search; using linkedlistsavitch::headinsert; using std::string; namespace hashtablesavitch { hashtable::hashtable() { } hashtable::~hashtable() { } int hashtable::computehash(string s) { return 0; } bool hashtable::containsstring(string target) const { return true; } void hashtable::put(string s) { } } // hashtablesavitch
hashtableimp.cpp
#include <string> #include <iostream> #include "hashtable.h" #include "listtools.cpp" #include "hashtable.cpp" using std::string; using std::cout; using std::endl; using hashtablesavitch::hashtable; int main() { hashtable h; cout << "adding cat, dog, turtle, , bird" << endl; h.put("cat"); h.put("dog"); h.put("turtle"); h.put("bird"); cout << "contains cat? " << h.containsstring("cat") << endl; cout << "contains dog? " << h.containsstring("dog") << endl; cout << "contains turtle? " << h.containsstring("turtle") << endl; cout << "contains bird? " << h.containsstring("bird") << endl; cout << "contains cow? " << h.containsstring("cow") << endl; cout << "contains fish? " << h.containsstring("fish") << endl; return 0; }
listtools.h
#ifndef listtools_h #define listtools_h namespace linkedlistsavitch { template<class t> class node { public: node(const t& thedata, node<t>* thelink) : data(thedata), link(thelink) {} node<t>* getlink() const {return link;} const t getdata() const {return data;} void setdata(const t& thedata) {data = thedata;} void setlink(node<t>* pointer) {link = pointer;} private: t data; node<t> *link; }; template<class t> void headinsert(node<t>*& head, const t& thedata); template<class t> void insert(node<t>* afterme, const t& thedata); template<class t> void deletenode(node<t>* before); template<class t> void deletefirstnode(node<t>*& head); template<class t> node<t>* search(node<t>* head, const t& target); } // linkedlistsavitch #endif
you should not #include
cpp's main hashtableimp.cpp
, if want list them on g++
command line.
actually have 2 options here:
- include all code 1 file (or split code several files ,
#include
1 another, 1 file) — in such case need list main file ing++
command line - really split code across several files — list these files on
g++
command line, not need#include
files definitions (usually cpp) other files.
the reason one definition rule: function (with exceptions) should defined in 1 place in whole program. if #include
1 cpps anothers, , compile of them, repeat definitions.
usually former approach used small programs, , latter lager projects. more commonly, in latter approach compile each file 1 one, , link them together.
also note not need list .h
files in command line, should not compiled themself, #include
d other .cpp
files compiler knows declaration of functions/objects/etc used there.
Comments
Post a Comment