before I continue I'd like to tell you that I am new to programming (I've done a bit of python programming though but not that much).
I recently started learning C++ from YouTube videos and Sololearn.
I decided to start a project to train myself and I think that creating a python-like dictionary type is a good project (and maybe useful).
I created the Dictionary.h which contains the following code:
#ifndef DICTIONARY_H
#define DICTIONARY_H
class Dictionary { // Dictionary is a custom type that takes a key value(string required) and returns a value linked to it(int, string, bool are supported)
private:
std::list<std::string> keyList;
std::list<char> containerList; // I == int | S == string | B == bool
std::list<int> indexingList; // contains the index of each value
std::list<int> intVList;
std::list<std::string> strVList;
std::list<bool> boolVList;
// private function for getting the last index from a value list
int getLastIndex(char lType);
int getKeyIndex(std::string key);
char getContainerFromIndex(int indexNum);
int getIndexFromKeyIndex(int indexNum);
int _getValue(std::string key);
public:
// constructor
Dictionary();
// add element to the dictionary
void _add(std::string key, int value);
void _add(std::string key, std::string value);
void _add(std::string key, bool value);
// get element from the dictionary using the key value
int _get(std::string key);
// check if a key exists in the dictionary
bool keyInDict(std::string key);
};
#endif
and then I created the Dictionary.cpp with contains the following code (NOTE: the following is just a part of the code, have created all the mention functions from the header in the .cpp):
#include "Dictionary.h"
#include <iostream>
#include <string>
#include <list>
Dictionary::Dictionary() {
std::cout << "Hello World";
}
void Dictionary::_add(std::string key, int value) {
if (keyInDict(key)) {
throw 9403;
}
char containerChar = 'I';
keyList.push_back(key);
containerList.push_back(containerChar);
int atIndex = getLastIndex(containerChar);
indexingList.push_back(atIndex+1);
intVList.push_back(value);
}
int Dictionary::_getInt(std::string key) {
int keyI = getKeyIndex(key);
int realIndex = getIndexFromKeyIndex(keyI);
if (getContainerFromIndex(keyI) != 'I') {throw 9400;}
list<int>::iterator i = intVList.begin();
advance(i, realIndex);
return *i;
}
so I've written some code and now I want to test if it works so far (then I'll move forward to adding more functions.), so I created a run.cpp with the following content:
#include <iostream>
#include <string>
#include <list>
#include "Dictionary.h"
int main() {
Dictionary dict;
dict._add("test1", 1);
dict._add("test2", 5);
int got = dict._get("test1");
std::cout << got;
return 0;
}
but the compiler (CygWin g++ on windows 10 64 bit) throws this error:
$ g ++ run.cpp
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0x1e): undefined reference to `Dictionary :: Dictionary () '
/tmp/cc7qwomO.o:run.cpp:(.text+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: Dictionary () '
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0x58): undefined reference to `Dictionary :: _ add (std :: string, int) '
/tmp/cc7qwomO.o:run.cpp:(.text+0x58): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: _ add (std :: string, int) '
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0xab): undefined reference to `Dictionary :: _ add (std :: string, int) '
/tmp/cc7qwomO.o:run.cpp:(.text+0xab): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: _ add (std :: string, int) '
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc7qwomO.o:run.cpp :(. text + 0xf9): undefined reference to `Dictionary :: _ get (std :: string) '
/tmp/cc7qwomO.o:run.cpp:(.text+0xf9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Dictionary :: _ get (std :: string) '
collect2: error: ld returned output mode 1
#cause of my computer's language I had to pass the output through google translator so it may differ a bit from what you normally see
by the way, all files (Dictionary.h, Dictionary.cpp, run.cpp) are on the same directory
QUESTIONS
- why do I get this error?
- how can I fix it?
- is there something I may not understand about including headers? do I need to link them with the cpp somehow?
ADDITIONAL INFO
I tried to edit the Dictionary.h and add the constructor body
(an empty body like {}) and the first error
about the Dictionary::Dictionary() did not appear(the rest appeared though).
my guess is: it doesn't read the Dictionary.cpp, cause if I edit the main to just std::cout << 5;
it works as expected (and in the error it says "undefined reference to ...")