Working on a project program for my programming class and I am having a hell of a time with it. I am pretty sure the solution to this problem is simple and staring me in the face, so please feel free to laugh at me after the error is pointed out :).
After trying to compile (using either Codeblocks or Bloodshed) the code shown below, I get an "driver_dictionary.cpp: undefined reference to `Dictionary::Dictionary()' error.
Here are the files that I am using:
driver_dictionary.cpp (I CAN NOT modify this file according to project rules):
void test2()
{ Dictionary two; <-- undefined error on this line
string word ;
cout << "input words to add to the dictionary;enter ??? to stop ";
cin >> word ;
while ( word != "???" )
{
two.add (word);
cin >> word;
}
cout << "input word to search for in dictionary;enter ??? to stop ";
cin >> word ;
while ( word != "???" )
{
cout << two.contains (word) << endl;
cin >> word;
}
}
int main(int argc, char** argv)
{
test1();
test2();
return 0;
}
dictionary.h (I CAN NOT modify this file according to project rules):
dictionary.cpp: (This file will be modified to perform the required actions on a linked list (add and contain functions) AFTER I figure out why I am getting the undefined error:)
string toLowerCase (string word)
{
string result = word;
for (int i = 0; i < word.length(); ++i)
if (word[i] >= 'A' && word[i] <= 'Z')
result[i] = result[i] + 'a' - 'A';
return result;
}
// modify this function to add a word to the dictionary in alpha order
void Dictionary::add (std::string word)
{
cout << word;
}
// check to see is the word already exists before adding the word
bool Dictionary::contains (std::string word) const
{
int k = 1;
cout << k;
return k >= 0;
}
Any assistance would be greatly appreciated and thank you very much for your time!
In dictionary.h a constructor is declared, however it has no definition. Add Dictionary::Dictionary() {/*you might want to do some initializing*/} in whatever cpp file you can.
Can you please elaborate on where to add Dictionary::Dictionary()?
From my understanding, when the constructor was called in the .h file, a STRUCT was created using the information in the "private" section. Is this correct or am I way off base?
Or do I need to add something like this to the dictionary.cpp file to set up the linked list?
Constructor is a function and a function needs an implementation. Put it where you put all of your implementations - in dictionary.cpp
From my understanding, when the constructor was called in the .h file, a STRUCT was created using the information in the "private" section. Is this correct or am I way off base?
I don't get what you're asking..
Or do I need to add something like this to the dictionary.cpp file to set up the linked list?
In a constructor you don't declare members. You (may) give them initial values. For example it is good to initialize your pointers to 0.
Thanks Hamsterman for the help.
Adding the constructor to the implementation file work great to get me started. However I am having trouble initializing the head pointer to NULL. Added a new post with the current code.