1

我试图在字符串向量中找到一个 2 部分名称,我有我认为可行的方法,但它没有。我不能使用迭代器。这是我的代码,它是一个更大程序的一部分,它是一个有 5 个函数的程序中的一个函数。

int deletenames()
{
    ifstream inFile;
    ofstream outFile;
    string strFileName;
    string strFName,strLName;
    vector<string> vecStudent;
    string strDFName, strDLName;
    int i=0;
    char line[80];
    //delets a name
    cout << endl<< " Enter a name to be deleted( First and Last Name):";
    cin>>strDFName>>strDLName;

    for(i<0;strDFName+ " "+strDLName=strFName+ " "+strLName; i++)
    {
        if(strDFName+ " "+strDLName=strFName+ " "+strLName)
            {
                vecStudent.erase;
                cout << "Student Deleted";
        }
    }

    // open output file for writing
    outFile.open(strFileName.c_str());
    if ( outFile.fail())
    {
        cout<<" Output file error! Student was not added"<<endl;
        return -1;
    }

    //display the content of the vector
    for(int i=0; i<vecStudent.size(); i++)
        cout<< vecStudent[i]<<endl;

    for(int i=0; i<vecStudent.size();i++)
        outFile<<vecStudent[i]<<endl;
    outFile.close();
return 0;
}

我从用于编译它的 Microsoft Visual Studio 收到以下错误消息。

main.cpp
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(78): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(64): warning C4101: 'line' : unreferenced local variable
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(105): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(108): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(90): warning C4101: 'line' : unreferenced local variable
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(132): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(135): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(122): warning C4101: 'line' : unreferenced local variable
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(158): warning C4552: '<' : operator has no effect; expected operator with side-effect
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(158): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(160): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(162): error C3867: 'std::vector<_Ty>::erase': function call missing argument list; use '&std::vector<_Ty>::erase' to create a pointer to member
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(179): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(182): warning C4018: '<' : signed/unsigned mismatch
4

1 回答 1

0

似乎可以肯定,至少有一个(可能是两个)位置(x; a=b; a++),或者if (a=b),您实际上打算进行比较而不是分配(即,您打算使用==而不是=)。

特别是,在这两种情况下,左侧都是通过将一些字符串连接在一起而构建的临时值。一般来说,分配给临时人员是不可能的。即使有可能,这通常也是一个错误。

顺便说一句,您似乎还复制了代码来显示数组的内容(即,您有该代码的两个副本)。此外,除非这是您必须“手动”完成任务的任务,否则您可能希望使用std::find而不是编写循环来查找字符串。

于 2012-04-08T02:56:09.240 回答