包含二维char数组的Constructo

时间:2015-03-31 04:44:00

标签: c++ arrays multidimensional-array constructor

我正在尝试制作包含2D数组的第二个构造函数,但我似乎无法使其工作。如果您需要更多信息,我会尽力提供和回答。

Node.h

struct data
{
   string record;
   string ID;
   string name;
   string email;
   string units;
   string major;
   string grade;
   int absent;
   char missed[32][32];
};

class List;


class ListNode
{
   friend class List;
   friend struct data;
   public:
   ListNode();
   //ListNode(data newData);
   ListNode(string newrecord,
            string newID,
            string newname,
            string newemail,
            string newunits,
            string newmajor,
            string newgrade);
   ListNode(string newRecord,
            string newId,
            string newName,
            string newEmail,
            string newUnits,
            string newMajor,
            string newGrade,
            int newAbsent,
            char dates[][32]); // this constructor
   ListNode(ListNode &copyObject);
   ~ListNode();

   data getData() const;
   ListNode *getNextPtr() const;

   ListNode & operator = (ListNode &rhs);

   private:
   data mData;
   ListNode *mpNext;
};

在Node.cpp中

 ListNode::ListNode(string newRecord,
                   string newId,
                   string newName,
                   string newEmail,
                   string newUnits,
                   string newMajor,
                   string newGrade,
                   int newAbsent,
                   char date[32][32] )
{
   mData.record = newRecord;
   mData.ID = newId;
   mData.name=newName;
   mData.email=newEmail;
   mData.units=newUnits;
   mData.major=newMajor;
   mData.grade=newGrade;
   mData.absent=newAbsent;
   memcpy(mData.missed, date, sizeof(date)); //attempt at copying
   //mData.missed = date;


   this->mpNext = NULL;
}

在Linkedlist.cpp中这是错误开始的地方:

ListNode *List::makeNodeM (string newRecord,
                           string newId,
                           string newName,
                           string newEmail,
                           string newUnits,
                           string newMajor,
                           string newGrade,
                           string newAbsent,
                           char dates[32][32])//here and
{
   ListNode *pMem = NULL;
   pMem = new ListNode(newRecord,
                       newId,
                       newName,
                       newEmail,
                       newUnits,
                       newMajor,
                       newGrade,
                       newAbsent,
                       dates); //here    
   return pMem;
}

2 个答案:

答案 0 :(得分:0)

使用

等参数时
char date[32][32] )

编译器忽略第一个维度的大小。就好像你曾经使用过

char date[][32] )

鉴于此,sizeof(date)sizeof(void*)相同。

这意味着,行

memcpy(mData.missed, date, sizeof(date)); //attempt at copying

不会复制您希望的内容。用循环替换该行。

for (int i = 0; i < 32; ++i )
{
    memcpy(mData.missed[i], date[i], sizeof(data[i]));
}

答案 1 :(得分:0)

我认为适用于您的问题的最佳解决方案是Error "An array may not have elements of this type"

只需使用以下

替换Node.cpp中的代码即可
          ListNode::ListNode(string newRecord,
               string newId,
               string newName,
               string newEmail,
               string newUnits,
               string newMajor,
               string newGrade,
               int newAbsent,
               char date[][32] )
             {
            mData.record = newRecord;
            mData.ID = newId;
            mData.name=newName;
            mData.email=newEmail;
            mData.units=newUnits;
            mData.major=newMajor;
              mData.grade=newGrade;
             mData.absent=newAbsent;
             memcpy(mData.missed, date, sizeof(date)); //attempt at copying
           //mData.missed = date;


           this->mpNext = NULL;
     }