Vector of objects

Hello, I try to make vector of objects, can you see what is wrong with my code?
Thanks for help!

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
43
44
45
46
47
  #include <iostream>
#include <vector>

using namespace std;

class Person
{
  public:
      char name[20];
      char nationatlity[30];
      unsigned int height;
      unsigned int weight;  
      unsigned int age;

      void put()
      {
          cin>>name;
          cin>>nationality;
          cin>>height;
          cin>>weight;
          cin>age;
      }
      void print()
      {
          cout<<name<<endl;
          cout<<nationality<<endl;
          cout<<height<<endl;
          cout<<weight<<endl;
          cout<<age<<endl;
      }


};

int main()
{
  Person human;
 
  vector <Person> array;
   for(int i=0; i<10; i++)
   {
       array.push_back(human.put());

   }

    return 0;
}
Line 42: You're calling human.put() inside push_back(). push_back is expecting a Person object. human.put returns a void. You can't do a push_back() of a void.

39
40
41
42
43
vector <Person> array;
   for(int i=0; i<10; i++)
   {   human.put();  // Get the data 
       array.push_back(human);
   }


Or as an alternative, you could change put to return a reference.
15
16
17
18
19
20
Person & put ()
{ ...
   return *this;
}
...
    array.push_back(human.put());
Last edited on
"put" is a strange name for a method that "gets" things.
Hello asxxx,

As I worked with your program I came up with these questions:

Do you have to use a C style character array for "name" and "nationality"? Could you use a "std::string"?

Is "name" and "nationality" comprised of a single word or could it contain a space? Your formatted input would stop at the first space and what is stored in these variables would not be correct. Using some form of "getline()" would be a better choice for these two variables. Also a "std::string" would help.

Check the spelling of "nationality". You have it spelled two different ways in your program. Neither of which I believe is the correct spelling.

Do you really need an "unsigned int" for "height" and "weight"? And are these values in English or metric? Your entry may contain a decimal number, but any type of "int" will only store the whole number and drop the decimal part. Also calculations may not come out right using "int"s.

This may fix your problem, but still may not be the best way of ysing what you have.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>

using namespace std;

class Person
{
	char name[20];
	char nationatlity[30];  // <--- Check spelling.
	unsigned int height; // <--- "unsigned int" works, but you could also use "size_t", alais for "unsigned int".
	unsigned int weight;
	unsigned int age;

	public:
		void GetInfo()
		{
			// <--- Each "cin" needs a prompt.
			cin >> name;
			cin >> nationatlity; // <--- Watch spelling.
			cin >> height;
			cin >> weight;
			cin >> age; // <--- Syntax error
		}

		void print()
		{
			//cout << name << '\n'; // <--- Not every line needs an "endl". The new line works just as well.
			//cout << nationatlity << '\n'; // <--- Watch spelling.
			//cout << height << '\n';
			//cout << weight << '\n';
			//cout << age << endl;

			// <--- An alternative.
			cout
				<< name << '\n'
				<< nationatlity << '\n'
				<< height << '\n'
				<< weight << '\n'
				<< age << endl;
		}
};

int main()
{
	Person human;

	vector <Person> array;

	for (int i = 0; i < 10; i++)
	{
		human.GetInfo();

		array.push_back(human);

		human.print();

		std::cout << std::endl; // <--- Used as a break point for testing.
	}

	return 0;
}


Andy
Please don't use methods to get C-strings that don't limit the number characters they will accept into the C-string. The extraction operator is very dangerous when working with C-strings, and don't forget it doesn't allow whitespace characters.

A better alternative is to use getline(), or use the setw() manipulator prior to the C-string.

edit:

However probably the best alternative is to use std::string instead of the error prone C-strings.

Last edited on
Thank you very much for help everyone.
Topic archived. No new replies allowed.