Reputation: 11
i have an assignment where i need to read the words from different documents and store them in a vector of strings, i made this vector static so that each document would just add their words to the vector so that i can have one list of all the words. i made a document class and in the header i wrote :
class document {
public:
document(string filename);
static vector<string> words;
string name;
vector<int> frequency;
void getFrequency();
static void addWord(string wordd);
in the document.cpp file implemented the addWord method with the following :
static void document::addWord(string wordd){
vector<string>::iterator i = find(words.begin(), words.end(), wordd);
if (i == words.end()) {
words.push_back(wordd);
}
}
however this doesn't work and every time i try to build the code it gives me this error message
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/assignment1 mkdir -p build/Debug/GNU-MacOSX rm
-f build/Debug/GNU-MacOSX/main.o.d g++ -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp mkdir -p dist/Debug/GNU-MacOSX g++ -o dist/Debug/GNU-MacOSX/assignment1 build/Debug/GNU-MacOSX/main.o Undefined symbols for architecture x86_64: "document::words", referenced from:
document::getFrequency() in main.o
document::addWord(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in main.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status make[2]: *** [dist/Debug/GNU-MacOSX/assignment1] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
Upvotes: 1
Views: 1199
Reputation: 67743
The line
Undefined symbols for architecture x86_64: "document::words"
is telling you that your static data member was never actually created: you declared it in the header, but never told the compiler where to store the object.
This is sort of a non-problem in your case, but in a larger project with many .cpp
files including the same header, it's important the static object is only allocated once.
Add this line to document.cpp
:
vector<string> document::words;
It's exactly like a global variable declaration (since that's really what a static member is), except for the scoped name.
Upvotes: 0
Reputation: 153840
The immediate error which this error message is about is that you have declared document::words
but you haven't defined it. A definition of this lives outside the class definition, typically in the class's translation unit. You need to have definition which looks something like this:
static std::vector<std::string> document::words;
That said, please not that static data is pretty much like slightly better controlled global data. Especially with concurrent programs having anything which is shared for all object is a Bad Idea. If this thing is mutable it is an even worse idea (BTW, the also applies if the global data is referred to as "Singleton": just because something is allegedly a design pattern doesn't mean it is somehow blessed and the problems are gone away).
Upvotes: 2
Reputation: 16761
Remove keyword static
from function definition (from .cpp file).
When static
is used in function definition then the meaning of the word is that function is visible only in current compilation unit (similar to unnamed namespace). So, just remove it from .cpp.
Upvotes: 0