出於某種原因,下面的代碼給出DEVC++編譯器錯誤:[錯誤]不能聲明成員函數「靜態雙*排序::冒泡(雙*,INT)」爲有靜態鏈接[-fpermissive]靜態函數調用不工作(C++)
BubbleSort.cpp:
#include <iostream>
#include "Sort.h"
int main(int argc, char** argv) {
double list[] = {4.0, 4.5, 3.2, 10.3, 2.1, 1.6, 8.3, 3.4, 2.1, 20.1};
int size = 10;
double* sortedList = Sort::bubbleSort(list, size);
return 0;
}
Sort.h:
class Sort
{
public:
static double* bubbleSort (double list[], int size);
}
;
Sort.cpp:
#include "Sort.h"
#include <algorithm> // std::swap
static double* Sort::bubbleSort (double list[], int size)
{
bool changed = true;
do
{
changed = false;
for (int j = 0; j < size - 1; j++)
{
if (list[j] > list[j +1])
{
std::swap(list[j], list[j + 1]);
changed = true;
}
}
}
while (changed);
return list; // return pointer to list array
}
本質上講,我試圖調用冒泡功能,而無需創建一個排序的對象。如果我先創建一個對象,代碼工作正常。
什麼可能導致錯誤?
感謝您的任何建議。
刪除原型實現中的靜態 – jbh
但是我不需要聲明靜態函數以便在不創建類的對象的情況下使用它? – navig8tr
「靜態」修飾符進入Sort.h中,但不在Sort.cpp中。 –