我的代码出了什么问题?

时间:2014-03-07 20:28:50

标签: c++

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

string flipwords(string str) {

    string rword = "";
    vector<string> wrd;
    for (int i = 0; i < str.length(); ++i)
        wrd.push_back(str.substr(i,1));
    reverse(str.begin(), str.end());
    for (int = 0; i < str.size(); ++i)
        rword += str[i];
    return rword;
}

int main()
{
    cout << "Enter a word: ";
    string word;
    cin >> word;
    string rword = flipwords(word);

    if (word == rword ) {
        cout << "It's a palindrome.";

    } else {
        cout << "Not a palindrome.";

    }

    return 0;
}

当我尝试运行此代码时,它根本不起作用,我完全从InfiniteSkills的教程中复制它,代码适用于视频,但在我的电脑上却没有!

编译器给出了以下错误:

F:\apsat\Vectors Project\main.cpp|13|error: expected unqualified-id before '=' token|
F:\apsat\Vectors Project\main.cpp|13|error: expected ';' before '=' token|
F:\apsat\Vectors Project\main.cpp|13|error: expected primary-expression before '=' token|
F:\apsat\Vectors Project\main.cpp|13|error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]|

如果有帮助,我正在使用Code :: Blocks 13.12和MinGW。

1 个答案:

答案 0 :(得分:5)

这一行就在这里:

for (int = 0; i < str.size(); ++i)

应该是:

for (int i = 0; i < str.size(); ++i)