用C ++中的另一个类调用函数

时间:2018-02-24 06:21:12

标签: c++

所以,我正在用C ++编写一个程序,它在Sort类中有一个函数,我希望在Main类中调用它。该函数有几个使用的数据成员,它们存在于该类中,不存在于Main类中,并且我不断收到C2660错误,“函数不接受0个参数”。有没有办法(没有写一堆getter和setter)来解决这个问题?

#include "Sort.h"
#include "Timer.h"
using namespace std;
int main
{
Sort *sort = new Sort();
Timer ti;
sort->SetRandomSeed(12345);

sort->InitArray();
cout << "starting InsertionSort" << endl;
ti.Start();
sort->InsertionSort();
ti.End();
cout << "Insertion sort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;

//sort->InitList();
//cout << "starting InsertionSortList()" << endl;
//ti.Start();
//sort->InsertionSortList();
//ti.End();
//cout << "Insertion sort list duration: " << ti.DurationInMilliSeconds() << "ms" << endl;

sort->InitArray();
cout << "starting SelectionSort" << endl;
ti.Start();
sort->SelectionSort();
ti.End();
cout << "SelectionSort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;

sort->InitArray();
cout << "starting MergeSort" << endl;
ti.Start();
sort->MergeSort();
ti.End();
cout << "MergeSort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;

sort->InitArray();
cout << "starting QuickSort" << endl;
ti.Start();
sort->QuickSort();
ti.End();
cout << "QuickSort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;

sort->InitVector();
cout << "starting std::sort() of Vector<int>" << endl;
ti.Start();
sort->VectorSort();
ti.End();
cout << "std::sort() duration: " << ti.DurationInNanoSeconds() << "ns" << endl;

delete sort;

cout << endl <<"Press [Enter] key to exit";
getchar();

}


Sort.cpp
//const int for array
int num = 10000000;
int val = 10000;
//array
int *tmpArray, *qArr, *insArr, *selArr, *mergArr = NULL;
int low, high;

//duration for timer
int duration = 0;

Sort::Sort()
{
}


Sort::~Sort()
{
}

void Sort::InitArray()
{
//int for index
int i = 0;
tmpArray = new int[num];
qArr = new int[num];
insArr = new int[num];
selArr = new int[num];
mergArr = new int[num];
//fill temp array with sequential numbers
for (int i = 0; i < num; i++)
{
    tmpArray[i] = 1 + rand() % val;
}

for (i = 0; i < num; i++)
{
    qArr[i] = tmpArray[i];
    insArr[i] = tmpArray[i];
    selArr[i] = tmpArray[i];
    mergArr[i] = tmpArray[i];
}
low = qArr[0];
high = qArr[num - 1];
int n = sizeof(tmpArray) / sizeof(tmpArray[0]);
}

void Sort::InitVector()
{
vector<int> v(num);
std::generate(v.begin(), v.end(), std::rand);
}

void Sort::InitList()
{
// A set to store values
std::list<int> l;
// Loop until we get 50 unique random values
while (l.size() < num)
{
    l.push_back(1 + rand() % val);
}
for (int n : l) {
    std::cout << n << '\n';
}
}

//setting seed
void Sort::SetRandomSeed(unsigned int seed)
{
seed = rand();
}


void Sort::InsertionSort()
{
int i, key, j;

for (i = 1; i < n; i++)
{
    key = insArr[i];
    j = i - 1;

    /* Move elements of arr[0..i-1], that are
    greater than key, to one position ahead
    of their current position */
    while (j >= 0 && insArr[j] > key)
    {
        insArr[j + 1] = insArr[j];
        j = j - 1;
    }
    insArr[j + 1] = key;
}
delete[] insArr;
insArr = NULL;
}

int Sort::partition(int qArr[], int low, int high)
{
int pivot = qArr[high];    // pivot
int i = (low - 1);  // Index of smaller element

for (int j = low; j <= high - 1; j++)
{
    // If current element is smaller than or
    // equal to pivot
    if (qArr[j] <= pivot)
    {
        i++;    // increment index of smaller element
        swap(&qArr[i], &qArr[j]);
    }
}
swap(&qArr[i + 1], &qArr[high]);
return (i + 1);
}

void  Sort::QuickSort(int qArr[], int low, int high)
{
if (low < high)
{
    /* pi is partitioning index, arr[p] is now
    at right place */
    int pi = partition(qArr, low, high);

    // Separately sort elements before
    // partition and after partition
    QuickSort(qArr, low, pi - 1);
    QuickSort(qArr, pi + 1, high);
}
delete[] qArr;
qArr = NULL;
}

void Sort::SelectionSort()
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++)
{
    // Find the minimum element in unsorted array
    min_idx = i;
    for (j = i + 1; j < n; j++)
        if (selArr[j] < selArr[min_idx])
            min_idx = j;

    // Swap the found minimum element with the first element
    swap(&selArr[min_idx], &selArr[i]);
}
delete[] selArr;
selArr = NULL;
}

void Sort::swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void Sort::VectorSort()
{
std::sort(v.begin(), v.end());
}

/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void Sort::merge(int mergArr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int* L;
int* R;
/* create temp arrays */
L = new int[n1];
R = new int[n2];

/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
    L[i] = mergArr[l + i];
for (j = 0; j < n2; j++)
    R[j] = mergArr[m + 1 + j];

/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
    if (L[i] <= R[j])
    {
        mergArr[k] = L[i];
        i++;
    }
    else
    {
        mergArr[k] = R[j];
        j++;
    }
    k++;
}

/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
    mergArr[k] = L[i];
    i++;
    k++;
}

/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
    mergArr[k] = R[j];
    j++;
    k++;
}
}

void Sort::MergeSort(int mergArr[], int l, int r)
{
if (l < r)
{
    // Same as (l+r)/2, but avoids overflow for
    // large l and h
    int m = l + (r - l) / 2;
    // Sort first and second halves
    MergeSort(mergArr, l, m);
    MergeSort(mergArr, m + 1, r);
    merge(mergArr, l, m, r);
}
delete[] mergArr;
mergArr = NULL;
}


Sort.h
#include <iomanip>
#include <fstream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<list>
using namespace std;

#pragma once
class Sort
{
public:
Sort();
~Sort();

void InitArray();

void InitVector();
void InitList();
void SetRandomSeed(unsigned int seed);  
int n, right, left, l, r, m;
vector<int> v;
void InsertionSort();
int partition(int qArr[], int low, int high);
void QuickSort(int qArr[], int low, int high);
void swap(int * xp, int * yp);
void VectorSort();
void MergeSort(int arr[], int l, int r);
void merge(int arr[], int l, int m, int r);
void SelectionSort();
};

忽略Timer类(这些都很好)这里是代码的其余部分。在main中显示了排序&gt; MergeSort()和sort-&gt; QuickSort()调用的C2660错误。

1 个答案:

答案 0 :(得分:0)

我自己解决了这些问题。我在Sort类中创建了没有参数的辅助函数,并调用函数本身以在main中使用。辅助函数如下所示。

Sort.cpp
//method for Main to run to prevent C2660 errors
void Sort::mergeHelper()
{
    MergeSort(mergArr, l, r);//call merge sort method
}

//method for Main to run to prevent C2660 errors
void Sort::quickHelper()
{
     QuickSort(qArr, low, high);//call quick sort method
}

int Main
{
    sort->quickHelper();
    sort->mergeHelper();
}