当我尝试调用insertionSort()类方法时,我收到链接器错误。为什么会这样?
错误
Error 3 error LNK2019: unresolved external symbol
"public: static void __cdecl Guitar::insertionSort(class Guitar * const,int,int)" (?insertionSort@Guitar@@SAXQAV1@HH@Z)
referenced in function _main SortingDemo.obj SortingDemo
代码
//Guitar.h
#include <string>
class Guitar
{
void compareExchange(Guitar &a, Guitar &b);
void exchange(Guitar &a, Guitar &b);
public:
int yearMade;
int value;
std::string brand;
std::string model;
void static insertionSort(Guitar a[], int left, int right);
Guitar::Guitar(): yearMade(0), value(0){}
Guitar::Guitar(int year_made, int val, std::string brand_name, std::string model_name)
{
yearMade = year_made;
value = val;
brand = brand_name;
model = model_name;
}
};
//Guitar.cpp
#include "Guitar.h"
#include <string.h>
void exchange(Guitar &a, Guitar &b)
{
Guitar temp = a;
a = b;
b = temp;
}
void compareExchange(Guitar &a, Guitar &b)
{
if (b.value < a.value)
exchange(a, b);
}
void insertionSort(Guitar a[], int left, int right)
{
int i;
for (i = right; i > left; i--) compareExchange(a[i - 1], a[i]); // puts the lowest value in the array into array[0]
int lowest = a[0].value;
for (i = left + 2; i <= right; i++)
{
int j = i;
Guitar v = a[i];
while (v.value < a[j - 1].value)
{
a[j] = a[j - 1];
j--;
}
a[j] = v;
}
}
//Main
int main(int argc, char* argv[])
{
Guitar g1;
g1.value = 150000;
g1.yearMade = 1957;
g1.model = "Strat";
g1.brand = "Fender";
Guitar g2;
g2.value = 150;
g2.yearMade = 2015;
g2.model = "Squire";
g2.brand = "Fender";
Guitar g3;
g3.value = 5000;
g3.yearMade = 2015;
g3.model = "Les Paul";
g3.brand = "Gibson";
Guitar gtr_guitar;
Guitar gtrArr[] = { g1, g2, g3 };
Guitar::insertionSort(g, 0, 2);
}