vector of a class?

Hello again,
I wish to make a vector of class VItem, so that the vector can store both of the strings "word" and "pos" per space (or whatever the proper term for a vector slot is?)
Is such a thing possible? Or would this only work with structs?


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
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
using namespace std;


class VItem{
friend class Sentence;
public:
// ...

private:
string word;
string pos;

};

VItem VItemclass;



class Sentence{
friend class VItem;

public:	
//...
vector <VItem> translatedVItem; 
//...

private:
string sent;
vector <VItem> vItems;

};



Sentence sentclass;
int main()
{
	sentclass.vItems[0].word= "Hello";
	return 0;
}


I have attempted to create it as such, leading to errors saying (in main) that both vItems and word are private. Is there a way to make this work? If so, can it work while what is currently in private remains in private?
Thank you for your time
No it won't work that way. Private is private for exactly the reason to prevent such a thing you have done from happening

overload the >> operator for your Sentence class
OR
pass the string into the sentence class and have it populate the vector
You could use setters and getters.
1
2
3
4
5
6
7
8
9
class A
{
    int number;
    
public:
    int getNumber() { return number };
    
    void setNumber(int num) { number = num };
};


You don't need to explicitly state the private access specifier, as classes are defaulted to private.
Last edited on
Topic archived. No new replies allowed.