c++ - Assinging pointer to string variable fails -
match give_word( const vector< vector<string> > * source_words, const vector< vector<string> > * target_words, ...) { string synonym1, synonym2; ... // choose synonyms (if available) synonym1=source_words[index][rand()%source_words[index].size()]; synonym2=target_words[index][rand()%target_words[index].size()]; ...
i've made technical decision pass vector-in-vector objects pointers because don't want them copied , passed function. it's because vectors hold more 1000 strings.
but don't understand why compile error @ 2 lines assignment sign (=)
synonym1=source_words[index][rand()%source_words[index].size()]; synonym2=target_words[index][rand()%target_words[index].size()];
it says this: no operator "=" matches these operands
synonym1 = source_words[index][rand() % source_words[index].size()]; synonym2 = target_words[index][rand() % target_words[index].size()];
would valid code if used references, which should have.
as pointers, you'll need this:
synonym1 = (*source_words)[index][rand() % (*source_words)[index].size()]; synonym2 = (*target_words)[index][rand() % (*target_words)[index].size()];
that's called dereferencing (parentheses proper operator associativity).
without it, pointer canceled out [index]
(ub if index != 0
), luckily, doesn't compile, because end assigning std::vector<std::string>
std::string
.
similarly, source_words[index].size()
, you're getting size of std::vector<std::vector<std::string>>
, not std::vector<std::string>
.
Comments
Post a Comment