主班
#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':未知大小
错误在主要部分中找到。我也不知道为什么。以前,它仍然可以工作,但这很奇怪......那么它有什么问题呢?