Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting error expression must have class type in c++

I am getting the following IntelliSense error:

Expression must have class type f:\C++\prj\map1\map1\testMap1.cpp 11

Which is referring to the following line in my code (shown in full below):

theMap.insert(1, "one");

I cannot figure out what the issue is. It does not seem to be related to the declaration of theMap, but every time I try to call a method on theMap I get the error. Here is my code:

map1.h

#ifndef MAP_H
    #define MAP_H
    #include <list>
    #include <utility>
    using namespace std;

//pair class definition
template<typename F, typename S>
class Pair
{
public:
Pair(const F& a, const S& b);
F get_first() const;
S get_second() const;
private:
F first;
S second;
};

template<typename F, typename S>
inline Pair<F, S>::Pair(const F& a, const S& b):first(a),second(b){}

template<typename F, typename S>
inline F Pair<F, S>::get_first() const
{
    return first;
}

template<typename F, typename S>
inline S Pair<F, S>::get_second() const
{
    return second;
}

template<typename K, typename V>
class map
{
public:
    map();
    void insert(const K& key, const V& value);
    bool contain_key(const K& key);
    V value_of(const K& key);
    void remove_key(const K& key);
    void print();

private:
    list<Pair<K, V>> theList;
};

template<typename K, typename V>
inline map<K, V>::map():{}

template<typename K, typename V>
inline void map<K, V>::insert(const K& key, const V& value)
{
    bool notThere = true;
    if(contain_key(key))
    {
        notThere = false;
    }
    if(notThere)
    {
    theList.push_back<pair<key, value>>
    }
}

template<typename K, typename V>
inline bool map<K, V>::contain_key(const K& key)
{
    iterator iter = theList.begin();
    K temp;
    for(int x=0 : x< theList.size() ; x++)
    {
        temp = iter->first;
        if(temp == key)
        {
            return true;
        }
        iter.advance();
    }

    return false;
}

template<typename K, typename V>
inline V map<K, V>::value_of(const K& key)
{
    iterator iter = theList.begin();
    K temp;
    for(int x=0; x < theList.size() ; x++)
    {
        temp = iter->first;
            if(temp == key)
            {
                return iter->second;
            }
    }
    cout << “we don’t have this key " << key << " in the map” "\n";
    return 0;
}

template<typename K, typename V>
inline void map<K, V>::remove_key(const K& key)
{
    iterator iter = theList.begin();
    K temp;
    for(int x=0; x < theList.size() ; x++)
    {
        temp = iter->first;
        if(temp == key)
        {
            theList.erase(iter)
        }

    }
}

template<typename K, typename V>
inline void map<K, V>::print()
{
    iterator iter = theList.begin;
    K temp;
    V temp2;
    for(int x=0; x < theList.size() ; x++)
    {
        temp = iter->first;
        temp2 = iter->second;
        cout << "Key:" << temp << " Value:" << temp2 << "\n";
    }



}

#endif;

testMap1.cpp

#include "map1.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
    map<int, string> theMap();
    theMap.insert(1, "one");
    theMap.insert(2, "two");
    theMap.insert(2, "double");
    theMap.insert(3, "three");

    theMap.print();

    theMap.remove_key(3);

    cout << "please enter a number" << "\n";
    int temp;
    cin >> temp;

    bool contains = theMap.contain_key(temp);
    if(contains)
    {
        cout << theMap.value_of(temp);
    }
    else
    {
        cout << "we don’t have this key " << temp << " in the map" << "\n";
    }


    return 0;
}
like image 951
drenfro87 Avatar asked Dec 20 '22 07:12

drenfro87


1 Answers

map<int, string> theMap();

this is declaring a function theMap not invoke the default constructor of map

remove ()

map<int, string> theMap/*()*/;
like image 107
yngccc Avatar answered Dec 24 '22 01:12

yngccc