向量和结构的错误

时间:2013-10-18 01:57:54

标签: c++ vector struct

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

错误:

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;
        }

非常感谢任何和所有帮助,谢谢。

3 个答案:

答案 0 :(得分:2)

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

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

如果您使用的是g++,请尝试使用-std=c++11标记进行编译:

g++ -std=c++11 foobar.cpp

答案 1 :(得分:0)

如果您正在使用g ++,请设置-std = c ++ 11以使用auto让编译器根据表达式找出类型。

但是,您可能还想清理代码 - 您不必在if语句中找到一次,然后再在里面找到。

答案 2 :(得分: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声明中。

相关问题