0

所以我决定尝试只使用向量类来制作我在上一个问题中编写的程序,但我仍然遇到错误。

错误:

error: 'match' does not name a type
error: 'match' was not declared in the scope
error: 'conversion from 'int' to non-scalar type 'WordInfo' requested 

我的代码:

 #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include <algorithm>


    using namespace std;

    struct WordInfo {
        string text;
        int count;
    };

    int main(int argc, char** argv) {

        enum {
            total, unique, individual
        } mode = total;
        for (int c; (c = getopt(argc, argv, "tui")) != -1;) {
            switch (c) {
                case 't': mode = total;
                    break;
                case 'u': mode = unique;
                    break;
                case 'i': mode = individual;
                    break;

            }
        }
        argc -= optind;
        argv += optind;
        string word;
        vector<string> list;
        vector<WordInfo> words;
        int count = 0;
        while (cin >> word) {

            switch(mode){
                case total : 
                    count += 1;
                    break;
                case unique :
                    if (find(list.begin(), list.end(), word) != list.end()){
                    } else {
                      count += 1; 
                    }
                    break;
                case individual :
                    if (find(list.begin(), list.end(), word) != list.end()) {
                        auto match = find(list.begin(), list.end(), word);
                        words.at(match - list.begin()).count++;
                    } else {
                        int count = 1;
                        WordInfo  tmp = (word, i);
                        words.push_back(tmp);
                    }
                    }    
        }


        switch (mode) {
            case total: cout << "Total " << count << endl;
                break;
            case unique: cout << "Unique " << count << endl;
                break;
            case individual: 
                for (int i = 0; i < words.size(); i++){
                    cout << words.at(i); << endl;
                }
                break;
        }

        return 0;
        }

任何和所有的帮助将不胜感激,谢谢。

4

3 回答 3

2

你确定你使用的是 c++11 吗?

auto与早期的编译器具有完全不同的含义。

如果您正在使用g++,请尝试使用以下-std=c++11标志进行编译:

g++ -std=c++11 foobar.cpp
于 2013-10-18T02:01:09.747 回答
0

如果您使用的是 g++,请设置 -std=c++11 以使用 auto 让编译器根据表达式确定类型。

但是,您可能还想清理您的代码 - 您不必在 if 语句中查找一次,然后在内部再次查找。

于 2013-10-18T02:05:22.090 回答
0

有一些语法错误:

test.cpp:54:44: error: use of undeclared identifier 'i'
                WordInfo  tmp = (word, i);
                                       ^

test.cpp:68:31: error: invalid operands to binary expression ('ostream' (aka 

'basic_ostream<char>') and 'WordInfo')

                     cout << words.at(i); << endl;
                                        ^

看,你使用了未定义的变量 i 和一个额外的 ; 在 cout 声明中。

于 2013-10-18T02:09:12.593 回答