Reputation: 21
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:
Upvotes: 1
Views: 502
Reputation: 46
The name of an array (in your case "family") is always a pointer to the first element of that array.
Upvotes: 0
Reputation: 141554
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
.
Upvotes: 0
Reputation: 3452
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()
.
Upvotes: 1