动态数组的大小与提交的值不匹配
Size of dynamic array does not match the value submitted
我正在学习动态记忆,但有些地方不太对劲。我有一个函数,它接受一个数字作为输入,并应该生成一个该大小的数组。
class Doctor {
public:
Doctor();
void fillOut();
void listPatients();
void patientReset();
~Doctor();
private:
string name;
int numPatients;
string *patientList;
};
Doctor::Doctor() {
name = "";
numPatients = 0;
patientList = new string[numPatients];
}
(第三个代码块中的最相关代码)。
void Doctor::fillOut()
{
string buffer = "";
string buffer2 = "";
size_t found;
bool valid = false;
int numP = 0;
int tester = 0;
bool validNum = false;
while(!valid)
{
cout << "Enter doctor name: ";
getline(cin, buffer);
found = buffer.find_first_of("1234567890!@#$%^&*()-=_+/<>?;':][");
if(string::npos == found)
{
name = buffer;
valid = true;
}
}
while (!validNum)
{
cout << "nEnter number of patients: ";
buffer = "";
getline(cin, buffer);
buffer2 = buffer;
stringstream ss(buffer);
if(ss >> tester)
{
stringstream ss2(buffer2);
ss2 >> numP;
validNum = true;
}
else
{
cout << "Not a number. Please try again." << endl;
}
}
patientList = new string[numP];
cout << patientList->size() << endl;
for(int i = 0; i < (numP + 0); i++)
{
valid = false;
while(!valid)
{
cout << "nEnter patient " << (i + 1) << ": ";
getline(cin,buffer);
found = buffer.find_first_of("1234567890!@#$%^&*()-=_+,./<>?;':][");
if(string::npos == found)
{
*(patientList + i - 0) = buffer;
//patientList[i-1] = buffer;
valid = true;
}
else
{
valid = false;
}
}
}
}
然后我尝试显示列表的内容。
void Doctor::listPatients()
{
cout << "size: " << patientList->size() << endl;
cout << "nDoctor: " << name << endl;
for(int i = 0; i < (patientList->size() - 1); i++)
{
cout << "Patient " << (i+1) << ": " << patientList[i] << endl;
}
cout << "end patients" << endl;
}
但由于某种原因,我提交的数字并不是数组的大小。例如,在fillOut()
中,我有输出大小的函数。它每次都将大小输出为0。然后在listPatients()
中,我有一些东西可以再次打印尺寸,以验证我做得对。如果我最初输入3,它在这个函数中有输出5。
我完全感到困惑。
行patientList->size()
相当于patientList[0].size()
,即patientList
数组中初始字符串的长度。在刚刚分配数组的点上,结果总是为零;在其他情况下,它是第一个字符串的长度。
在C++中制作容器的一种优选方式是std::vector
或std::array
。在您的情况下,使用std::vector
更合适,因为您的代码动态地分配patientList
。
好的,我发现了你的问题。您正在声明一个字符串数组。但是,原始数组实际上只是指向数组内容的指针。当您调用->size时,它会取消引用该指针,该指针指向数组中的第一个字符串,并告诉该字符串的大小。
如果您想要一个真正知道其大小的数组,请使用std::array。
好的,所以您将patientList声明为字符串*,但当您对此调用size()时,实际上是在您创建的数组中的第一个项上调用它。编译代码的唯一原因是字符串有一个size()函数,因为这是数组中包含的类型,所以您可以忽略它,因为patientList实际上指向数组开头的项(字符串)。通常,数组没有size()函数。
你会遇到的另一个问题是在你的循环中
for(int i = 0; i < (patientList->size() - 1); i++) {
^
^
cout << "Patient " << (i+1) << ": " << patientList[i] << endl;
}
这将导致您的循环在patientList结束前终止一个。你应该使用
for(int i = 0; i < patientList->size(); i++) {
cout << "Patient " << (i+1) << ": " << patientList[i] << endl;
}
你最好在这里使用std::vector来放入字符串
if(string::npos == found) {
*(patientList + i - 0) = buffer;
//patientList[i-1] = buffer;
valid = true;
}
else {
valid = false;
}
因为只有当if语句为true时,您才会向数组赋值——当它为false时,数组中的索引将保持未初始化状态。老实说,我会从头开始,使用字符串向量。
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 动态数组的大小与提交的值不匹配