c++增删改查引發了异常: 讀取訪問權限沖突

CSDN問答 2021-12-30 05:48:44 阅读数:67

c++ 增删 删改 异常
#pragma once#include<string>#include<iostream>using namespace std;class employee{public: employee(); virtual void pay(); virtual void display(); virtual void setemployee(); int getno(); void setno(); void setname(); string getname();public: int no; string name; double salary; };#pragma once#include<vector>#include "salesman.h"class management{public: void add(); void decrease(); void revise(); void search();private: int type=0; int l = 0, m = 0,n = 0; int sum1 = 0,sum2 = 0,sum3 = 0; employee em[1000]; manager ma[1000]; salesman sa[1000]; technique te[1000]; };#pragma once#include"technique.h"class manager : public employee{public: manager(); virtual void display(); virtual void setemployee();private: double monthpay;};#include"manager.h"class salesman : public employee{public: salesman(); //virtual void add(); virtual void display(); virtual void setemployee();private: double commrate=0, sales=0;};#pragma once#include"employee.h"class technique : public employee{public: technique(); // virtual void add(); virtual void setemployee(); virtual void display(); private: double hourlyrate; int workhours; };#include <iostream>#include"employee.h"using namespace std;employee::employee() { salary = 0; //no = 0;}void employee::pay() {}void employee::display() {}int employee::getno(){ return no;}void employee::setno(){ cout << "請輸入員工編號:" << endl; cin >> no;}void employee::setname(){ cout << "請輸入員工姓名" << endl; cin >> name;}string employee::getname(){ return name;}#include "management.h"#include"employee.h"#include<iostream>#include<string.h>using namespace std;void employee::setemployee(){}void management::add(){ cout << "輸入本次想增添的員工數" << endl; int a; cin >> a; for (int i=1; i<=a; i++) { employee em; cout << "請輸入本次想增添的第" << i << "個員工的信息" << endl; cout << "請選擇身份:" << endl; cout << "1.經理 2.售貨員 3.技術員" << endl; cin >> type; if (type == 1) { ma[l++].setemployee(); sum1++; } else if (type == 2) { sa[m++].setemployee(); sum2++; } else if (type == 3) { te[n++].setemployee(); sum3++; } } cout << "增加成功!" << endl; system("pause"); system("cls");}void management::decrease(){ int k; cout << "請輸入你想删除的員工編號:" << endl; cin >> k; for (int i = 0; i < sum1; i++) { if (k == ma[i].getno()) { for (int j = i; j < sum1; j++) { ma[i]=ma[i+1]; } cout << "删除成功!\n"; } } for (int i = 0; i < sizeof(sa); i++) { if (k == sa[i].getno()) { for (int j = i; j < sizeof(sa); j++) { sa[i+1] = sa[i]; } cout << "删除成功!\n"; } } for (int i = 0; i < sizeof(te); i++) { if (k == te[i].getno()) { for (int j = i; j < sizeof(te); j++) { te[i] = te[i + 1]; } cout << "删除成功!\n"; } } cout << "無該人員信息!請確認ID是否正確\n"; }void management::revise(){ int k,m,i=0; string n; cout << "請輸入要修改信息的員工編號" << endl; cin >> k; for (int i = 0; i < sizeof(ma); i++) { if (k == ma[i].getno()) { cout << "選擇需要修改的信息 1.姓名 2.編號" << endl; cin >> m; switch (m) { case 1: //cin >> n; ma[i].setname(); ma[i].getname(); break; case 2: //ma[i].setno(stoi(n)); ma[i].setno(); ma[i].getno(); break; } cout << "修改成功"; } }}void management::search(){ int k,m; int i; cout << "請選擇身份:" << endl; cout << "1.經理 2.售貨員 3.技術員" << endl; cin >> m; cout << "請輸入要查找的員工編號:" << endl; cin >> k; switch (k) { case(1): for (i = 0; i < sizeof(ma); i++) { if (k == ma[i].getno()) { ma[i].display(); } } case(2): for (i = 0; i < sizeof(sa); i++) { if (k == sa[i].getno()) { sa[i].display(); } } case(3): for (i = 0; i < sizeof(te); i++) { if (k ==te[i].getno()) { te[i].display(); } } } cout << "查找失敗" << endl; }#include "manager.h"#include "employee.h"manager::manager() :employee(){ monthpay = 0;}void manager::setemployee(){ //cout << "請輸入員工姓名" << endl; //cin >> name; //cout << "請輸入員工編號" << endl; //cin >> no; setname(); setno(); cout << "月工資" << endl; cin>>monthpay ;}void manager::display() { cout << "經理:" << name << ",編號" << manager::no << ",月工資:" << manager::monthpay << endl;}#include "salesman.h"salesman::salesman() :employee(){ }void salesman::setemployee() { cout << "請輸入員工姓名" << endl; cin >> name; cout << "請輸入員工編號" << endl; cin >> no; cout << "月銷售額" << endl; cin >> sales; commrate = 0.04; salary = sales * commrate;}void salesman::display() { cout << "銷售員:" << name << ",編號" << no << ",月工資:" << salary << endl;}#include "technique.h"technique::technique() :employee(){ hourlyrate = 150; workhours = 0;}void technique::setemployee() { cout << "請輸入員工姓名" << endl; cin >> name; cout << "請輸入員工編號" << endl; cin >> no; cout << "請輸入工作時長(以小時為單比特)" << endl; cin >> workhours; salary = hourlyrate * workhours;}void technique::display() { cout << "技術員:" << name << ",編號" << technique::no << ",月工資:" << technique::salary << endl;}#include<stdio.h>#include<conio.h>#include"management.h"void menu(){ cout << "————————————————————————————————" << endl; cout << "———————————歡迎來到員工管理系統———————————" << endl; cout << "——————————————0.退出———————————————" << endl; cout << "————————————1.增添員工信息—————————————" << endl; cout << "————————————2.删除員工信息—————————————" << endl; cout << "————————————3.更改員工信息—————————————" << endl; cout << "————————————4.查找員工信息—————————————" << endl; cout << "————————————————————————————————" << endl; cout << "請輸入相關序號" << endl; }void menu1(){ }void menu();void menu1();int main(){ int k; while (1) { menu(); int k; cin >> k; system("cls"); management a; switch (k) { case 1: //system("cls"); a.add(); //a.print(); system("pause"); system("cls"); break; case 2: a.decrease(); system("pause"); system("cls"); break; case 3: a.revise(); system("pause"); system("cls"); break; case 4: a.search(); system("pause"); system("cls"); break; case 0: cout << "退出" << endl; system("cls"); } } return 0;} 

img


我一運行删除修改功能就報如下錯誤

還有這樣的警告,但是我不會指針,只想用數組

img




采納答案:

no改成this.no


版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://primo.wiki/2021/12/202112281443052717.html