1

So I am calling a function which takes input as limit i, and array of objects h.

#include<iostream>
#include<vector>
using namespace std;
class hotel
{
private:
    string name,add;
    char grade;
    int charge,no;
public:
    void getdata();
    void putdata();
    void grade_print(int,hotel[]);
    void room_charge();
    void top2();
};
void hotel::getdata()
{
    cout<<"Add Name: ";
    getline(cin>>ws,name);
    cout<<"Add Addres: ";
    getline(cin>>ws,add);
    cout<<"Enter grade,room charge and no. of rooms: ";
    cin>>grade>>charge>>no;
}
void hotel::putdata()
{
    cout<<name<<endl<<add<<endl<<grade<<endl<<charge<<endl<<no;
}
void hotel::grade_print(int num,hotel h[])
{
    int i,j,k; char val;
    for(i=0;i<num;i++)
    {
        val=h[i].grade;
        for(j=0;j<num;j++)
        {
            if(h[j].grade==val)
            {
                cout<<h[j].grade<<endl;
                h[j].grade=' ';
            }
        }
    }

}
int main()
{
    std::vector <hotel> h(1);
    int i=0,j;
    cout<<"Want to add hotel? Press 1: ";
    cin>>j;
    while(j==1)
    {
        h[i].getdata();
        h.resize(2);
        i++;
        cout<<"Want to add more? Press 1 for yes, 0 for no: ";
        cin>>j;
    }
    grade_print(i,h);
}

error here is showing that the grade_print is out of scope. Also the grade is a private member but is called by member function. So why is it showing that grade can't be called. Please tell me why so and what can I do to fix it? Edit1: Declaring function as static void is not helping as the compiler is showing function can't be declared as static void.

D:\C++ Programs\testfile.cpp|30|error: cannot declare member function 'static void hotel::grade_print(int, hotel*)' to have static linkage [-fpermissive]|
  • `grade_print(i,h);` you need an object. Maybe hotel::grade_print() should be a static function or a free function. – drescherjm Jan 21 '18 at 17:22
  • 2
    Possible duplicate of [Cannot call member function without object = C++](https://stackoverflow.com/questions/3304369/cannot-call-member-function-without-object-c) – user7860670 Jan 21 '18 at 17:23
  • declare static void isn't helping as compiler is showing that the function can't be declared as static void –  Jan 21 '18 at 17:29
  • Wow, all those `endl`s: that stream is absolutely, positively, undeniably flushed. Most or all of those should be replaced with `'\n'`; that ends a line, without extra stuff. – Pete Becker Jan 21 '18 at 17:30
  • @PeteBecker thnx for the suggestion. Will do so –  Jan 21 '18 at 17:33

2 Answers2

0
grade_print(i, h);

A non-static member function should be called on a specific object like :

h[i].grade_print(i, h);

But, In your case grade_print have to be static, so it should be declared like this:

static void grade_print(int,hotel []);

And the definition goes like normal. Also, after making grade_print static, you have to call it like this:

hotel::grade_print(i, h);
Se7eN
  • 548
  • 1
  • 7
  • 22
0

From what I can understand, grade_print prints out information about a group of hotels passed as a parameter. If you have a function that acts on a group of a certain class, that function should not be a member of that class. Instead, it should just be a function not associated with any class. This also fixes your scope problem, as it would then have global scope.

If my argument seems weird, think about it like this. Let's say I have a class called number, as well as a function called print_nums which prints an array of numbers passed to it. Would I make print_nums a global function, or a member of the class number? The first one, right? The second one, although it would work, just doesn't really make sense.

paper man
  • 488
  • 5
  • 19