2

I've furthered in the realization of my save function and got the idea of passing my arguments as "vector" (because they are) instead of "string" which gives this:

void saveFunction(ofstream& save, vector<string> site, vector<string> url, vector<string> username, vector<string> password)
{
    save << site;
    save << url;
    save << username;
    save << password;

}

which gives this error:

error: no match for 'operator<<' (operand types are 'std::ofstream' {aka 'std::basic_ofstream<char>'} and 'std::vector<std::__cxx11::basic_string<char> >')

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Solar
  • 29
  • 5
  • There is non `<<` operator for `std::vector`. If you have a `std::vector` what do you expect getting written into `save`? – Jabberwocky Mar 04 '19 at 14:15
  • What did you expect `save << site;` to do? – melpomene Mar 04 '19 at 14:17
  • I've changed the title so it doesn't look like it's about rvalue references. – Bathsheba Mar 04 '19 at 14:19
  • 1
    so for example `username` is several strings? Misnamed at least, probably misdesigned – sp2danny Mar 04 '19 at 14:19
  • 1
    You need a `struct` with fields `site`, `url`, `username`, `password` and then pass a const reference to vector of that struct, instead of 4 separate vectors – Slava Mar 04 '19 at 14:43
  • @sp2danny "so for example username is several strings?" I think OP wants to dump usernames, passwords, sites and urls. – Slava Mar 04 '19 at 14:45
  • Jabberwocky and melpomene : I expected the program to basically write the vector in a text file, so I can re-use it later, in other parts of my program sp2danny : username is a vector and I hoped that "save << username" would dump the whole vector in a text file. Slava : Thanks a lot, honestly I didn't even know this existed. I chose vectors because I knew that it was possible to target an element in them and add/remove elements as the program goes. I'll investigate more on that. – Solar Mar 04 '19 at 14:57
  • @Solar There would be issues around overloading `<<` for *ForwardIterable* objects. For example, what do you use for a delimiter, and how could you specify that? Perhaps a *manipulator* but what form would that take? Also, what would you write for an empty container? In C++ things don't get implemented unless the specification is watertight. – Bathsheba Mar 04 '19 at 15:01
  • @Slava excuse me, how would I add a new user input to a specific member in a struct? Because from what I saw, struct don't seem to be modifiable. – Solar Mar 04 '19 at 15:06
  • @Solar looks like you need a good textbook, SO is not a replacement for that. – Slava Mar 04 '19 at 15:07
  • @Slava Thanks for the advice, I'm planning to get myself one soon. But in the meantime I want to do something fun. So could you please tell me if it is possible (and if it is how?) to add a new element into a specific struct's member? Again, thanks a lot for all your answers ^^ – Solar Mar 04 '19 at 15:13
  • @Solar Adding new members to existing structs runtime is not possible, C++ is statically typed – sp2danny Mar 04 '19 at 16:25
  • @Solar you just do it different way. If your data structure is dynamic and fields change in runtime then closest thing is `std::map` – Slava Mar 04 '19 at 20:09
  • @Slava Thanks a lot for the advice, in the meantime I started reading the "C++ Primer 5th Edition" and I'm glued to it. Never knew it would make such a difference xD – Solar Mar 06 '19 at 22:22

1 Answers1

3

ofstream does not have an overloaded << operator for a std::vector, so you need to roll one yourself, for example

for (auto&& s : username){
    save << s;
}

although your reasons for using a std::vector might be questionable.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Hi, thanks a lot for the extraordinarily quick answer! However, I don't get how I should use this... (I'm a beginner who started 2 days ago) – Solar Mar 04 '19 at 14:20
  • @Solar: Pleasure, it's how we like to rock and roll on this site. – Bathsheba Mar 04 '19 at 14:20
  • `for (auto&& s : username){` is a range based for loop used to iterate over the container. Look at the examples at the bottom of this: https://en.cppreference.com/w/cpp/language/range-for – drescherjm Mar 04 '19 at 14:31
  • 1
    @Solar As a beginner, you might want to grab a [good book](https://stackoverflow.com/q/388242/1782465) to help your learning. – Angew is no longer proud of SO Mar 04 '19 at 14:34
  • @Bathsheba "move fast and break things" - when It's other peoples things it's all good ;) – UKMonkey Mar 04 '19 at 14:57