0

I have a vector of Person object pointers. I am trying to use std:sort to sort the vector based on the "name" of each object. I am getting an unresolved external symbol error when I try to build and run; can anyone see where I am going wrong? error:

Error   LNK2019 unresolved external symbol "public: static bool __cdecl Person::sortByName(class Person *,class Person *)" (?sortByName@Person@@SA_NPAV1@0@Z) referenced in function _main  Lab1b   C:\Users\jayjo\source\repos\Lab1b\Lab1b\Lab1b.obj   1   

error

Main cpp:

#include <iostream>
#include "Person.h"
#include "Employee.h"
#include "Customer.h"
#include <vector>
#include <algorithm>


int main()
{
    vector<Person*> people;
    people.push_back(new Person("Peter"));
    people.push_back(new Person("John"));
    people.push_back(new Person("David"));
    people.push_back(new Person("Aaron"));

    sort(people.begin(), people.end(), Person::sortByName);

    for (int i = 0; i < people.size(); i++)
    {
        cout << people[i]->getName() << endl;
    }

}

Person.h:

#pragma once
#ifndef Person_H
#define Person_H
using namespace std;
#include <iostream>


class Person
{
public:
    Person(string);
    virtual void printname(); 
    static bool sortByName(Person* A, Person* B);
    string getName();
protected:
    string name;
};

#endif // !Person_H

Person.cpp:

#include "Person.h"
using namespace std;


Person::Person(string n)
{
    name = n;
}

void Person::printname()
{
    cout << "Name: " << name << endl;
}

string Person::getName()
{
    return name;
}

static bool sortByName(Person* A, Person* B)
{
    return (A->getName().compare(B->getName()));
}
craig2020
  • 321
  • 1
  • 6
  • 12
  • `static bool sortByName(...)` - this is a free function you are defining, not a member of class `Person` – Mat Feb 02 '20 at 19:42

1 Answers1

2

Instead of this:

static bool sortByName(Person* A, Person* B)
{
    return (A->getName().compare(B->getName()));
}

This:

bool Person::sortByName(Person* A, Person* B)
{
    return (A->getName().compare(B->getName()) != 0);
}

In C++, you declare the class member function as static, but you leave the static keyword off when you define it. Also, the function needs to be defined as a class member (Person::).

M.M
  • 138,810
  • 21
  • 208
  • 365
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Also very important is that `compare()` returns an `int`, just like `memcmp`/`strcmp`. Casting it to bool will yield wrong results, since `std::sort` expectes less-like behaviour. – krzaq Feb 02 '20 at 19:46
  • @krzaq could you please elaborate or how I can fix this? I changed the code to what selbie suggested which was correct, although now I am getting an expression: invalid comparator error which is probably caused by what your are saying: https://i.ibb.co/yN4ZZgx/err2.png – craig2020 Feb 02 '20 at 20:45
  • @selbie Thanks a lot for the reply, do you know why I am now getting the following error - expression: invalid comparator error? https://i.ibb.co/yN4ZZgx/err2.png – craig2020 Feb 02 '20 at 20:55
  • just return `A->name < B->name` – krzaq Feb 02 '20 at 21:45