如何在类函数中访问向量?

时间:2014-11-06 00:23:03

标签: c++ class templates c++11 vector

我目前正在尝试创建一个Open Addressed Hash Table但是我似乎无法从一个类函数访问一个向量,并且该向量是在另一个类函数中创建的。

我无法将两个变量真正传递给函数。我想从插入函数访问构造函数中创建的向量。

我们收到的错误消息是:

 OAHashVector was no declared in this scope. OAHashVector.insert(addElement, hashvalue);

导致错误的是这一行:

 OAHashVector.insert(addElement, hashvalue);

#include <iostream>
#include <string>
#include <vector>

using namespace std;

std::string type;

template <class T>
class openhash {
private:  
    T addElement;
    T deleteElement;
    T searchElement;
    T input_element;

public:
    void initilisation();
    void delete_element();
    void lookup_element();
    void insert_element(openhash<T>);
    void size_table();
    void print_table();
    T HashKeyCalc(T input_element, string element_type){   // function takes in string and returns hash value

        int hashKey;
        /*if (element_type == "string"){
            int i = 0;
            while (i < input_element.size()){
                hashKey = hashKey + input_element.at(i);
                i++;
            }

            hashKey = hashKey%100;
        } else*/ if (element_type == "int"){
            hashKey = input_element;
            hashKey = hashKey%100;
        } /*else if (element_type == "char"){
            hashKey = input_element;
            hashKey = hashKey%100;
        } else if (element_type == "double"){
            for(int i = 0; i <17; i++){
                input_element = input_element*10;
            }
            for(int j = 0; j <17; j++){
                input_element = input_element%10;
            }
            hashKey = input_element;
        } else if (element_type == "float"){
            for(int i = 0; i <7; i++){
                input_element = input_element*10;
            }
            for(int j = 0; j <7; j++){
                input_element = input_element%10;
            }
            hashKey = input_element;
        }*/




        cin.clear(); // clears input buffer
        input_element;

        return hashKey;

    }
    openhash();
    ~openhash();

};



    template <class T>
    void openhash<T>::initilisation(){
        cout << "iii";
    }
    template <class T>
    void openhash<T>::delete_element(){
        cout << "iii";
    }
    template <class T>
    void openhash<T>::lookup_element(){
        cout << "iii";
    }

    template <class T>
    void openhash<T>::insert_element(openhash<T> OAHash){
        cout << "Please enter the element you would like to add: ";

        cin >> addElement; //takes in element user wishes to add

        int hashvalue = OAHash.HashKeyCalc(addElement, type);
        **OAHashVector.insert(addElement, hashvalue);**

    }


    template <class T>
    void openhash<T>::size_table(){
        cout << "iii";
    }
    template <class T>
    void openhash<T>::print_table(){
        cout << "iii";
    }

    template <class T>
    openhash<T>::openhash(){

         vector<T> OAHashVector;

    }

    template <class T>
    openhash<T>::~openhash(){

    }


template < class T >
void menu(openhash<T> OAHash){
char option;




cout << "\t\t MENU \n"
     << "\t Press 'i' to insert an item to hashtable\n"
     << "\t Press 'd' to delete an item from hashtable\n"
     << "\t Press 'p' to print the hashtable\n"
     << "\t Press 'l' to look up an item in the hashtable\n"    
     << "\t Press 's' to display the size of the hashtable\n"
     << "\t Press 'q' to quit\n";

cin >> option;

switch(option){
    case 'i' : OAHash.insert_element(OAHash);
    break;
    case 'd' : OAHash.delete_element();
    break;
    case 'p' : OAHash.print_table();
    break;
    case 'l' : OAHash.lookup_element();
    break;
    case 's' : OAHash.size_table();
    break;
    case 'q' : cout << "hjil";
    break;
}


}

void selectTable(){



cout << "Please enter table type" << endl;
cin >> type;

if ( type == "int" ){
    openhash<int> OAHash;
    menu(OAHash);
} /*else if (type == "string"){
openhash<std::string> OAHash;
menu(OAHash);
} else if (type == "char"){
openhash<char> OAHash;
menu(OAHash);
} else if (type == "float"){
openhash<float> OAHash;
menu(OAHash);
} else if (type == "double"){
openhash<double> OAHash;
menu(OAHash);
}*/

}


int main(){

selectTable();


return 0;
}

1 个答案:

答案 0 :(得分:1)

您在此构造函数中声明OAHashVector

template <class T>
openhash<T>::openhash(){

     vector<T> OAHashVector;

}

它是一个局部变量,仅在该范围内有效。它不会在其他地方使用。您希望将事物作为函数参数适当地传递,或者将其声明为类成员变量,如果这更有意义的话。

我建议您阅读this tutorial on variable scope,以及"not declared in this scope"的Google搜索结果。您可能希望在将来发布之前进行最少量的研究。