非标准语法使用'&'创建指向成员c ++的指针

时间:2017-08-01 14:33:14

标签: c++

我创建了两个向量o3(用于保存字符串中的单词的向量)和o4 ( a vector to hold those vector of words). In the if statement, once ";" has been found in the vector o3 [i] , I want to stop putting words from that o3 [i]`到o4,然后转到下一行在o3举行。我收到错误“非标准语法使用”&'在注释为ERROR的行中创建指向成员c ++的指针。任何帮助都非常感谢。谢谢!

    while (getline(myfile, line, (char)32)) // first read entire line into a 
                                            //string
                                  // problem : this also reads empty lines 
                                 // and gives error 
                                  // while returning words
    {
        abc2.push_back(line); // inserting individual strings into a vector
                             //cout << abc[i] << "\n"; // use this to see 
                             // them as a vector of lines
                             //i++;

    }
 for (int i = 0; i < abc.size(); i++)
    {
        single_line = abc[i];
        if (((single_line[0] >= 'A') && (single_line[0] <= 'Z')) || 
         ((single_line[0] >= 'a') && (single_line[0] <= 'z')))
        {

            if (abc[i] != "")
            {

                o3 = output_words(abc[i], (char)32); // function to separate 
                                                     //words in a line
                int j1 = 0; int j2 = 0;
                while (j2 < o3.size())
                {
                    if (o3[j2] != "" && "\t") // *IMP* require this line to 
                                               // get words
                                              // irrespective of spaces
                    {
                        if (o3[j2].find != ";") // ERROR
                        {
                            o4.resize(i + 1);// NO CLUE WHY IT WORKED WITH 
                                             // i+1 resize???!!!
                            o4[i].push_back(o3[j2]);
                            j2++;
                        }
                        else
                        {
                            j2++;
                        }
                    }
                    else
                    {
                        j2++;
                    }
                }

            }


        }
            else
            {
                o3 = { "" }; // o1 will be null vector (i.e will contain 
                             // nothing inside)
                o4.push_back(o3);
            }


        }

1 个答案:

答案 0 :(得分:1)

表达式o3[j2].find的结果是名为o3[j2]的{​​{1}}成员。然后将该结果与完整表达式find中的字符串文字进行比较。

警告消息似乎暗示,o3[j2].find != ";"是成员函数。在此上下文中,成员函数的名称衰减为成员函数指针。编译器会向您发出警告,因为此类隐式转换根据标准格式不正确,但编译器支持作为语言扩展。标准方法是明确使用地址运算符decltype(o3[j2])::find

将成员函数(指向)与字符串文字进行比较毫无意义。您可能打算调用成员函数。要调用函数,可以添加括号括起来的参数列表:&

假设o3[j2].find(/* arguments */)decltype(o3[j2])(您忘记声明std::string),那么与字符串文字的比较似乎也很可疑。 o3返回找到的子字符串或字符的索引。将整数与字符串文字进行比较也没有任何意义。我建议考虑该行应该做什么。