0

主班

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

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

4

3 回答 3

2
ItemType itemList[MAX_SIZE];

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

于 2013-01-13T01:55:46.690 回答
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,适当的解决方法是在每个标题的顶部添加几行,在底部添加一行,如下所示。

列表.h

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

... Original content of header ...

#endif /* LIST_H_INCLUDED */

工人.h

#ifndef WORKER_H_INCLUDED
#define WORKER_H_INCLUDED

... Original content of header ...

#endif /* WORKER_H_INCLUDED */

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

于 2013-01-13T02:00:23.937 回答
2

loginWorker 类中的函数是一个独立的函数,你没有实现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
于 2013-01-13T01:53:17.623 回答