简体   繁体   中英

Templated classes in C++

Below is my code. I am using the GNU G++ compiler. I am getting error "main.cpp: In function 'int main()': main.cpp:12:9: error: 'class hash' has no member named 'printVectorContents'"

I know I am overlooking something stupid I just can't put my finger on it. Any help?

int main()
{
    std::cout << "Yeah you did dat"<<std::endl;
    hash<int> newHash;
    newHash.printVectorContents();
    std::cin.get();
    return 0;
}

and my templated class...

template <class dataTypeClass> class hash{
private:
    std::vector <dataTypeClass> hashVector;
    std::string mName;
public:
    hash(){}
    hash(std::string aName = "unnamedHash"):mName(aName),hashVector(100,NULL){}
    ~hash(){std::cout<<"Hash "<<mName<<"destructing!";}
    void addHashItem(dataTypeClass aHashItem, std::string akey){}
    int hashFunction(dataTypeClass hashItem){return 0;}
    void printVectorContents()
    {
        for (typename std::vector<dataTypeClass>::iterator i = hashVector.begin(); i<hashVector.rend(); i++)
        {
            std::cout<< *i << std::endl;
        }
    }
};
  1. hash is a common name, make it myhash , etc. or place it into its own namespace to avoid name clashes.

  2. Remove hash(){} . You have a constructor taking a default parameter already, when you declare hash<int> myhash; , it doesn't know which constructor to call.

  3. Change i<hashVector.rend() to i != hashVector.end() .

  4. Change hashVector(100,NULL) to hashVector(100) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM