包含 2D 字符数组的构造函数
Posted
技术标签:
【中文标题】包含 2D 字符数组的构造函数【英文标题】:Constructo containing a 2D array of chars 【发布时间】:2015-03-31 04:43:59 【问题描述】:我正在尝试创建第二个包含二维数组的构造函数,但我似乎无法让它工作。如果您需要更多信息,尽管问,我会尽力提供和回答。
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 ©Object);
~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;
【问题讨论】:
什么二维数组?二维字符串数组? @Abhi 这是一个二维字符数组。它是[32][32] 报错了吗? @YasirMajeed 是的,我将 cmets 放在发生错误的代码中。 IntelliSense:没有构造函数“ListNode::ListNode”的实例与参数列表参数类型匹配:(std::string, std::string, std::string, std::string, std::string, std::string , std::string, std::string, char (*)[32] 只需将 char dates[32][32] 替换为 char dates[][]。 【参考方案1】:当您使用诸如
之类的参数时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]));
【讨论】:
除此之外你知道为什么它给我 Linkedlist.cpp 中的语法错误 @Montop20,我无法回答这个问题,因为您没有发布List
类的定义。您只发布了List::makeNodeM
的实现。【参考方案2】:
我认为适用于您的问题的最佳解决方案是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;
【讨论】:
以上是关于包含 2D 字符数组的构造函数的主要内容,如果未能解决你的问题,请参考以下文章
C将“字符串”的2D数组收集到数组中,然后将每个2D传递到C98函数中
C -(malloc、calloc 或静态)从函数返回的 2d 字符数组