0

出于某种原因,编译器不允许我从我创建的映射中检索整数向量,我希望能够用新向量覆盖这个向量。编译器给我的错误是荒谬的。谢谢你的帮助!!

编译器不喜欢我的这部分代码:

line_num = miss_words[word_1];

错误:

[Wawiti@localhost Lab2]$ g++ -g -Wall *.cpp -o lab2
main.cpp: In function ‘int main(int, char**)’:
main.cpp:156:49: error: no match for ‘operator=’ in ‘miss_words.std::map<_Key, _Tp, _Compare, _Alloc>::operator[]<std::basic_string<char>, std::vector<int>, std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, std::vector<int> > > >((*(const key_type*)(& word_1))) = line_num.std::vector<_Tp, _Alloc>::push_back<int, std::allocator<int> >((*(const value_type*)(& line)))’
main.cpp:156:49: note: candidate is:
In file included from /usr/lib/gcc/x86_64-redhat->linux/4.7.2/../../../../include/c++/4.7.2vector:70:0,
                 from header.h:19,
                 from main.cpp:15:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/vector.tcc:161:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/vector.tcc:161:5: note:   no known conversion for argument 1 from ‘void’ to ‘const std::vector<int>&’

代码:

map<string, vector<int> > miss_words;       // Creates a map for misspelled words
string word_1;                  // String for word;
string sentence;                // To store each line;
vector<int> line_num;               // To store line numbers

ifstream file;                  // Opens file to be spell checked
file.open(argv[2]);

int line = 1;
while(getline(file, sentence))          // Reads in file sentence by sentence
{
    sentence=remove_punct(sentence);    // Removes punctuation from sentence
    stringstream pars_sentence;     // Creates stringstream
    pars_sentence << sentence;      // Places sentence in a stringstream
    while(pars_sentence >> word_1)      // Picks apart sentence word by word
    {
        if(dictionary.find(word_1)==dictionary.end())
        {
            line_num = miss_words[word_1]; //Compiler doesn't like this
            miss_words[word_1] = line_num.push_back(line);
        }       
    }
line++;                     // Increments line marker
}
4

3 回答 3

1

不,编译器在您的想法之后抱怨该行:

miss_words[word_1] = line_num.push_back(line);

函数std::vector::push_back()返回void。您不能将其分配给vector.

与其复制你的向量,推入一个值,然后把它复制回去,你为什么不直接推入它:

miss_words[word_1].push_back(line);
于 2013-07-02T22:52:20.967 回答
0
line_num = miss_words[word_1]; // use assignment operator to assign value at key word_1 to line_num
miss_words[word_1] = line_num.push_back(line); // trying to reset the value at key word_1

正如 RyanMcK 所说, push_back 返回 void。

重写上面两行(更简单,正确,并且您没有使用赋值运算符复制任何数据):

miss_words[word_1].push_back(line) // directly access the value at key word_1

这是因为 std::map::operator[] 返回对映射值的引用。

资料来源:

http://www.cplusplus.com/reference/vector/vector/push_back/

http://www.cplusplus.com/reference/map/map/operator[]/

于 2013-07-02T23:01:35.020 回答
0

您正在制作矢量的副本,这是不必要的。

line_num = miss_words[word_1];

应该是:

std::vector<int> &v = miss_words[word_1];
v.push_back(line);

这也应该解决 RyanMck 指出的相同问题。

于 2013-07-02T22:51:50.963 回答