1

this is my first post and I just want to start with saying thank you for this community, it has helped me alot of times, you guys are great :)

Unfortunately, I have not found an answer to this specifik question. Although I solved my problem on my own, I do not understand how and why my code works.

So this post is for everyone that had the same problem as me and is looking for an solution. But, it is also a question because I do not understand why the solution works. So mostly I am interested in learning how and why it works so I can implement it in my coding in the future :)

So, this is my code:

#include <iostream>
#include <cstdlib>

using namespace std;

const int number=4;

class Person
{
public:
string name;
int age;
void setInfo(string _name, int _age)
{
    name=_name;
    age=_age;
}
void Print(Person* family, int number) //This is the method I had a problem with
{
    for (int i=0; i<number; i++)
    {
    cout<<family[i].name<<" is "<<family[i].age<<" years old"<<endl;
    }
}
};

int main()
{
Person family[number]; //Creating an array/vector to put my objects in

for (int i=0; i<number; i++) //The user creates people for a family of 4
{

    string name;
    int age;

    cout<<"Name on person number "<<i+1<<": ";
    getline(cin,name);

    cout<<"How old is the person: ";
    cin>>age;
    cout<<endl;
    cin.get();

    family[i].setInfo(name, age); //Here I am using a . and it works just fine
}

family->Print(family, number); //Works!

    //family.Print(family, number); This does not work.  
    //Errormessage: request for member 'Print' in 'family', which is of 
    //non-class type 'Person [4]'

return 0;
}

My code is quite simple, but what I struggle with was to get the call for the method to work.

My question, and what I want to learn, is;

Why do a . not work but a -> does? When do I use the ->? I have never used it before or read about it in my course book. My book has only examples of methods called by single objects and then it uses object.method().

In another words, why does family[i].setInfo(name, age); work but not family.Print(family, number); ? Why do I have to use the -> in this specific case?

The errormessage I get is: request for member 'Print' in 'family', which is of non-class type 'Person [4]' (What does that mean?)

Looking forward reading your answers and to learn a little bit more about C++ :)

Thank you!

Edit: Got some good answers below, but I also found this video that explains the -> operator:

https://thenewboston.com/videos.php?cat=16&video=17518

And this (chapter Pointers to classes):

http://www.cplusplus.com/doc/tutorial/classes/

Also, see this link (posted below in a comment by Alan Stokes) to understand arrays a little bit more:

Is an array name a pointer?

Community
  • 1
  • 1
Elola
  • 21
  • 7
  • It's just because `family` is an array, and arrays name is a pointer to int's first element. So basicly `family->setInfo()` is the same as `(&family[0])->setinfo()`. – sheikh_anton May 19 '16 at 22:03
  • You'd be much better off with `std::vector`; fewer sharp edges. – Alan Stokes May 19 '16 at 22:05

3 Answers3

1

Your family->Print(family, number) worked just because family is an array, and arrays name acts as a pointer to its's first element. So basicly family->setInfo() is the same as (&family[0])->setinfo(). But as your compiler said, family itself is an array (or the pointer to it's first element), and not a Person, so it has no such member function setInfo().

sheikh_anton
  • 3,422
  • 2
  • 15
  • 23
0

family->Print means the same as family[0].Print.

The problem is that your Print should not be a non-static member function. It doesn't use any member variables, it only operates on the arguments you pass in.

Simplest solution is to move void Print(Person* family, int number) {....} outside of the class definition of Person.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Absolutely, but my assignment/teacher specifically asked us to create the Print-function inside the class. – Elola May 19 '16 at 22:18
  • @Elola perhaps you are supposed to make the `Print` function have no arguments and only print that member, and then you call it in a loop. The way you have it now is bad design. Another option would be to make it `static` – M.M May 19 '16 at 22:22
  • How do I do it without arguments? The method has to print all the objects inside the class to create a list of all the family members that the user entered. – Elola May 19 '16 at 22:28
  • Maybe you misunderstood the requirements – M.M May 19 '16 at 22:58
  • 1
    I think I now understand what you meant and did this instead (took away the arguments). `void Print() { cout< – Elola May 19 '16 at 23:08
0

The name of an array (in your case "family") is always a pointer to the first element of that array.

O Moya
  • 46
  • 1