vendredi 22 septembre 2017

C2955 and C2244 from templates

C2955:'KVList': use of class template requires template argument list

C2244: 'KVList::add': unable to match function definition to an existing declaration

I've spent several hours looking at many threads, but I just can't seem to find the answer for my situation. I keep receiving C2955 and C2244 error for unabling to match function definition... where i dont see a difference. The purpose of my program is to simply request the user for an item name and price and then call the function add an item and price to their arrays.

KVList.h

template <typename K, typename V, int N>
class KVList
{
 private:
 K keyArray[N];
 V valueArray[N]; 
 size_t num;
 public: 
 KVList();
 KVList& add(const K&, const V&);
} 

KVList.cpp

 #include "KVList.h" 
 #include <iostream>

 template<typename K, typename V, int N> 
 KVList<K, V, N>::KVList() 
 {
   keyArray = nullptr; 
   valueArray = nullptr; 
   num = 0; 
 }

 template<typename K, typename V, int N> 
 KVList& KVList<K, V, N>::add(const K& key, const V& val) 
 { 
    keyArray[num] = key; 
    valArray[num] = val; 
    num++;
    return *this;
 } 

w4.cpp

 #include <iostream>
 #include <iomanip>
 #include <string>
 #include "KVList.h"

 int main(int argc, char** argv) {
 if (argc != 1) {
     std::cerr << argv[0] << ": too many arguments\n";
     return 1;
 }

 int width;
 bool keepreading;
 std::cout << std::fixed << std::setprecision(2);

 std::cout << "\nInventory\n=========\n";
 KVList <std::string, double, 5> inventory;
 std::string str;
 double price;

 keepreading = true;
 do {
      std::cout << "Product : ";
      getline(std::cin, str);
      if (str.compare("quit") == 0) {
          keepreading = false;
      } else {
          std::cout << "Price : ";
          std::cin >> price;
          std::cin.ignore();
          inventory.add(str, price);
      }
 } while(keepreading);
 display("\nPrice List\n----------\n", inventory, 13);

 std::cout << "\nCorrections\n-----------\n";
 keepreading = true;
 do {
      std::cout << "Product : ";
      getline(std::cin, str);
      if (str.compare("quit") == 0) {
          keepreading = false;
      } else {
          int i = inventory.find(str);
          if (i != -1) {
              std::cout << "Price : ";
              std::cin >> price;
              std::cin.ignore();
              inventory.replace(i, str, price);
          }
      }
 } while(keepreading);

Aucun commentaire:

Enregistrer un commentaire