【问题标题】:error prototype does not match any in class错误原型与类中的任何一个都不匹配
【发布时间】:2016-10-18 17:18:17
【问题描述】:

我在 NetBeans 上运行它时遇到问题。 这是我的冒泡排序算法课程,包括主要功能:

#include <iostream>
using namespace std;


template <class elemType>
class arrayListType
{
public:
    void BubbleSort();

private:
    elemType list[100];
    int length;
    void swap(int first, int second);
    void BubbleUp(int startIndex, int endIndex);
    void print();
    void insert();
};

template <class elemType>
void arrayListType<elemType>::BubbleUp(int startIndex, int endIndex)
{

for (int index = startIndex; index < endIndex ; index++){
    if(list[index] > list[index+1])
        swap(index,index+1);
}
} 

template <class elemType>
void arrayListType<elemType>::swap(int first, int second)
{

elemType temp;
temp = list[first];
list[first] = list[second];
list[second] = temp;
} 

template <class elemType>
void arrayListType<elemType>::insert()
{
cout<<"please type in the length: ";
cin>>length;
cout<<"please enter "<<length<<" numbers"<< endl;
for(int i=0; i<length; i++)
{
    cin>>list[i];
}
}

template <class elemType>
void arrayListType<elemType>::print()
{
    cout<<"the sorted numbers" << endl;
    for(int i = 0; i<length; i++)
    {
        cout<<list[i]<<endl;        
    }
}

此函数声明中表示错误:

template <class elemType>
void arrayListType<elemType>::BubbleSort(elemType list[], int numvalues)
{
    insert();
    int current=0;
    numvalues--;

    while(current < numvalues)
    {
        BubbleUp(current,numvalues);
        numvalues--;
    }

    print();
}

主要功能:

    int main()
    {
    arrayListType<int> list ;
    list.BubbleSort();

    }

我之前做过另一种排序算法,但效果很好。我怎样才能真正解决这个原型匹配问题?

【问题讨论】:

    标签: c++ algorithm data-structures bubble-sort


    【解决方案1】:

    错误在这里:

    template <class elemType>
    void arrayListType<elemType>::BubbleSort(elemType list[], int numvalues)
    

    在您的课程中,您为该方法编写了一个原型,如下所示:

    void BubbleSort();
    

    这不匹配:

    BubbleSort(elemType list[], int numvalues)
    

    要解决错误,请从实际实现中删除参数,或将参数添加到原型中。您的错误是不言自明的,它抱怨原型与定义不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 2015-03-14
      • 2020-02-04
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多