【问题标题】:OOP Bubble sort C++ programOOP 冒泡排序 C++ 程序
【发布时间】:2019-10-07 11:57:44
【问题描述】:

我收到这些错误 编译器错误 C3867 (((( 'func': 函数调用缺少参数列表;使用 '&func' 创建指向成员的指针))))

什么都没有

#include <iostream>
using namespace std;

class Cuzmo
{
private:
    int array[1000];
    int n;

public:
    Cuzmo ()
    {
        int array[] = { 95, 45, 48, 98, 485, 65, 54, 478, 1, 2325 };
        int n = sizeof (array) / sizeof (array[0]);
    }

    void printArray (int* array, int n)
    {
        for (int i = 0; i < n; ++i)
            cout << array[i] << endl;
    }

void bubbleSort (int* array, int n)
{
    bool swapped = true;
    int j = 0;
    int temp;

    while (swapped)
    {
        swapped = false;
        j++;
        for (int i = 0; i < n - j; ++i)
        {
            if (array[i] > array[i + 1])
            {
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swapped = true;
            }
        }
    }
}
};

int main ()
{
    Cuzmo sort;

cout << "Before Bubble Sort :" << Cuzmo::printArray << endl;

cout << Cuzmo::bubbleSort << endl;

cout << "After Bubble Sort :" << Cuzmo::printArray << endl;

return (0);
}

我收到这些错误 编译器错误 C3867 (((( 'func': 函数调用缺少参数列表;使用 '&func' 创建指向成员的指针))))

【问题讨论】:

  • 你的构造函数并没有按照你的想法去做。
  • array 是构造函数中的局部变量。构造函数退出后不再存在。与n相同,这些与同名的类成员无关。

标签: c++ oop


【解决方案1】:

这不是你调用没有参数的函数f 的方式:

f;

这就是你的做法:

f();

此外,您尝试将bubbleSort() 的返回值发送到cout,但由于该函数的返回类型为void,因此没有这样的值。

事实上,您的printArray() 函数也是如此:它已经进行了打印,并且没有结果值要发送到cout

试试:

cout << "Before Bubble Sort :";
Cuzmo::printArray();
cout << endl;

Cuzmo::bubbleSort();

cout << "After Bubble Sort :";
Cuzmo::printArray();
cout << endl;

另一个问题是您在构造函数中声明并初始化了一个局部变量array;此变量与成员无关。

您的变量n 也是如此。你不断地重新声明新的局部变量,隐藏成员变量。

【讨论】:

    【解决方案2】:

    也许您只是在函数调用后忘记了括号? 试试Cuzmo::printArray()Cuzmo::bubbleSort()。 此外,您可能希望使用 std::vector 而不是固定大小的 int 数组(以便循环遍历实际条目而不是 10000 个大部分未初始化的值)并查看 std::swap。

    【讨论】:

      猜你喜欢
      • 2014-03-26
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      相关资源
      最近更新 更多