Member of the class "was not declared in this scope"

0

I have the following exercise:

  

Implement the int bsearch method (int i, int d, const T &   x) of the vector_t class that performs the recursive binary search in an ordered vector, returning the position of the found element and -1 if it does not find it

The class they give me as help is this:

    template <class T>
    class vector_t{
    private:
           T* v_;
           int sz_;
    public:
           vector_t(int sz);
           ~vector_t(void);
           int get_sz(void) const;
           T get_v(int pos) const;
           T& get_set_v(int pos);
    };

I have changed it             BBR (int v [], int i, int d, int x);

    template<class T>

int vector_t:: bsearch (int i, int d, const T& x){


    if (i>d){

        return -1;
    }

    int medio=(i+d)/2;

    if(v_[medio]==x){
        return medio;
    }

    else if(x<v_[medio]){
        return bsearch(i,medio-1,x);
    }
    else if(x>v_[medio]){
        return bsearch(medio+1,d,x);
    }
}

I did the main too:

    #include<iostream>
    #include"SinNombre1.cpp"
    using namespace std;

    int main (void){
        vector_t<int>& tabla[10];

        tabla[0]= 50;
        int buscado=50;
        int central=0;

        for(int i=0;i<=10;i++){
            (tabla[i])++;
        }

        int izq= 50;
        int dch= tabla-1;

        central= bsearch(izq,dch,buscado);
    }

Thanks

What happens that gives me errors that I do not understand:

In function 'int main()':
6    declaration of 'tabla' as array of references
8   'tabla' was not declared in this scope
22   'template<class T> class vector_t' used without template parameters
    In function 'int bsearch(int, int, const T&)':
32  [Error] 'v_' was not declared in this scope
    
asked by AER 22.07.2017 в 15:19
source

1 answer

2

Several things (really, I would touch a separate question for each error, but good):

  • Lines 6 and 8. I think you've messed with the vector. The vector is defined within the class , in v_ . Each time you create an instance of vector_t , the memory for the vector will be created (naturally, you have to implement the constructor and the other classes defined).

    What you have to do is define a single object that will be the one that contains the data:

    vector_t tabla(10);
    

    As I said before, the methods to fill the content of the internal vector are missing. You have to implement them (if you want to use [] , you will have to overload the operator [] , which implies defining and implementing a method of the class in the appropriate way). Or you can add a set(int posicion, const T&valor) method

    for (int i = 0; i < 10; i++) {
        tabla.set(i, i*10);
    }
    
  • Lines 22 and 32. The declaration of the function is wrong, it should be

    int vector_t<T>:: bsearch (int i, int d, const T& x){
    

    because the class is vector_t<T> . As this fails, it does not recognize the function as belonging to the class, so it does not know that v_ is a member of the class. Additionally, you have to declare the method in the public: part.

    Once this has been resolved, it will give you an error in the call to bsearch since, since it is a method of an instance, you must indicate which instance the method is called:

    central= tabla.bsearch(0, 9, buscado);
    
answered by 22.07.2017 / 18:37
source