gpt4 book ai didi

c++ - strlen() 不工作

转载 作者:行者123 更新时间:2023-11-27 22:30:26 24 4
gpt4 key购买 nike

基本上,我将一个指向字符串的指针传递到我的构造函数中,然后在传递字符串值时初始化它的基本构造函数。由于某种原因 strlen() 不工作,所以它不会进入对的 if 语句。我已经检查以确保变量中有一个值并且存在。

这是我的代码,我去掉了所有不相关的部分:

标签类内容:

Label(int row, int column, const char *s, int length = 0) : LField(row, column, length, s, false)
{
}

Label (const Label &obj) : LField(obj)\
{
}

~Label()
{
}

Field *clone() const
{
return new Label(*this);
}

LField类内容:

LField(int rowNumVal, int colNumVal, int widthVal, const char *valVal = "", bool canEditVal = true)
{
if(strlen(valVal) > 0)
{
}
else
{
//This is where it jumps to, even though the value in
//valVal is 'SFields:'
val = NULL;
}
}

Field *clone() const
{
return new LField(*this);
}

LField(const LField &clone) {
delete[] val;
val = new char[strlen(clone.val) + 1];
strcpy(val, clone.val);
rowNum = clone.rowNum;
colNum = clone.colNum;
width = clone.width;
canEdit = clone.canEdit;
index = clone.index;
}

屏幕类内容:

class Screen {
Field *fields[50];
int numOfFields;
int currentField;
public:
Screen()
{
numOfFields = 0;
currentField = 0;
for(int i = 0; i < 50; i++)
fields[i] = NULL;
}


~Screen()
{
for (int i = 0; i < 50; i++)
delete[] fields[i];
}

int add(const Field &obj)
{
int returnVal = 0;
if (currentField < 50)
{
delete[] fields[currentField];
fields[currentField] = obj.clone();
numOfFields += 1;
currentField += 1;
returnVal = numOfFields;
}
return returnVal;
}

Screen& operator+=(const Field &obj)
{
int temp = 0;
temp = add(obj);
return *this;
}
};

主要内容:

int main () {
Screen s1;
s1 += Label(3, 3, "SFields:");
}

希望有人能够看到我是否做错了什么。

最佳答案

<语言功能 XXXX 损坏>! ... 不,不是。

就在测量字符串之前,写入 puts(valVal),以确保您没有弄错该变量的内容。

关于c++ - strlen() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com