-4

I am trying to create a string array to hold the strings generated by a void function. I am very confused about if or how I can do this. The below code is giving me an error about "no suitable constructor."

getWords("theFile.dat");     // Function to extract list of strings from file       
string wordsList[] = {getWords("theFile.dat")};    // Add strings from function to array
  • If it's a `void` function, how does it generate strings? – Barry Jul 20 '16 at 16:59
  • It uses cout. This is a function given by another person I am working with, so I am not able to change it from a void function – Bill Weiler Jul 20 '16 at 17:01
  • 2
    Then you can't use it to initialize a list of strings. – Benjamin Lindley Jul 20 '16 at 17:03
  • 1
    `cout` prints on the console. You will have to make the function return the string somehow (or use some stream trickery to catch whatever the function puts into `cout`) – 463035818_is_not_an_ai Jul 20 '16 at 17:03
  • @tobi303 What "stream trickery" options are there? Like I said I am not able to modify the void function so any changes would have to be outside of the function itself. – Bill Weiler Jul 20 '16 at 17:05
  • 1
    Why are you obligating yourself into using someone else's poorly designed function? Why don't you instead design a good function? – Benjamin Lindley Jul 20 '16 at 17:07
  • @BenjaminLindley I wish I could but convention disallows me. Unfortunately, I have to work with the function given, for better or worse. – Bill Weiler Jul 20 '16 at 17:08
  • actually I dont know how to do it, but as anything is possible, I would expect that also this is doable somehow. – 463035818_is_not_an_ai Jul 20 '16 at 17:13
  • this might help : http://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files – 463035818_is_not_an_ai Jul 20 '16 at 17:15
  • 1
    *I wish I could but convention disallows me* -- You've been given a trick question, and your coworkers are all laughing at you. And I don't think I'm being all that sarcastic. – PaulMcKenzie Jul 20 '16 at 17:23
  • "I wish I could but convention disallows me" - Well, sometimes you just have to say "to hell with conventions/rules - I'm doing this *right* and accept the consequences" - professional pride *is* a thing... – Jesper Juhl Jul 20 '16 at 17:25
  • one solution would be to do `#define cout return` call the function and then undef it again. ps: Dont do it ! – 463035818_is_not_an_ai Jul 20 '16 at 17:26
  • @tobi303 Noooooo! *Please* don't. – Jesper Juhl Jul 20 '16 at 17:31
  • @tobi303: You'd have to do that before and after the function definition to have any effect. However, it wouldn't have a useful effect, and would almost certainly result in an error on a line that looked something like `return << word << endl;`, or `std::return << word << std::endl;` – Benjamin Lindley Jul 20 '16 at 17:34
  • @BenjaminLindley it wasnt meant that serious, but actually yes I didnt realize that it wont work at all... – 463035818_is_not_an_ai Jul 20 '16 at 17:35
  • @BillWeiler Are you allowed to change the function's prototype to add a second argument that denotes the stream to write to? – PaulMcKenzie Jul 20 '16 at 17:36

1 Answers1

3

If (as your comment states) the code uses cout to print the strings, then you have no way to capture them and put them in your array. Well, ok, you might be able to, by doing some major/ugly hack that grabs hold of the stdout file descriptor and reads what is written to it, but that would just be ugly beyond words (and most likely would need different platform specific implementations). Just don't go there.

Write your own good function to read the file and return what you need or fix the crap function you've been given.

There's got to be a limit to how much crud (with the risk of introducing more crap/bugs) one has to add to work around broken crap code before fixing or re-implementing/re-factoring it.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • completely agree. I would take it as a fun challenge to get it working somehow, but in any serious code I would simply throw that function into the garbage. – 463035818_is_not_an_ai Jul 20 '16 at 17:29