传递矢量的正确方法是什么?

时间:2015-10-16 00:18:11

标签: c++

我在10年级,我们的老师希望我们创建一个原创项目并使用指针

我想要做的是创建会员,并能够按名称对成员进行排序并打印出来

当我运行我的代码时,它说无效访问

Team.h
#ifndef TEAM_H
#define TEAM_H
#include "Staff.h"
#include <vector>
#include <iostream>
using std::vector;

class Team: public Staff
{
    public:
        Team();
        ~Team();
        vector<Staff *> &getVector();
    private:
        vector<Staff *> myStaffs;
};
#endif // TEAM_H
Team.cpp
    Team::Team()
    {
        for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
        {
            myStaffs[iStaff] = createStaff(iStaff);
        }
    }

    vector<Staff*>& Team::getVector()
    {
        return myStaffs;
    }

命令类将对团队进行排序并打印所有团队成员

Command.cpp
void Command::printStaffs(vector<Staff*>&myStaffs)
{
    for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
    {
        std::cout << "Staff ID number: "<<  myStaffs[iStaff]->getStaId()    << std::endl
                  << "Staff Skills 1: " << *myStaffs[iStaff]->getStaSkill() << std::endl
                  << "Staff Skills 2: " << *myStaffs[iStaff]->getStaSkill() << std::endl
                  << "Staff Skills 3: " << *myStaffs[iStaff]->getStaSkill() << std::endl
                  << std::endl;
    }
}
Command.h
#ifndef CommandH
#define CommandH
#include "Team.h"
#include <vector>
#include <iostream>
using std::vector;

class Command: public Team
{
    public:
         Command(){}
        ~Command(){}
        void sortVector(vector<Staff* >&vectorTemp);
        void printStaffs(vector<Staff* >&);
    private:
        vector<Staff *> vectEmployee;
};
//--------------------------------------------------------------------------
#endif
的main.cpp
#include <iostream>
#include <conio.h>
#include "Team.h"
#include "Command.h"


int main()
{

    Team t;
    Command c;

    c.printStaffs(t.getVector());
    getch();
    return 0;
}
Staff.h
#ifndef STAFF_H
#define STAFF_H
#include <cstdlib>
#include <ctime>
#include <string>
using std::rand;
class Staff
{
    public:
        Staff();
        ~Staff();

        static Staff* createStaff(int); // creates staffs
        int** getStaSkill();
        int getStaId();            // returns Staff ID
        static int genRanNum(int); //Generate random number

    private:
        int *staSkill[3];
        int staId;

        //int staDeptAsigned;
};

#endif
Staff.cpp
#include "Staff.h"

Staff::Staff()
{
    *staSkill = new int[3];
}

Staff *Staff::createStaff(int s)
{
    Staff *staff = new Staff();
    staff->staId = s;
    *(staff->staSkill[0]) = genRanNum(10);
    *(staff->staSkill[1]) = genRanNum(10);
    *(staff->staSkill[2]) = genRanNum(10);
    return staff;
}

int** Staff::getStaSkill()
{
    return staSkill;
}

int Staff::getStaId()
{
    return staId;
}

int Staff::genRanNum(int num)
{
    return 1 +(std::rand()%num);
}

Staff::~Staff(){}

1 个答案:

答案 0 :(得分:2)

构造Team时,您有以下构造函数:

Team::Team()
{
    for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
    {
        myStaffs[iStaff] = createStaff(iStaff);
    }
}

但是,myStaffsTeam的成员并获得default constructed as empty,因此myStaffs.size() == 0以来没有任何结果。

在此printStaffs上拨打Team::getVector()会正确通知您vector为空:

int main()
{
    Command c;
    Team t; // t.myStaffs will be empty
    c.printStaffs(t.getVector()); // passes an empty vector to printStaffs
    return 0;
}

可能想要将一个号码传递给Team构造函数来创建那么多员工:

Team::Team(int number_of_staff)
{
    for(unsigned int iStaff = 0; iStaff < number_of_staff; iStaff++)
    {
        myStaffs.push_back(createStaff(iStaff));
    }
}

int main()
{
    Command c;
    Team t(5); // t.myStaffs will contain 5 staff members
    c.printStaffs(t.getVector()); // passes vector of 5 staff
    return 0;
}