Vectors in Classes?

Every time I try to compile this code, I get an error. What am I doing wrong?...

1
2
3
4
5
Severity	Code	Description	Project	File	Line
Error	C2064	term does not evaluate to a function taking 2 arguments	The Learning Process	c:\users\jim\documents\visual studio 2015\projects\the learning process\the learning process\the learning process.cpp	16

Error (active)		call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type	The Learning Process	c:\Users\Jim\Documents\Visual Studio 2015\Projects\The Learning Process\The Learning Process\The Learning Process.cpp	16


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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

class Items
{
public:
	std::vector<std::string> inventory;
	Items();
};

Items::Items()
{
	inventory(5, "nothing"); //this is where the errors are
}

int main()
{
	Items item_object_use;
	std::cout << "Here are the items in your inventory:";
	for (unsigned int inv = 0, invNum = 1; inv < item_object_use.inventory.size(); inv++, invNum++)
	{
		std::cout << '\n' << '\t' << item_object_use.inventory[inv] << std::endl;
	}
return 0;
}
Last edited on
Line 16: You're trying to invoke vector's fill constructor
See constructor #2 here:
http://www.cplusplus.com/reference/vector/vector/vector/

However, inventory's default constructor was already invoked at line 10:

Try this instead:
14
15
Items::Items() : inventory(5, "nothing")
{}

If I wanted to define more vectors within the same class, how would I do that?
Same way. Multiple initializers are separated by commas.

1
2
Items::Items() : v1 (5, "nothing") , v2 (2, "whatever") , v3 (10, "magic")
{} 








Topic archived. No new replies allowed.