VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 625|回复: 7

[已解决]为什么f5调试出错,CTRL+f5运行没错误呢

[复制链接]
65_avatar_middle
最佳答案
0 
在线会员 发表于 2021-9-13 23:49:36 | 显示全部楼层 |阅读模式
就是f5调试的时候会在断点出现第一个图片的错误,CTRL+f5却可以直接运行,

以下为代码,实在不好意思,这个好像不能直接上传工程文件吗?只能这样上传了,有点多,抱歉抱歉

#include <iostream>
using namespace std;
#include <string>
#include "Student.h"
int average(CStudent*arr_stu, int num)
{
        if (!arr_stu || num <= 0)
        {
                return 0;
        };
        EstudentType stud_type = arr_stu[0].type;
        int sum_age=0;
        for (int idx = 0; idx < num; idx++)
        {
                switch (stud_type)
                {
                case EstudentType_Xiao:
                        sum_age += ((CXiaoStudent*)arr_stu)[idx].age;
                        break;
                case EstudentType_Zhong:
                        sum_age += ((CZhongStudent*)arr_stu)[idx].age;
                        break;
                default:
                        break;
                }
        }
                int av_age=sum_age / num;
                switch (stud_type)
                {
                case EstudentType_Xiao:
                        cout << "小学生的平均年龄是" << av_age;
                        break;
                case EstudentType_Zhong:
                        cout << "中学生的平均年龄是" << av_age;
                        break;
                default:
                        break;
                }
                return av_age;
}
int main()
{
        CStudent *stud = new CStudent("ddddddddddddddd", 'm', 50, 40);       
        CZhongStudent arr[3];
        arr[0].age = 20;
        arr[1].age = 18;
        arr[2].age = 25;
        average(arr, 3);
        delete stud;
        return 0;
}



#pragma once
#include <string>
#include <iostream>
using namespace std;
enum         EstudentType
{
        EstudentType_Error,
        EstudentType_Xiao,
        EstudentType_Zhong
};
class CStudent
{
public:
        char *p_name;
        char sex;
        int num;
        int age;
        EstudentType type;
        CStudent()
        {
        ///
        }
        CStudent(char* t_name, char t_sex, int t_num, int t_age);
        ~CStudent();       
};
class  CXiaoStudent:public CStudent
{
public:
        int shuxue_score;
        int yuwen_score;
        CXiaoStudent();
protected:
private:
};
class  CZhongStudent :public CXiaoStudent
{
public:
        int wuli_score;
        int huaxue_score;
        CZhongStudent();

};


#include "Student.h"
CStudent::CStudent(char* t_name,char t_sex,int t_num,int t_age)
{
        int n_len = strlen(t_name);
        p_name = new char[n_len + 1];
        memset(p_name,0, n_len + 1);
        strcpy(p_name, t_name);
        type = EstudentType_Error;
               
        cout << "oooo" << endl;
        sex = t_sex;
        num = t_num;
        age = t_age;
}
CStudent::~CStudent()
{
        if (p_name)
        {
                delete[]p_name;               
        }
        cout << "niubi" << endl;
}
CXiaoStudent::CXiaoStudent()
{
        type = EstudentType_Xiao;
}
CZhongStudent::CZhongStudent()
{
        type = EstudentType_Zhong;
}
最佳答案
31_avatar_small
2021-9-14 10:17:18
报错的原因:
CZhongStudent arr[3] 没有初始化p_name,而析构器中却直接delete []p_name

p_name 声明修改:
  1. class CStudent
  2. {
  3. public:
  4.         //
  5.         char *p_name = nullptr;
  6.         char sex;
  7.         int num;
  8.         int age;
  9.         EstudentType type;
  10.         CStudent(){}
  11.         CStudent(char* t_name, char t_sex, int t_num, int t_age);
  12.         ~CStudent();
  13. };
复制代码


析构器修改为:
  1. CStudent::~CStudent()
  2. {
  3.         if (nullptr != p_name)
  4.         {
  5.                 delete[]p_name;
  6.                 p_name = nullptr;
  7.         }
  8.         cout << "niubi" << endl;
  9. }
复制代码

A5K)K}NN6(K}~VR_Y~SNB]R.png
5555.png
4444.png
3333.png
222 (1).png
11111111.png




上一篇:C++ 如何不用图形库绘图?
下一篇:奇怪 为什么gcc运行结果和vc2013会不一样
07_avatar_middle
最佳答案
12 
在线会员 发表于 2021-9-14 14:40:20 | 显示全部楼层
不忘初心 发表于 2021-9-14 11:04
对,谢谢大神,我刚开始觉得可能是这个原因,就是不知道该咋改

以下内容纯属个人愚见,
个人在写C/C++的时候,使用指针或变量时,一定要保证该指针或变量是处在一个明确的状态。这个明确的状态,是指在指针创建后就马上指向了内容或者马上被直接置空了。
也就是说,出现指针时,比如char* p ,要不直接置空,char*p = nullptr;或者直接指向确定的内容,char*p= &aaa;或者char*p = A_Pointer_from_function();
个人不喜欢如下风格,
{
char* p;
p = nullptr;
}
因为项目后期维护的时候,没准什么时候就被小白给你在中间插入什么代码了。虽然上面这个风格在很多C开源项目里很常见。

回到你的问题,在默认构造函数的时候,直接初始化指针状态即可。

不要依赖任何编译器的行为,把所有指针\变量都置于你自己的严格控制中。这样才能减少开发后期遇到隐藏坑的几率。
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-9-14 10:17:18 | 显示全部楼层    本楼为最佳答案   
bestAnswer
报错的原因:
CZhongStudent arr[3] 没有初始化p_name,而析构器中却直接delete []p_name

p_name 声明修改:
  1. class CStudent
  2. {
  3. public:
  4.         //
  5.         char *p_name = nullptr;
  6.         char sex;
  7.         int num;
  8.         int age;
  9.         EstudentType type;
  10.         CStudent(){}
  11.         CStudent(char* t_name, char t_sex, int t_num, int t_age);
  12.         ~CStudent();
  13. };
复制代码


析构器修改为:
  1. CStudent::~CStudent()
  2. {
  3.         if (nullptr != p_name)
  4.         {
  5.                 delete[]p_name;
  6.                 p_name = nullptr;
  7.         }
  8.         cout << "niubi" << endl;
  9. }
复制代码

75_avatar_middle
最佳答案
23 
online_supermod 发表于 2021-9-14 07:02:00 | 显示全部楼层
把exe文件删除 然后生成试试  在运行看看
65_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-9-14 09:51:13 | 显示全部楼层
wl1383838438 发表于 2021-9-14 07:02
把exe文件删除 然后生成试试  在运行看看

我尝试了一下  不行
65_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-9-14 10:20:41 | 显示全部楼层
wl1383838438 发表于 2021-9-14 07:02
把exe文件删除 然后生成试试  在运行看看

是因为只调用了一次基类的构建函数,却调用了三次析构函数,所以导致多delete导致的吗
65_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-9-14 11:04:49 | 显示全部楼层
cpp2019 发表于 2021-9-14 10:17
报错的原因:
CZhongStudent arr[3] 没有初始化p_name,而析构器中却直接delete []p_name

为什么f5调试出错,CTRL+f5运行没错误呢对,谢谢大神,我刚开始觉得可能是这个原因,就是不知道该咋改为什么f5调试出错,CTRL+f5运行没错误呢
75_avatar_middle
最佳答案
23 
online_supermod 发表于 2021-9-14 13:10:41 | 显示全部楼层
不忘初心 发表于 2021-9-14 10:20
是因为只调用了一次基类的构建函数,却调用了三次析构函数,所以导致多delete导致的吗

这个我不确定,但是假如  默认的构造函数有析构那么不需要在写,但是问题描述 感觉又不像    能力有限为什么f5调试出错,CTRL+f5运行没错误呢
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-29 23:47

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表