Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in c++ code "expected constructor, destructor, or type conversion before ‘(’ token" and "no matching function for call to ..."

really trying to solve the errors, double checked everything. please help. New to c++, please be kind.

header file (.h)

#ifndef GUARD_Optimized_quick_sort_h
#define GUARD_Optimized_quick_sort_h

#include <vector>
#include <list>
#include <iterator>

using namespace std;

template <class T> class optimized_quick_sort 
{
    public:
        optimized_quick_sort(vector<T> array) 
        {
            this->array = array;
        }

        optimized_quick_sort(list<T> array)
        {
            vector<T> temp(array.begin(), array.end());
            this->array = temp;
        }

        vector<T> sort()
        {
            typename vector<T>::iterator begin = array.begin();
            typename vector<T>::iterator end = array.end();

            quick_sort(begin, end);
            return array;
        }

    private:

        vector<T> array;


        int quick_sort( vector<T>::iterator , vector<T>::iterator);
        int insertion_sort(vector<T>::iterator, vector<T>::iterator);
        vector<T>::size_type devide(vector<T>::iterator, vector<T>::iterator );
};

#endif

.cpp file

#include "optimized_quick_sort.h"

#include <iostream>
 using namespace std;

template <class T>
optimized_quick_sort<T>::quick_sort( vector<T>::iterator begin, vector<T>::iterator end)
{

    if(iterator::distance(begin, end) <= 5)
    {
        this->insertion_sort(begin, end);
    }
    else 
    {

        typename vector<T>::size_type devider;

        devider = devide(begin, end);

        quick_sort(begin + (devider+1),end);
        quick_sort(begin,begin + (devider-1));
    }

    return 1;
}

template <class T>
optimized_quick_sort<T>::devide(vector<T>::iterator begin, vector<T>::iterator end)
{
    return distance(begin, end)/2;
}

template <class T>
optimized_quick_sort<T>::insertion_sort(vector<T>::iterator begin, vector<T>::iterator end)
{
    return 1;

}

int main(int argc, char* argv[])
{ 
    vector<int> vec;
    int temp;
    while(cin >> temp)
        vec.push_back(temp);

    list<int> l(vec.begin(),vec.end());

    optimized_quick_sort<int> obj = optimized_quick_sort<int>(l);

    vec = obj.sort();

    vector<int>::iterator iter = vec.begin() ;

    while(iter != vec.end())
        cout << *iter++;

    cout << endl;
    return 1;
}

errors:

optimized_quick_sort.cpp:7: error: expected constructor, destructor, or type conversion before ‘(’ token

optimized_quick_sort.cpp:29: error: expected constructor, destructor, or type conversion before ‘(’ token

optimized_quick_sort.cpp:35: error: expected constructor, destructor, or type conversion before ‘(’ token

optimized_quick_sort.h: In member function ‘std::vector<T, std::allocator<_CharT> > 
optimized_quick_sort<T>::sort() [with T = int]’:

optimized_quick_sort.cpp:52:   instantiated from here

optimized_quick_sort.h:29: error: no matching function for call to ‘optimized_quick_sort<int>::quick_sort(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
std::allocator<int> > >&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)’

optimized_quick_sort.h:38: note: candidates are: int optimized_quick_sort<T>::quick_sort(int&, int&) [with T = int]
like image 757
aditya mishra Avatar asked Dec 17 '22 12:12

aditya mishra


2 Answers

You're missing the return type in all of your function implementations, as well as a little typename for the iterators, example:

template <class T>
int optimized_quick_sort<T>::quick_sort( typename vector<T>::iterator begin, typename vector<T>::iterator end)
^^^                                      ^^^^^^^^                            ^^^^^^^^
{
  // ...
}

Also, as a design tip, directly initialize your stuff in the ctor-initializer:

optimized_quick_sort(vector<T> array)
  : array(array)
{ //^^^^^
}

optimized_quick_sort(list<T> array)
  : array(array.begin(), array.end())
{ //^^^^^
}
like image 162
Xeo Avatar answered Apr 27 '23 01:04

Xeo


You need the typename keyword:

int optimized_quick_sort<T>::quick_sort(
             typename vector<T>::iterator begin,
             typename vector<T>::iterator end)
{
      ...
}

Also, who said iterator is a class that you can use :: on? It may just be a pointer.

iterator::distance(begin, end) <= 5

distance is in std namespace, not iterator class.

std::distance(begin, end) <= 5
like image 42
Armen Tsirunyan Avatar answered Apr 27 '23 02:04

Armen Tsirunyan