c++ - vector 类错误

标签 c++ class vector

我正在尝试创建一个程序,它接受文件的输入,然后将单词存储到一个 vector 中,到目前为止,我已经让它与 std::vector 类一起工作,尽管我想尝试使我的拥有,使用。但是,我遇到了很多错误,但不确定如何解决它们。

错误信息:

main.cpp:43: error: expected constructor, destructor, or type conversion before "Vector"
main.cpp:43: error: expected `;' before "Vector"
main.cpp:47: error: expected constructor, destructor, or type conversion before "Vector"
main.cpp:47: error: expected `;' before "Vector"
main.cpp:55: error: expected constructor, destructor, or type conversion before "Vector"
main.cpp:55: error: expected `;' before "Vector"
main.cpp: In function `int main(int, char**)':
main.cpp:115: error: expected primary-expression before "auto"
main.cpp:115: error: expected `;' before "auto"
main.cpp:116: error: expected primary-expression before "auto"
main.cpp:118: error: no matching function for call to `std::vector<WordInfo, std::allocator<WordInfo> >::push_back(std::string&, int)'

我的代码:

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

using namespace std;

struct WordInfo {
    string text;
    int count;
};

template <class T> class Vector {
public:
    typedef T* iterator;
    Vector();
    iterator begin();
    iterator end();
    int size();
    iterator insert(iterator position, const T& item);
    void alloc_new();

private:
    T items[1000];
    int used;
};

template <class T> Vector<T>::Vector() {

    used = 0;
}

template <class T> Vector<T>::iterator Vector<T>::begin() {
    return items;
}

template <class T> Vector<T>::iterator Vector<T>::end() {
    return items + used;
}

template <class T> int Vector<T>::size() {
    return used;
}

template <class T> Vector<T>::iterator Vector<T>::insert(iterator position, const T& item) {

    if (used + 1 > items) {
        alloc_new();
    }

    items[position] = item;
    used = +1;

}

template <class T> void Vector<T>::alloc_new() {
    items = used * 2;
    T tmp[] = items;

    for (int i = 0; i < used; i++) {
        tmp[i] = items[i];
    }
    delete items;
    items = tmp;
}

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

    enum {
        total, unique, individual
    } mode = total;
    for (int c; (c = getopt(argc, argv, "tu")) != -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;
    int count = 0;
    int count2 = 0;
    while (cin >> word) {
        count += 1;

        if (find(list.begin(), list.end(), word) != list.end()) {
            list.push_back(word);

        }

    }


    switch (mode) {
        case total: cout << "Total " << count << endl;
            break;
        case unique: cout << "Unique " << count2 << endl;
            break;
        case individual:
            vector<WordInfo> list;
            while (cin >> word) {
                if (find(list.begin(), list.end(), word) != list.end()) {
                    auto = find(list.begin(), list.end(), word);
                    list.at(auto).count++;
                } else {
                    list.push_back(word, 1);
                }
            }
            break;
    }

    return 0;
}

如有任何帮助,我们将不胜感激,在此先感谢您!

最佳答案

您需要将返回类型限定为类型名称:

template <class T> typename /* <---- THIS */
    Vector<T>::iterator Vector<T>::begin() 
{
    return items;
}

因为迭代器是一个从属名。不过,在类中实现这些方法可能更为惯用(无论如何,模板方法要求定义位于头文件中)。

还有:

                auto = find(list.begin(), list.end(), word);
                list.at(auto).count++;

应该是这样的

                auto match = find(list.begin(), list.end(), word);
                list.at(match - list.begin()).count++;

使用标准库

根据 my comment above ,我建议使用标准库容器做事(至少在尝试实现容器之前)。这是它的样子:

查看 Live On Coliru

#include <iostream>
#include <map>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <getopt.h>

using namespace std;

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;

    map<string, unsigned> tally;

    unsigned long total_count = 0;
    for_each(
            istream_iterator<string>(cin), {}, 
            [&] (std::string const& word) mutable 
            { 
                tally[word]++; 
                total_count++; 
            }
        );

    switch (mode) {
        case total: 
            cout << "Total " << total_count << endl;
            break;
        case unique: 
            cout << "Unique " << tally.size() << endl;
            break;
        case individual:
            for (auto const& entry: tally)
                cout << entry.first << "\t" << entry.second << "\n";
            break;
    }
}

用input.txt

apple pear jumped over the apple moon 
blob the cow blob jumped over the apple moan grumpy dog apple

这是输出:

  • test -t < input.txt

    Total 19
    
  • test -u < input.txt

    Unique 11
    
  • test -i < input.txt

    apple   4
    blob    2
    cow 1
    dog 1
    grumpy  1
    jumped  2
    moan    1
    moon    1
    over    2
    pear    1
    the 3
    

关于c++ - vector 类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19439773/

相关文章:

c++ - 用单词和数字按字母顺序排列字符串

c++ - 如何为 const char* 和 int 重载运算符 +

c++ - 设置一个 2d char 空格数组

java - 类、方法和随机数生成

c++ - static const的C++容器初始化列表导致栈溢出

C++搜索MAX的 vector ,并从第二个 vector 中获得相同的位置

c++ - 为什么 Boost 在 "Program Options"中使用全局函数覆盖来实现自定义验证器

c++ - Qt C++ 拆分类

c++ - 用C++实现二叉搜索树,搜索不起作用。尝试打印节点的元素会导致输出崩溃

C++模板类