0

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

  1. why do I get this error?
  2. how can I fix it?
  3. 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 ...")

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
PTorPro
  • 63
  • 8
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 0x5453 Apr 01 '21 at 21:16
  • In any multi-file build you're eventually going to need to buckle down and learn either `Makefile` or CMake-based builds, if not use an IDE like Visual Studio or XCode that manages builds for you. Doing this by hand is nothing but trouble. – tadman Apr 01 '21 at 21:18

1 Answers1

1

The main file and the .cpp file must be linked before executing the main file. One solution is to create a makefile and run that. or Try This

This line creates the .o files of the cpp files

g++ -c main.cpp Dictionary.cpp

This code link both the .o files together

g++ main.o Dictionary.o
rupinderg00
  • 189
  • 8
  • 1
    I wouldn't say that the `.cpp` file needs to be "executed before the main file". But the two do need to be *linked* together. – 0x5453 Apr 01 '21 at 21:15
  • by executed you mean i need to compile them first? and then have the run.cpp compiled and executed? – PTorPro Apr 01 '21 at 21:17
  • @PTorPro Both of the file must be linked first in order to use the functions implemented in other files. First line in my code create the .o files and the second line link both – rupinderg00 Apr 02 '21 at 14:33