Searching through a vector of a class of objects

I've got a .h class called data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  class data
{

    public:

        /*!
        * constructor and destructor
        */

        data();
        virtual ~data(void);


        double getPrice();
        double getID();
        int getCount();

        void setPrice(double dPrice);
        void setID(double dID);
        void setCount(int dCount);


    private:

        double price;
        double ID;
        int count;

};


And Another .h class where I read in a .csv file and store the data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   class data
{
    public:

        data();
        virtual ~ data();

        void populateVector(vector <data> &);
        void findHighestCount(vector <data> &);

    private:

       vector <data> dataVector;
       double highestCount;
};


and the .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    void dataRunner::populateVector(vector <data>& dataVector) {

    string temp;
    data vdata;

	ifstream inFile("data.csv");

	//check if file is open for reading
	if (inFile.is_open()) {

		cout << "File Opened!" << endl;
		cout << endl;
	}
	else{

        cout << "File Could not be opened" << endl;
	}


	while(!inFile.eof())
        {
            if( getline(inFile, temp, ','))
            {
               
                vdata.setPrice(atof(temp.c_str()));


                getline(inFile, temp, ',');
                vdata.setCount(atoi(temp.c_str()));

                getline(inFile, temp, ',');
                vdata.setID(atof(temp.c_str()));

                dataVector.push_back(vdata);
    }

  }

  inFile.close();

}


How do I search through the vector to , for example, find the highest or lowest count? I have a function dataRunner::findHighestCount (vector <data>& dataVector)
Last edited on
1
2
3
4
5
std::vector<data> foo;
auto result = std::max_element( foo.begin(), foo.end(),
                 []( const data & lhs, const data & rhs ) {
                     return lhs.getCount() < rhs.getCount();
                     } );

See also std::find_if for searching.
Why do I get an error when I use

 
  dataRunner::findHighestCount (vector <data>& dataVector)


Am I not allowed to use the same dataVector?
What error?

Whose highest count should that function retrieve and where should it put the result?
I want to print out the information (ID, PRICE) associated with the highest count and print it out on the screen for the user. This is what my main function looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

vector <data> dataVector;

int main()
{


    	dataRunner dataObj;

        dataObj.populateVector(dataVector);
       
       dataObj.findHighestCount(dataVector);

}


I get an error at the dataObj.findHighestCount(dataVector); saying there is an
undefined reference to dataRunner::findHighestCount

That is a linker error.
The linker combines compiled code from object files and libraries into executable binary.

That error says that no linked object file contains the implementation of function dataRunner::findHighestCount.

You have not shown the source code of that function's implementation. Where is it?

(Now I notice that you have posted two (conflicting) class data definitions, but no class dataRunner definition.)
The dataRunner.h is simple and just has a constructor/destructor and definitions of my functions

My main is also in a separate class main.cpp class
You have shown
1
2
3
void dataRunner::populateVector(vector <data>& dataVector) {
  // some code
}


Where is the:
1
2
3
void dataRunner::findHighestCount(vector <data>& dataVector) {
  // some code
}
Topic archived. No new replies allowed.