Nested Vectors or ye olde error C2679

Hi,

I'm writing a server software with an onboard database using MS VC++2008 Express. Right now, it's the latter part that has me a bit stumped. I've created a class for a User, and I'm instantiating it through a vector. The problem is that one of the attributes of the class is a vector itself, and therefore its accessor is a vector, too. When I try to use cout to print it out, I get this:

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)


TBH, I'm not even sure that my accessor will do what I want it to do, but at least it compiles if I don't try to call it. Here's my whole code so far:

I've tried calling it with the other accessors, and it works just fine, so it's not a namespace issue.

User.h
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
// Specification file for the "user" class
#ifndef USER_H
#define USER_H

#include <iostream>
#include <stdarg.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "windows.h"
#include <string>
#include <vector>
using namespace std;

class User
{
protected:
	char *userNumber;
	char *userName;
	char *password;
	vector<string> friendList;

public:

	// Default constructor
	User();

	// Constructor with three arguments
	User(char *uNum, char *uName, char *p);

	// Destructor
	~User();

	// Accessors
	char *getUserNumber() const;
	char *getUserName() const;
	char *getPassword() const;
	vector<string> getFriendList();

	// Mutators
	void setUserNumber(char *uNum);
	void setUserName(char *uName);
	void setPassword(char *p);
	void setFriendList(int count, ...);

};

#endif 


User.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Implementation file for the "User" class
#include "User.h"

// Default Constructor
User::User()
{
	userNumber = " ";
	userName = " ";
	password = " ";
}

// Constructor that takes three arguments
User::User(char *uNum, char *uName, char *p)
{
	userNumber = uNum;
	userName = uName;
	password = p;
}

// Destructor
User::~User()
{
}

// Accessors and mutators

//userNumber
char *User::getUserNumber() const
{
	return userNumber;
}

void User::setUserNumber(char *uNum)
{
	userNumber = uNum;
}

// userName
char *User::getUserName() const
{
	return userName;
}

void User::setUserName(char *uName)
{
	userName = uName;
}

// password
char *User::getPassword() const
{
	return password;
}

void User::setPassword(char *p)
{
	password = p;
}

// friendList
vector<string> User::getFriendList()
{
	return friendList;
}

void User::setFriendList(int count, ...)
{
	va_list friends;
	va_start(friends, count);
	for (int i = 0; i < count; i++)
	{
		User::friendList.push_back(va_arg(friends, char *));
	}
	va_end(friends);
}


And the main file
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
// The main file

#include "User.h"

// Function Prototype
void makeDataBase();

int main()
{
	// First create the database
	makeDataBase();

	system("pause");
	return 0;
}

/*********************************************************************
* Definition of function makeDataBase                                *
* This function instantiates a vector of User objects.  In this      *
* way, it can be searched and sorted.  For test purposes, the        *
* vector will be initialized with three addresses.  It will first    *
* call the default constructor, then call each mutator function for  *
* each attribute.  Other portions of this function are temporary for *
* testing the accessors.                                             *
*********************************************************************/
void makeDataBase()
{
	const int NUM_USERS = 3;

	vector<User> userDataBase(NUM_USERS);
	
	for(int count = 0; count < NUM_USERS; count++)
	{
		userDataBase[count] = User();
	}

	userDataBase[0].setUserNumber("000000");
	userDataBase[0].setUserName("Admin");
	userDataBase[0].setPassword("password");
	userDataBase[0].setFriendList(2, "Seymour Butts", "Ben Dover");

	userDataBase[1].setUserNumber("000001");
	userDataBase[1].setUserName("Seymour Butts");
	userDataBase[1].setPassword("booger");
	userDataBase[1].setFriendList(2, "Admin", "Ben Dover");

	userDataBase[2].setUserNumber("000002");
	userDataBase[2].setUserName("Ben Dover");
	userDataBase[2].setPassword("snotty");
	userDataBase[2].setFriendList(2, "Seymour Butts", "Admin");

	for (int count = 0; count < NUM_USERS; count++)
	{
		cout << userDataBase[count].getFriendList() << endl;
	}
}


Does anyone have any suggestions on how to resolve this error? I'm thinking I might need to overload the '<<' operator, but I'm not sure how I'd go about doing that properly. Thank you for your help in advance.
Last edited on
cout does not have a way to print a vector. You should nest another for loop to iterate over each element of the vector you are looking at.
Yes, that worked once I modified my accessor method a bit.

1
2
3
4
5
6
7
8
9
10
11
string User::getFriendList(int i)
{
	return friendList[i];
}

for (int count = 0; count < NUM_USERS; count++)
{
	for(int i = 0; i < 2; i++)
		cout << userDataBase[count].getFriendList(i) << "\t\t";
	cout << endl;
}


Thanks.
Topic archived. No new replies allowed.