C2133:未知大小

时间:2013-01-13 00:44:31

标签: c++ compiler-errors

主要课程

#include "List.h"
#include "Worker.h"
#include <iostream>
#include <string>

using namespace std;
void initWorkerList(List<Worker>);

int main() {
    List<Worker> WorkerList; // Error highlighted here
    initWorkerList(WorkerList);

    string username, password;
    cout << "Please enter your username: " << endl;
    getline(cin, username);
    cout << "Please enter your password: " << endl;
    getline(cin, password);

    Worker w;
    bool success = w.login(username,password, WorkerList);
    if(success) {
             // code

    } else {
        cout << "Invalid username and/or password. \nPlease try again!";
    }

    system("pause");
    return 0; }

void initWorkerList(List<Worker> WorkerList) {
    Worker w1 = Worker("Ben Ang", "Ben123", "pass123", 'M');
    WorkerList.add(w1);
    Worker w2 = Worker("Grace Eng", "Gr4ce", "loveGrace", 'W');
    WorkerList.add(w2);
    Worker w3 = Worker("Rebecca Xuan", "Xuanz", "Rebecca Xuan", 'W');
    WorkerList.add(w3); }

工人阶级

#include <string>
#include "List.h"
using namespace std;
class Worker { private:
    string name;
    string username;
    string password;
    char position; public:
    Worker();
    Worker(string, string, string, char);
    string getName();
    string getUserName();
    string getPassword();
    char getPosition();
    bool login(string, string, List<Worker>); };

Worker::Worker() {   }

Worker::Worker(string n, string un, string pw, char p) {
    name = n;
    username = un;
    password = pw;
    position = p; }

string Worker::getName() {
    return name; }

string Worker::getUserName() {
    return username; }

string Worker::getPassword() {
    return password; }

char Worker::getPosition() {
    return position; }

bool login(string username, string password, List<Worker> WorkerList) {
    string u, pw;
    for(int i =0; i<WorkerList.length(); i++) {
        Worker w = WorkerList.get(i);
        u = w.getUserName();
        pw = w.getPassword();
        if(username == u && password == pw) {
            return true;
        }
    }
    return false; }

列出班级

#include <iostream>
using namespace std;

const int MAX_SIZE = 20;

template <typename ItemType> class List { private:
    ItemType itemList[MAX_SIZE];
    int size; public: 
    List();
    void add(ItemType);
    void del(int index);
    bool isEmpty();
    ItemType get(int);
    int length(); };


template<typename ItemType> List<ItemType>::List() {
    size = 0; }

template<typename ItemType> void List<ItemType>::add(ItemType item) {
    if(size < MAX_SIZE) {
        itemList[size] = item;
        size++; 
    } 
    else {
        cout << "List is full.\n";
    } }

template<typename ItemType> void List<ItemType>::del(int index) {
    if(!isEmpty()) {
        if(index > 0 && index < size) {
            for(int i = index + 1; i <= size; i++) {
                itemList[i-2] = itemList[i-1];
            }
            size--;
        }
    } else {
        cout << "List is empty.\n";
    } }

template<typename ItemType> bool List<ItemType>::isEmpty() {
    return size == 0; }

template<typename ItemType> ItemType List<ItemType>::get(int index) {
    if(index > 0 && index <= size) 
        return itemList[index-1]; }

template<typename ItemType>
int List<ItemType>::length() {
    return size;
}

我有很多错误,但我认为这也是其他错误的原因。

错误11错误C2133:&#39;工人列表&#39; :未知大小

错误可在主要部分找到。我也不知道为什么。以前,它仍然可行,但这很奇怪...... 那有什么问题呢?

3 个答案:

答案 0 :(得分:2)

Worker类中的

login函数是一个免费的函数,你没有实现Worker::login

更改

bool login(string username, string password, List<Worker> WorkerList)

bool Worker::login(string username, string password, List<Worker> WorkerList)

此外,您有多个#include List.h"生成一些错误,表示List.h中的多个定义标识符,惯用的方法是在标头中提供包含警示:

例如:

#ifndef LIST_H
#define LIST_H

#include <iostream>
using namespace std;

#define MAX_SIZE 20

// .... other source code

template<typename ItemType>
int List<ItemType>::length() {
    return size;
}

#endif

答案 1 :(得分:2)

ItemType itemList[MAX_SIZE];

我认为你有问题。请考虑将其替换为动态分配的数组,或将const int MAX_SIZE = 20;替换为#define

答案 2 :(得分:2)

当我向标题添加多个包含保护时,我从g ++(Mac OS X 10.7.5上的G ++ 4.7.1)获得的唯一编译警告是:

$ g++ -O3 -g -I/Users/jleffler/inc -std=c++11 -Wall -Wextra -c so14299529.cpp
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h: In member function ‘ItemType List<ItemType>::get(int) [with ItemType = Worker]’:
List.h:49:35: warning: control reaches end of non-void function [-Wreturn-type]
$

没有标题保护,我会遇到很多错误,例如:

In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:7:11: error: redefinition of ‘const int MAX_SIZE’
In file included from so14299529.cpp:1:0:
List.h:7:11: error: ‘const int MAX_SIZE’ previously defined here
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:9:36: error: redefinition of ‘class List<ItemType>’
In file included from so14299529.cpp:1:0:
List.h:9:36: error: previous definition of ‘class List<ItemType>’
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:20:29: error: redefinition of ‘List<ItemType>::List()’
In file included from so14299529.cpp:1:0:
List.h:20:29: error: ‘List<ItemType>::List()’ previously declared here
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:23:34: error: redefinition of ‘void List<ItemType>::add(ItemType)’
In file included from so14299529.cpp:1:0:
List.h:23:34: error: ‘void List<ItemType>::add(ItemType)’ previously declared here
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:32:34: error: redefinition of ‘void List<ItemType>::del(int)’
In file included from so14299529.cpp:1:0:
List.h:32:34: error: ‘void List<ItemType>::del(int)’ previously declared here
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:44:34: error: redefinition of ‘bool List<ItemType>::isEmpty()’
In file included from so14299529.cpp:1:0:
List.h:44:34: error: ‘bool List<ItemType>::isEmpty()’ previously declared here
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:47:38: error: redefinition of ‘ItemType List<ItemType>::get(int)’
In file included from so14299529.cpp:1:0:
List.h:47:38: error: ‘ItemType List<ItemType>::get(int)’ previously declared here
In file included from Worker.h:6:0,
                 from so14299529.cpp:2:
List.h:52:5: error: redefinition of ‘int List<ItemType>::length()’
In file included from so14299529.cpp:1:0:
List.h:52:5: error: ‘int List<ItemType>::length()’ previously declared here
List.h: In member function ‘ItemType List<ItemType>::get(int) [with ItemType = Worker]’:
List.h:49:35: warning: control reaches end of non-void function [-Wreturn-type]

标题保护是一种简单可靠的技术,可确保标题不会被多次包含,因为这通常会导致出现的问题。

因此,IMO,适当的解决方法是在顶部添加几行,在每个标题的底部添加一行,如下所示。

List.h

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

... Original content of header ...

#endif /* LIST_H_INCLUDED */

Worker.h

#ifndef WORKER_H_INCLUDED
#define WORKER_H_INCLUDED

... Original content of header ...

#endif /* WORKER_H_INCLUDED */

你可以在标题后面发表评论,但没有别的;所有操作代码都应该在标题保护中。