Multiple Functions Definition Errors

2019-09-04 07:29发布

I have a header, and a cpp file I was attempting to build.

.cpp file:

#include "SelectionSort.h"  
void SelectionSort::Fill(){  
    Buffer = new char[Size];  
    for(int i=0;i<Size;i++){  
        Buffer[i] = rand() % 10;  
    }  
}  
void SelectionSort::PrintOut(){  
    for(int i=0;i<Size;i++){  
        cout<<Buffer[i]<<endl;  
    }  
}  
void SelectionSort::Sort(){  
    int lowest;  
    for(int i=0;i<Size;i++){  
        lowest=i;  
        for(int j=i;j<(Size-i);++j)  
            if(Buffer[j]>lowest) lowest = j;  
        swap(Buffer[lowest], Buffer[i]);  
    }    
}

.h file:

#ifndef SELECTIONSORT_H  
#define SELECTIONSORT_H  
#include <algorithm>  
#include <stdlib.h>  
#include <iostream>  
using namespace std;  
class SelectionSort {  
public:  
    SelectionSort();  
    SelectionSort(int S){Size= S;}  
    void Fill();  
    void PrintOut();  
    void Sort();  
private:  
    int Size;  
    char * Buffer;  
};  
#endif  /* SELECTIONSORT_H */

But I get these errors:

SelectionSort.cpp:17: multiple definition of 'SelectionSort::PrintOut()'
SelectionSort.cpp:17: first defined here
SelectionSort.cpp:23: multiple definition of 'SelectionSort::Sort()'
SelectionSort.cpp:23: first defined here
SelectionSort.cpp:10: multiple definition of 'SelectionSort::Fill()'
SelectionSort.cpp:10: first defined here,

How am I defining my functions incorrectly?

I am using netbeans and their generic make/build settings. I've been meaning to get more into make files, should i try to write my own and solve the problem?

1条回答
\"骚年 ilove
2楼-- · 2019-09-04 08:18

Once you get your code to compile, you have a number of logical mistakes (see comments):

void SelectionSort::Sort()
{       
     int lowest;       
     for(int i = 0; i < Size; i++)
     {           
         lowest = i;
         for(int j = i; j < (Size - i); ++j)    // j should terminate at the end of Buffer, not one before the end
             if(Buffer[j] > lowest) lowest = j; // comparing a data element to an index, comparison operator reversed
         swap(Buffer[lowest], Buffer[i]);
     }
} 
查看更多
登录 后发表回答