【问题标题】:How do I access a vector within a class function? [closed]如何访问类函数中的向量? [关闭]
【发布时间】:2023-04-01 08:26:01
【问题描述】:

我目前正在尝试创建一个开放寻址哈希表,但是我似乎无法从一个类函数访问向量,并且该向量是在另一个类函数中创建的。

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

我们收到的错误信息是:

 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;
}

【问题讨论】:

  • “我遇到了麻烦” 太含糊了。 Clarify by editing your question please!
  • 实际上在哪一行?突出显示。
  • 我把这条线加粗了。该行是:OAHashVector.insert(addElement, hashvalue);
  • 我仍然无法为那个神秘的OAHashVectorOAHash 类找到任何声明/定义。你只是想念#include 别的什么吗?
  • 今后除了在研究上投入少量精力外,还要努力创建最少的示例。例如,菜单的文字完全不相关。通过这样做,您可能会在创建示例时自行发现问题。

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


【解决方案1】:

你在这个构造函数中声明OAHashVector

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

     vector<T> OAHashVector;

}

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多