user2935569
user2935569

Reputation: 347

sort element inside a vector c++

I have a text file points.txt

which is formatted by Points/NotPoints , X ,Y

Points, [3, 3]
NotPoints, [0, 0]
Points, [4, 4]
NotPoints, [0, 0]

What I want to do is retrieve the X values from the textfile and place it into a vector and sort the X values by either ASC or DSC order.

I found a similar post for my question

www.stackoverflow.com/questions/7102246/sorting-a-vector-of-objects-in-c#=

but the thing is, when I tried to do this at main.cpp,

sort(pointsVector.begin(), pointsVector.end(),sortOrder()) ;

it shows an error,Non static members sortOrder found in multiple base-class subobjects of type Points.

see my codes below for on my implementation.

Some Requirements for my task,

I must initialize int x as protected variable as there are other class inheriting it , 
I can't use struct therefore I am using get and set

Codes

Points.h

#ifndef __Points__Points__
#define __Points__Points__
#include <iostream>

class Points {
    friend class main;

protected:
     int x;
     int y;

private:
     friend bool sortOrder (const Points & rhs, const Points & lhs);
public:
    Points() {
        x=0;
        y=0;
    };//default Constructor

    Points (int x , int y);
    int getX();
    int getY();

    void setX(int x);
    void setY(int y);

    bool sortOrder (const Point2D & rhs, const Point2D & lhs) {
        return lhs.x < rhs.x;
    }
};
#endif 

Points.cpp

#include "Points.h"

Points::Points(int x , int y) {
    setX(x);
    setY(y);

}

int Points::getX() {
    return x;
}

int Points::getY() {
    return y;
}

void Points::setX(int x)   {
    this->x = x;
}

void Points::setY(int y)   {
    this->y = y;
}

main.h

 #ifndef Points_main_h
 #define Points_main_h
 #include <iostream>
 #include "Points.h"
 #include <vector>

class main : public Points  {

private:
    int input;
    std::string type,line;
    std::string pointX, pointY;
    std::vector<Points> pointsVector;
 public:
    void mainMenu();
    void getPoint2DRecordsFromFile();
};
#endif 

main.cpp

 #include <iostream>
 #include "main.h"
 #include "Points.h"
 #include <fstream>
 #include <sstream>
 #include <string>
 #include <vector>
 #include <stdio.h>
 #include <algorithm>

class main outputMethod;

void main::getRecordsFromFile() {

    std::ifstream readFile;
    //put your text file name inside the ""
    readFile.open("points.txt");
    if (!readFile.is_open()) {
        std::cout <<" "<< std::endl;
        std::cout << "File Not Found!" << std::endl;
        std::cout <<" "<< std::endl;
    }
    else    {
        while (readFile.good()) {
            while(getline(readFile,line))   {
                std::stringstream iss(line);
                getline(iss, type,',');
                if(type == "Points")  {

                getline(iss, pointX,'[');
                getline(iss, pointX,',');

                getline(iss, pointY ,' ');
                getline(iss, pointY,']');

                Points pointsConsturctor(std::stoi(pointX),std::stoi(pointY));
                pointsVector.push_back(pointsConsturctor);
                }
            }
        }
    }
    readFile.close();

    /*error!
    Non static members sortOrder found in multiple base-class subobjects of type Points*/
    sort(pointsVector.begin(), pointsVector.end(),sortOrder());

    for (int i = 0; i<pointsVector.size(); i++) {
        std::cout << pointsVector[i].getX() << "," << pointsVector[i].getY() << std::endl;
    }  
}


void main::mainMenu() {

    std::cout << "1. Read data " << std::endl;
    std::cout << "2. sort " << std::endl;
    std::cin >> input;

    switch ( input ) {
        case 1:
            std::cout << "In Progress!" << std::endl;
            break;
        case 2:
            getRecordsFromFile();
            break;
        default:
            break;
        std::cin.get();
    }

}

int main()  {
    outputMethod.mainMenu();
}

Upvotes: 0

Views: 239

Answers (4)

Some programmer dude
Some programmer dude

Reputation: 409176

Since sortOrder is a function, when you do

sort(point2DVector.begin(), point2DVector.end(),sortOrder()) ;

you are actually attempting to call the function. Since you don't provide the wanted arguments you will get an error. Instead just pass it without the parentheses:

sort(point2DVector.begin(), point2DVector.end(), &Points::sortOrder) ;

The other error is that it's a member function, and all member functions have a hidden first argument that is the this pointer in the function, so just doing the above won't work either. You either have to make the function a free-standing function, or make it a static member function.

Upvotes: 2

codeling
codeling

Reputation: 11379

You're declaring sortOrder as friend when you're actually defining it as a member function. friend is intended to give functions access to a class which are not part of the class themselves.

Furthermore, the two declarations differ in the parameter types - the one takes two Points, the other two Point2D - where does this Point2D type come from?

So:

  • Put the sortOrder function outside of your class declaration (or make it static as suggested by Joachim Pileborg)
  • Correct the parameter types
  • Remove the parenthesis after sortOrder in the std::sort call - like it is at the moment, you're trying to call the function.

Upvotes: 2

Claudio
Claudio

Reputation: 10947

Note that in class Points you have defined sortOrder twice (as friend function and as method):

friend bool sortOrder (const Points & rhs, const Points & lhs);

bool sortOrder (const Point2D & rhs, const Point2D & lhs) {
    return lhs.x < rhs.x;
}

Upvotes: 5

BigBoss
BigBoss

Reputation: 6914

When you write sortOrder() compiler think you are calling a function, but what you really need for this parameter s a function or a functor(a class that act like a function). So you can either write a function or a functor or use a lambda expression. Or in simplest case you can make sortOrder an static function and call it like this:

sort(point2DVector.begin(), point2DVector.end(),&Points::sortOrder)

Upvotes: 2

Related Questions