问题描述
- 【初学者跪地求助】一个关于类中vector类型调用的问题。
-
一个项目管理的程序,现在在写输出项目中任务列表的功能。
getTaskList()函数中似乎总是无法调用project类中的 任务列表vector
程序运行就会崩溃
C++初学者求帮助啊!数据已经由initData()函数初始化
有两个包含任务列表的容器
还有一个包含项目列表的容器#include
#include
#include
using namespace std;class Time
{
public:
Time()
{
hour = 0;
minute = 0;
sec = 0;
}
void set_time(int h,int m,int s);
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void Time::show_time()
{
cout << hour << ":" << minute << ":" << sec << endl;
}class task{
protected:
string id,releaserId;
Time releaseTime;
int type;
bool state;
string title;Time finishTime; public: task(string i,string r,Time re,int t,bool s,string ti,string f,Time fi); void showTaskInfo(); string finisherId;
};
task::task(string i,string r,Time re,int t,bool s,string ti,string f,Time fi){
id=i;
releaserId=r;
releaseTime=re;
type=t;
state=s;
title=ti;
finisherId=f;
finishTime=fi;
}void task::showTaskInfo(){
cout<<"ID:"<<id<<endl;
cout<<"发布者ID:"<<releaserId<<endl;
cout<<"创建时间:";
releaseTime.show_time();
cout<<"任务类型:";
if(type==0){
cout<<"普通任务"<<endl;
}
else if(type==1){
cout<<"私密任务"<<endl;
}
else if(type==2){
cout<<"指派任务"<<endl;
}
cout<<"任务状态:";
if(state==0){
cout<<"已发布"<<endl;
}
else{
cout<<"已完成"<<endl;
}
cout<<"题目:"<<title<<endl;
if(state==1){
cout<<"完成者ID:"<<finisherId<<endl;
cout<<"完成时间:";finishTime.show_time();
}
}class proj{
protected:
string id,releaserId;
Time releaseTime;
vector vecTaskList;
string title;
public:
proj(string,string,Time,vector,string);
void getTaskList();
};proj::proj(string i,string r,Time t,vector v,string ti){
id=i;
releaserId=r;
releaseTime=t;
vecTaskList=v;
title=ti;
}void proj::getTaskList(){
/* for(int i=0;i<vecTaskList.size();i++){vecTaskList[i].showTaskInfo();
} */
cout<<vecTaskList[0].finisherId;
cout<<"00000";
}vector vecProj1;
vector vecProj2;
vector vecProj;void initData(){
Time time1,time2,time3,time4,time5,time6;
time1.set_time(8,36,53);
time2.set_time(9,32,40);
time3.set_time(12,38,56);
time4.set_time(11,12,23);
time5.set_time(12,06,10);
time6.set_time(00,00,00);
task task1("001","Eacials",time1,0,0,"A","NULL",time6);
task task2("002","Eacials",time1,1,1,"B","Anne",time3);
task task3("003","Eacials",time1,2,0,"C","NULL",time6);
task task4("004","hs",time2,0,1,"D","Ben",time4);
task task5("005","hs",time2,1,0,"E","NULL",time6);
task task6("006","hs",time2,2,1,"F","Justin",time5);
proj proj1("001","Eacials",time1,vecProj1,"IMP");
proj proj2("002","hs",time2,vecProj2,"NEC");vecProj1.push_back(task1); vecProj1.push_back(task2); vecProj1.push_back(task3); vecProj2.push_back(task4); vecProj2.push_back(task5); vecProj2.push_back(task6); vecProj.push_back(proj1); vecProj.push_back(proj2); proj1.getTaskList();
}
main(){
initData();vecProj[0].getTaskList(); system("pause"); return 0;
}
解决方案
虽然我不知道你的程序运行后报什么错误!但是从你的main函数来看,你的vecProj[0]在哪里定义了?你initData()初始化了数据,但是这个函数里定义了task,定义了vecProj1,vecProj2,但这些变量的作用范围都只是在initData,而不是整个工程!所以你应该定义在initData()之前,作为全局变量,然后再initData()里进行初始化,最后再输出向量vecProj中的值!