How can I make a vector of objects then print it's contents out?

The program I am making I wish to choose 2 values for an object, and then add them to a vector , my problem is I can't print them out through a function

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
class x {

public:
	x() {}

	x(int x, int y)
	{
		std::cout << x << " " << y << " ";
	}
	void print_vect(std::vector<x> vect)
	{
		for (int i = 0; i < vect.size(); i++)
		{
			std::cout << vect[i] << " ";
		}
	}

};


int main()
{
	
	std::vector<x> v;
	v.reserve(7);

	x obj(3,6);
	for (unsigned i = 0; i < 6; i++) {
		v.push_back(obj);
		v.print_vect();
	}
	return 0;
}

I'm getting a bunch of errors and nothing works. Please excuse my rusty class knowledge.
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
#include <iostream>
#include <vector>

class x {
public:
	x() {}

	x(int x, int y)
	{
		std::cout << x << " " << y << " ";
	}
	void print_vect(std::vector<x> vect)
	{
		for (int i = 0; i < vect.size(); i++)
		{
			std::cout << vect[i] << " ";
		}
	}
};

int main()
{
	std::vector<x> v;
	v.reserve(7);

	x obj(3,6);
	for (unsigned i = 0; i < 6; i++) {
		v.push_back(obj);
		v.print_vect();
	}
	return 0;
}

 In member function 'void x::print_vect(std::vector<x>)':
14:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
16:23: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

 In function 'int main()':
29:5: error: 'class std::vector<x>' has no member named 'print_vect'

Looks like two errors.

Line 29. v.print_vect();
The v is a vector. There is no vector::print_vect().

The 'v' has elements. Each element has member void x::print_vect(std::vector<x>)
However, calling v[i].print_vect(); would be an error too, because there is no void x::print_vect(std::vector<x>). Which vector do you want to pass to the function?


Line 16: std::cout << vect[i]
The type of vect[i] is x. what do you want to happen in:
1
2
x obj(3,6);
std::cout << obj;
Line 14: You're trying to print an object of type x. Your class doesn't know how to do that.
On top of that, your class doesn't contain any data to be printed.

Line 27: You construct obj with 3 and 6. Those values are printed by the constructor, but not saved.

Line 30, you call print_vect, but don't pass an argument. print_vect requires one argument.

The traditional way to print an object is using a friend function:
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
#include <vector>
#include <iostream>
using namespace std;

class x 
{	int		x1;
	int		y1;

public:
	x() 
	{	x1 = 0;
		y1 = 0;
	}

	x(int x, int y)
	{	x1 = x;
		y1 = y;
		std::cout << x1 << " " << y1 << endl;
	}
	friend ostream& operator << (ostream& os, const x& obj)
	{
		std::cout << obj.x1 << " " << obj.y1;
		return os;
	}
};

int main()
{	std::vector<x> v;
	v.reserve(7);

	x obj(3, 6);
	for (unsigned i = 0; i < 6; i++) {
		v.push_back(obj);
		cout << obj << endl;
	}
	return 0;
}
Last edited on
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
#include <iostream>
#include <vector>

class x {
    
    int a,b;

public:

	x() {
	    a=0;
	     b=0;
	    }

	x(int x, int y)
	{
		a = x;
		b = y;
	}
	
	void print_vect()
	{		
			std::cout << a << " " << b << " " << std::endl;	
	}

};


int main()
{
	
	std::vector<x> v;
	v.reserve(7);

	x obj(3,6);
	for (unsigned i = 0; i < 6; i++) {
		v.push_back(obj);
		v[i].print_vect();
	}
	return 0;
}
Topic archived. No new replies allowed.