From 922a7702ea05a84cb5272fd1d5ff50e3d873cb6c Mon Sep 17 00:00:00 2001
From: hzq <hzq123456>
Date: 星期三, 14 五月 2025 11:18:45 +0800
Subject: [PATCH] 常见错误提交

---
 Test/linestyle.h    |   11 +++++
 Test/mainwindow.cpp |   69 +++++++++++++++++++++++++++++-----
 Test/Test.pro       |    4 ++
 Test/linestyle.cpp  |    6 +++
 4 files changed, 80 insertions(+), 10 deletions(-)

diff --git a/Test/Test.pro b/Test/Test.pro
index 0086fd6..afe781a 100644
--- a/Test/Test.pro
+++ b/Test/Test.pro
@@ -16,10 +16,14 @@
 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 SOURCES += \
+    errorexample.cpp \
+    linestyle.cpp \
     main.cpp \
     mainwindow.cpp
 
 HEADERS += \
+    errorexample.h \
+    linestyle.h \
     mainwindow.h
 
 FORMS += \
diff --git a/Test/linestyle.cpp b/Test/linestyle.cpp
new file mode 100644
index 0000000..f35e83f
--- /dev/null
+++ b/Test/linestyle.cpp
@@ -0,0 +1,6 @@
+#include "linestyle.h"
+
+linestyle::linestyle()
+{
+
+}
diff --git a/Test/linestyle.h b/Test/linestyle.h
new file mode 100644
index 0000000..a5d7243
--- /dev/null
+++ b/Test/linestyle.h
@@ -0,0 +1,11 @@
+#ifndef LINESTYLE_H
+#define LINESTYLE_H
+
+
+class linestyle
+{
+public:
+    linestyle();
+};
+
+#endif // LINESTYLE_H
diff --git a/Test/mainwindow.cpp b/Test/mainwindow.cpp
index 41a26bd..2364edf 100644
--- a/Test/mainwindow.cpp
+++ b/Test/mainwindow.cpp
@@ -1,15 +1,64 @@
 #include "mainwindow.h"
+
 #include "ui_mainwindow.h"
 
-MainWindow::MainWindow(QWidget *parent)
-    : QMainWindow(parent)
-    , ui(new Ui::MainWindow)
-{
-    ui->setupUi(this);
+MainWindow::MainWindow(QWidget* parent)
+    : QMainWindow(parent), ui(new Ui::MainWindow) {
+  ui->setupUi(this);
+
+  /**********类型转换错误**********/
+  // 1.隐式类型转换导致的精度损失
+  double numDouble = 3.1415926;
+  int numInt = numDouble;  // 隐式转换,精度损失,没有进行显式 static_cast
+  // int numInt = static_cast<int>(numDouble); // 显式转换,明确表示接受精度损失
+  // int numInt = (int)numDouble;
+
+  // 2.强制类型转换错误
+  void* void_ptr = nullptr;
+  int* int_ptr = static_cast<int*>(
+      void_ptr);  // 强制转换错误,void 指针不能直接转为有符号类型指针
+
+  // 3.转换类型后最好判断一下是否转换成功,指针是否为空
+
+  /**********逻辑错误**********/
+  // 1.循环无退出条件(死循环)
+  for (int i = 0;; i++) {
+    // 循环体
+  }
+  // 2.数组越界
+  int array[10];
+  array[10] = 0;  // 越界访问容器元素
+  std::vector<int> vec(5);
+  vec[10] = 0;  // 越界访问容器元素
+
+  // 3.条件判断中的逻辑运算符误用(= 代替 ==)
+  int a = 5, b = 10;
+  if (a = b) {  // 误用 = 代替 ==,此处逻辑短路,a 被赋值为 b 的值,条件为真
+                // 执行代码块
+  }
+
+  // 4.未初始化变量的使用
+  int uninitialized;
+  if (uninitialized > 0) {  // 使用未初始化变量,可能导致未定义行为
+                            // 执行代码块
+  }
+
+  /**********内存管理错误**********/
+  // 1.引用空指针
+  int* ptr = new int[5];
+  delete[] ptr;
+  ptr[0] = 10;  // 指针已悬空,仍尝试访问
+
+  // 2.内存泄露
+  int* pt = new int[10];
+  // 没有 delete[] pt
+
+  /**********其他错误**********/
+  // 1.未使用的变量
+  int unused = 0;
+
+  // 2.未包含必要的头文件
+  //#include<>
 }
 
-MainWindow::~MainWindow()
-{
-    delete ui;
-}
-
+MainWindow::~MainWindow() { delete ui; }

--
Gitblit v1.9.1