-1

Literally just trying to move text data from a 2D vector to a variable.

I spent quite a bit of time searching google and SOF for this and I am shocked, there are hundreds of results but nothing this ...simple.

If this is a dup (and I am sure this is), if a mod could point me to it, I would be grateful.

#include <vector>
#include <string>

void main()
{
    std::string str_hi;
    std::string str_how;
    std::string str_goodbye;

    std::vector<std::vector<std::string>> vec_data
    {
        //    
            { "Hi", "" },
            { "How are you ?", "" },
            { "Good bye :)", "" }
    };

    str_hi = vec_data[0, 0]; // error here; red line under '='
    str_how = vec_data[1, 0]; // error here; red line under '='
    str_goodbye = vec_data[2, 0]; // error here; red line under '='
}```


> error: E0349 no operator "=" matches these operands
> error: E0349 no operator "=" matches these operands
> error: E0349 no operator "=" matches these operands
> 


  • `vec_data[0, 0]` does not do what you think it does. What led you to believe that this is how multi-dimensional vectors are used, in C++? That's not how any C++ textbook explains this. The thing about C++ is that if the syntax for something is unknown, making random guesses will never be productive, and will never work. – Sam Varshavchik Jan 24 '23 at 23:46
  • 2D access is always using `[][]`, not a comma. – Thomas Matthews Jan 24 '23 at 23:46
  • If the reply is helpful, you could click '✔' to mark it as the accepted answer. – Minxin Yu - MSFT Jan 25 '23 at 02:24
  • Related question: [How does the Comma Operator work](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work/) – Philipp Jan 25 '23 at 12:53

1 Answers1

0

You need to change your syntax:

str_hi = vec_data[0][0];

Your version is using the comma operator. Look up "comma operator" in a good C++ text book. Also search the internet for "C++ FAQ Matrix".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154