-4

I'm getting the below error when try to call my function add() from the main.

C:\studies\NMB1231_Question5\main.cpp:25:23: error: no matching function for call to 'Dictionary<std::vector<int>, std::vector<std::__cxx11::basic_string<char> > >::add(int&, std::__cxx11::string&)'

.h file

#include <vector>
#include <string>
#include <iostream>
using namespace std;

template <class A,class B>
class Dictionary
{
    public:
        Dictionary();
        void add(A key,  const B &value);
        B find (A key) const;
        void display();
    
    private:
        vector<A> keys;
        vector<B> values;
};

.cpp

#include "Dictionary.h"
#include <vector>
#include <iostream>

using namespace std;

template <class A,class B>
Dictionary<A,B>::Dictionary()
{
    //nothing to do, vector member variables are empty on declaration
};

template <class A,class B>
void Dictionary<A,B>::add(A key,  const B &value)
{
    keys.push_back(key);
    values.push_back(value);
}
    
template <class A,class B>
B Dictionary<A,B>::find (A key) const
{
    B value = " ";
    for (unsigned int i = 0; i < keys.size(); i++)
        if (key == keys[i])
            value = values[i];
    if (value == " ")
        return "no such key can be found";
    else return value;
}

template <class A,class B>
void Dictionary<A,B>::display()
{
    for (unsigned int i = 0; i < keys.size(); i++)
        cout << keys[i] << ' ' << values[i] << endl;
    return;
}

main

#include <iostream>
#include <cstdlib>
#include "Dictionary.h"
#include <vector>


using namespace std;

int main()
{
    Dictionary <vector<int>, vector<string > > part1;
    string part;
    int key;
    //add 4 values to the parts dictionary

    for (int i = 0; i <= 3; i++)
    {
        cout << "Please enter a part name and a key to add to the parts dictionary." << endl;
        cout << "Part name: ";
        getline(cin, part);
        cout << "Key for part name: ";
        cin >> key;
        part1.add(key,part);
        cin.get();
    }

    cout << endl;
    part1.display();
    cout << endl;

    //find the part for a key
    cout << "For which key do you want to find the part? ";
    cin >> key;
    cout << "The part for key " << key << " is ";
    cout << part1.find(key) << endl;
    // cout << parts.find(100002);
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

In main, you are declaring your part1 variable as Dictionary<vector<int>, vector<string>>. So its A template parameter is vector<int> and its B template parameter is vector<string>.

Dictionary::add() takes instances of A and B as parameters. So, in this case, add() is expecting a vector<int> in its 1st parameter and a vector<string> in its 2nd parameter. But main is passing an int and a string instead. That is why you are getting the error.

You need to declare part1 as Dictionary<int, string> instead.


That being said, there are other problems with your code.

  • Dictionary::add() does not look for an existing key before pushing the input into the vectors. If the key already exists, you should replace the existing entry with the new value, otherwise push the new key and value. By definition, a dictionary should not contain duplicate keys.

  • Dictionary::find() does not compile when B is not std::string or const char*. Also, if it can't find the specified key it should either return a default-constructed B, or throw an exception.

  • You really should be using std::(unordered_)map instead of std::vector. Let it handle the key/value management for you.

  • You can't implement a template class in separate .h/.cpp files (not the way you are trying to do, anyway).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Hi Remy, thank you for you assistance, this was very insightful. – Glad malete Aug 30 '21 at 08:19
  • If I change my declaration of part1 to Dictionary, I now get more errors because my functions are expecting vectors not . I also need to keep using vector because this what is required in the assignment, I'm not allowed to change the data type. Error - undefined reference to `Dictionary, std::allocator > >::Dictionary()' – Glad malete Aug 30 '21 at 08:28
  • @Gladmalete `part1` needs to be `Dictionary`, that is the way you coded `main()` to expect, and it just makes sense for a dictionary anyway. As for the `undefined reference` error, that is because [you can't implement a template class in separate `h`/`cpp` files](https://stackoverflow.com/questions/495021/) (not the way you are trying to do, anyway) . – Remy Lebeau Aug 30 '21 at 14:22
  • moving the function implementation to the header file seems to resolve the error. thank you. – Glad malete Aug 30 '21 at 17:49