gpt4 book ai didi

c++ - C++ 代码错误 "expected constructor, destructor, or type conversion before ‘(’ token ”和 "no matching function for call to ..."

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:08:16 25 4
gpt4 key购买 nike

真正尝试解决错误,仔细检查所有内容。请帮忙。c++ 新手,请多关照。

头文件(.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文件

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

错误:

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]

最佳答案

您缺少所有函数实现中的返回类型,以及迭代器的一些 typename,例如:

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

另外,作为一个设计技巧,直接在 ctor-initializer 中初始化你的东西:

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

optimized_quick_sort(list<T> array)
: array(array.begin(), array.end())
{ //^^^^^
}

关于c++ - C++ 代码错误 "expected constructor, destructor, or type conversion before ‘(’ token ”和 "no matching function for call to ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6633574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com