visual Using A Delete Keyword With Arrays C++
visual Using A Delete Keyword With Arrays C++
因此,在我的程序中添加了另一个功能,我决定包含一个可以从myCourses[10]
中删除所有信息的函数。最新代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Course
{
string name;
double grade;
int block;
};
Course enter_course()
{
Course foo;
cout << "What is the name of the course you wish to enter? (Use this format: ExampleFormat)n";
cin >> foo.name;
cout << "What block is " << foo.name << " ?n";
cin >> foo.block;
cout << "What is your current grade as a percent?n";
cin >> foo.grade;
return foo;
}
void display_courses(Course courseList[10], int courseCount)
{
for (int i=0; i<courseCount; i++){
cout << i+1 << "t" << courseList[i].name
<< "ttBlock: " << courseList[i].block
<< "tGrade: " << courseList[i].grade << "%" << endl;
}
}
double get_gpa(Course courseList[10], int courseCount)
{
double gradePoints;
double total = 0.0;
for (int i=0; i<courseCount; i++){
if (courseList[i].grade < 100){
gradePoints = 4;
}
if (courseList[i].grade < 90){
gradePoints = 3;
}
if (courseList[i].grade < 80){
gradePoints = 2;
}
if (courseList[i].grade < 70){
gradePoints = 1;
}
if (courseList[i].grade < 60){
gradePoints = 0;
}
total += gradePoints;
}
return total*1.0/courseCount;
}
void display_options()
{
cout << "1. Exitn";
cout << "2. Enter a Coursen";
cout << "3. Display Coursesn";
cout << "4. Display GPAn";
cout << "5. Request a text file outputn";
cout << "6. Delete Gradesn";
cout << "nn";
}
int main()
{
bool exit=0;
int option;
int courseCount=0;
Course myCourses[10]; //nobody should ever take more than 10 courses!
while (exit == 0)
{
cout << "GradeBook 2.0n";
display_options();
cout << "Enter a command.n";
cin >> option;
switch (option)
{
case 1:
exit = 1;
break;
case 2:
myCourses[courseCount] = enter_course();
courseCount++;
break;
case 3:
display_courses(myCourses, courseCount);
break;
case 4:
cout << get_gpa(myCourses, courseCount) << endl;
break;
case 5:
ofstream outputFile;
outputFile.open("userGrades.txt");
for (int i = 0; i < courseCount; i++)
{
outputFile << myCourses[i].name << " " << myCourses[i].grade << " " << myCourses[i].block << endl;
}
outputFile.close();
cout << "Grades saved to file!" << endl;
break;
case 6:
cout << "Removing data...n";
delete[] myCourses;
break;
}
}
return 0;
}
但是,我收到以下错误:错误C2360:"case"标签跳过了"outputFile"的初始化(第114行)IntelliSense:控制权的传输绕过:变量"outputFile"(在第105行声明)的初始化(第89行)警告C4154:删除数组表达式;转换为提供的指针(第116行)有人知道在开关块中使用delete[]
关键字有什么问题吗?
问题是您正在尝试delete
,而不是用new
创建的东西。也就是说,阵列myCourses
不是动态分配的。您可以使用new
动态分配一些内容,然后使用delete
解除分配。如果你不动态地分配它,你只会让它超出范围。
只需去掉delete[] myCourses;
行。
不能从数组中删除元素。它们从声明数组的那一刻起就存在,直到数组超出范围。如果你希望元素有某种清除状态,你需要决定它是什么。例如,你可以给Course
一个标志,说明它是否被清除。然而,一个更好的方法是使用像std::vector
这样的标准库容器——这样,你就可以真正地从中删除元素
另一个错误是由于试图在switch
语句中声明一个变量而没有将其包含在自己的块中。可以通过在case 5:
的内容周围放置大括号来解决此问题。
你不能delete
你没有new
。它们必须对应。您正在尝试delete
一个从未分配过new
的对象。
由于其他人已经对不明智的delete[]
发表了评论,让我们也解决另一个问题:如果你想在switch
-语句的块内构造一个对象,你需要将它放入自己的块中:
switch (option)
{
case 5:
{
ofstream outputFile;
outputFile.open("userGrades.txt");
for (int i = 0; i < courseCount; i++)
{
outputFile << myCourses[i].name << " " << myCourses[i].grade << " "
<< myCourses[i].block << endl;
}
outputFile.close();
cout << "Grades saved to file!" << endl;
break;
}
}
(注意std::ofstream
信息块周围的额外括号)。另外,请注意,不必要地使用std::endl
会导致性能问题!除非您真的想刷新流,否则请使用'n'
。。。当您想要刷新流时,请使用CCD_ 22,而不是使用std::endl
隐藏它。
- Problems with std::cin.fail()
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- Qt with WinAPI MouseProc
- [[maybe_unused]] with structured_binding?
- Issue with WriteProcessMemory
- OpenCV RTP-Stream with FFMPEG
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- std::adjacent_difference with std::chrono time_point
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- DLL Made with CMake 使程序崩溃
- QtCreator with C 库中的链接器问题
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- specialized std::default_delete with QQmlComponent
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- visual Using A Delete Keyword With Arrays C++
- boost::ptr_vector with arrays:我能确定它的析构函数调用delete[]而不是delete吗