1

I cannot solve the error - main.cpp:31:34: error: ‘sortBook’ was not declared in this scope in the line sortBook(arr,arr+n,comparator);

I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors.

 #include<iostream>
    using namespace std;
     class BOOK{
        private:
            char bookname[20];
            float bookprice;
        public:
            void getBookDetails()
            {
            cout<<"Enter the Book Name:";
             cin>>bookname;
             cout<<"Enter the Book Price:";
             cin>>bookprice;
              }
              void displayDetails()
              {
                cout<<"Book Name:"<<bookname<<endl;
                cout<<"Book Price:"<<bookprice<<endl;
              }
     };
     bool comparator(string a,string b)
    {
        return a<b;
    }
    int main()
    {
        int n=5;
        string arr[]={"sandwich","apple","banana","zombie","pear"};
        
        sortBook(arr,arr+n,comparator);
           for(int i=0;i<n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49

2 Answers2

0

I think it's just that you have not declared sortBook function anywhere and trying to use it.

Asit Singh
  • 17
  • 2
  • where should I declare it ? –  Jun 13 '21 at 20:17
  • It needs to be declared before you use it. With that said where did you implement `sortBook()`? Also maybe @Job_September_2020 is correct in that you should use `std::sort()` instead of some `sortBook()` – drescherjm Jun 13 '21 at 20:23
-1

Your error shows up because you have not defined the method "sortBook".

There are 2 ways to fix this error:

  1. Define your own "sortBook" method in your class.
  2. Use C++ sort method that is defined in "Algorithm". Below is my code that uses this sort method. You have to "include algorithm " at the top of your file, and call the sort method.

#include<iostream>
#include<algorithm>   //////  YOU SHOULD INCLUDE THIS STATEMENT
    using namespace std;
     class BOOK{
        private:
            char bookname[20];
            float bookprice;
        public:
            void getBookDetails()
            {
            cout<<"Enter the Book Name:";
             cin>>bookname;
             cout<<"Enter the Book Price:";
             cin>>bookprice;
              }
              void displayDetails()
              {
                cout<<"Book Name:"<<bookname<<endl;
                cout<<"Book Price:"<<bookprice<<endl;
              }
     };
     bool comparator(string a,string b)
    {
        return a<b;
    }
    int main()
    {
        int n=5;
        string arr[]={"sandwich","apple","banana","zombie","pear"};
        
        std::sort(arr,arr+n,comparator); // HERE, YOU CALL : sort()
           for(int i=0;i<n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }

Note: My code above compiles with no errors. It runs and produces the correct output. Please let me know if you have any other issues.

Job_September_2020
  • 858
  • 2
  • 10
  • 25
  • Thanks, this code is working fine. But i have one issue I am unable to create an array object of type Book in it, and a function sort book- to sort arra in alphabetical order . How can I do that. –  Jun 13 '21 at 20:29
  • @Utkarsh Chauhan, You are welcome. I am glad it works. BTW, please accept the answer so that future readers can re-use the answer if they have similar questions. :-) Have a nice day. – Job_September_2020 Jun 13 '21 at 20:33
  • 1
    Sure, thank a lot. –  Jun 13 '21 at 20:35
  • If you want to sort an array of BOOK objects, the solution is very similar. (1) You can write a new method "comparator" that has 2 arguments : bool comparator(const Book& a, const Book& b) and in that method you compare the "bookname" of "a" vs "b". (2) You can make the data member "bookname" public to make it accessible inside that "comparator" function..... If you still need extra help, please write the next question with your own code, then we can help you quickly with your new code. – Job_September_2020 Jun 13 '21 at 20:56
  • Thanks, for explaining. Yeah now I can do it. –  Jun 13 '21 at 21:03
  • How can I call the book name and the price, and then getting input from user, can I sort it alphabetticaly. –  Jun 14 '21 at 16:29
  • Sure, thanks :), yeah answer is true, but ehat i am trying to do, I cannot achieve that https://stackoverflow.com/questions/67970841/want-to-create-object-array-of-type-book-in-this-program/67972145#67972145 –  Jun 15 '21 at 03:08
  • I am trying to get the output of book details and book price and then sort it alphabeticaly by getting input data from user. –  Jun 15 '21 at 03:12