0

so I have a class A - parent and B - child, i also have a class c thats unrelated that holds a vector of child objects. Im trying to sort the vector using a compare function in class A that uses operator overloading and having diffcultly making this comparison this is the a.hpp and a.cpp combined

class c; //forward declare
class a{
a();

setfunc(string name){names=name}; //added names to vector
getFunc(){return names};
 
//use in other class  
friend bool compare(const a& A,const a& A1) //would like to compare vector element object
if(A.name == A1.name)
{
 return false;
}
if(A.name > A1.name)
{return true;}
if(A.name < Al.name)
{return false;}
else
return false;
}


friend bool operator==(const a& A,const a& A1)
{return A.names == A1.names};
friend bool operator<(const a& A,const a& A1)
{return A.names < A1.names};
friend bool operator>(const a& A,const a& A1) //im using the operators bcuz my understanding it is good practice to use with compare function

//and its a way to compare objects

{return A.names > A1.names};

friend class c;
private:
 string names;
}

theres another class b which inherited all the variables thats all done done correct, then class c holds the vector

#include "a.hpp"
class c
class c{
c();

setVec(const a& A)
{
 A.getFunc(); //pushing objects into vector
vec.push_back(A);
}

Vecsort(const a& A,const a& A1) //not sort algo
{
 for(int i = 0; i < vec.size();i++)
{
 if(compare(A,A1) == true) // how would i sent this vec[i] and vec[i+1] through there
{
 swap(A,A1)                // to be received by the compare func
 i = 0 ;                    //because currently the only thing it compares is the last 
                           //object that was set
}

private:
vector<b> vec; //child of a objects
}

TLDR, have 3 class, a - parent , b - child . c - friend. Ive initialized all parent objects through the child class in main using set func's and sent it to the friend class to sort in a vector, how do i successfully sent a vector element object to that compare() function to be compared? do i need a [] operator or do i need to change the params in the compare() to take a const vector<b&> object?

Colton F
  • 1
  • 1
  • Check stl sort function: https://www.cplusplus.com/reference/algorithm/sort/ – Tomingsun Oct 27 '21 at 05:23
  • i get that there's ways to do this. This is just a requirement I have to do it this way using this function. – Colton F Oct 27 '21 at 05:31
  • What are you trying to sort? Is it the vector `vec`? If so, why would your `sort()` even need those arguments? – Tharsalys Oct 27 '21 at 05:46
  • changed it, its not the sort algorithm, im trying to sort strings that have been initialized as objects . Its the vector vec which holds b class objects – Colton F Oct 27 '21 at 05:49
  • im trying to send the vec elements b which holds a name to the compare function, but the only object it recognizes is the very last b object that was set – Colton F Oct 27 '21 at 05:52
  • 1
    `how do i successfully sort using that compare function` You can't, You need < and > operations or a starship operator implemented. ALso that `for()` doesn't even looks close to a sort attempt. You're sayin sort is not sort, but sort word means sort algorithm, you have to be clear. – Swift - Friday Pie Oct 27 '21 at 05:55
  • Read your code carefully. For one, your `compare()` function only checks for equality ( and that too on attribute `name` which I don't see anywhere ); for sorting, you need to check for less than, greater than, etc. Moreover, `compare()` is doing the same thing as `friend operator==()`. Do some rubber duck debugging and reason about your code and what it's doing vs what you want it to do. – Tharsalys Oct 27 '21 at 05:56
  • so mainly, im trying to ascertain how to send a vector of objects to that compare function, i have the >,< in my code. I just dont know how to send that vector element. My logical, the vector contains a objects, i should be able to send a vector element of said a object to the compare function for comparison. Ive assign 100 objects to the vector but that compare function and operator only recognizes the last 1. – Colton F Oct 27 '21 at 06:08
  • @ColtonF if you have `<, >` in your code, then please post the code. Please see: https://stackoverflow.com/help/minimal-reproducible-example – Tharsalys Oct 27 '21 at 06:11
  • ive spent so much time on this and im just not able to figure it out and no matter how much i read about operators excetra i cant do it, im missing something. – Colton F Oct 27 '21 at 06:12
  • You can cut down your `compare` function to the single line `return A > A1;` – molbdnilo Oct 27 '21 at 08:35

0 Answers0