c++ - C2133 : Unknown size

标签 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); }

worker 类(Class)

#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 error C2133: 'WorkerList': 未知大小

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

最佳答案

ItemType itemList[MAX_SIZE];

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

关于c++ - C2133 : Unknown size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14299529/

相关文章:

c++ - 如何将 SDL_Texture 与 SDL_Renderer 解耦?

c++ - 了解 '&' 运算符

c++ - AVL树的插入方法?

c++ - 未能绑定(bind)到转发引用

android - 膨胀类 android.support.design.internal.BottomNavigationView 时出错

html - scss css编译在以下css上抛出错误

c++ - "Undefined reference to"模板类构造函数

c++ - 二叉搜索树赋值运算符(递归)

从 char * 到 unsigned char 的 C++ 转换?

opencv - fatal error : opencv2/xfeatures2d. hpp:没有这样的文件或目录