c0917a009b2eb7b4bbb70e4ae0ea4ccac6318a29..fd154e0d993431af3cf1c5383b13efce4b6a6396
2025-05-14 zxin
Merge branch 'master' of http://117.78.1.188:8089/r/test-upload
fd154e 对比 | 目录
2025-05-14 zxin
提交
d5fad6 对比 | 目录
2025-05-09 hzq
常见错误提交
922a77 对比 | 目录
2个文件已修改
4个文件已添加
238 ■■■■ 已修改文件
Test/Test.pro 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Test/errorexample.cpp 105 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Test/errorexample.h 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Test/linestyle.cpp 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Test/linestyle.h 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Test/mainwindow.cpp 99 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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 += \
Test/errorexample.cpp
New file
@@ -0,0 +1,105 @@
#include "errorexample.h"
#include <QLabel>
errorexample::errorexample() {
  // 1.未包含必要的头文件
  // 错误:缺少 #include <QLabel>
  QLabel label("Hello");  // 编译错误:'QLabel' was not declared in this scope
  label.show();
  // 2.未初始化指针
  QLabel *label4;
  label4->setText("Hello");  // 错误:使用未初始化的指针
  // 修复:初始化指针
  QLabel *label5 = new QLabel;
  // 3.父对象管理不当导致内存泄露
  QLabel *label1 = new QLabel("Memory Leak");
  label1->show();
  // 正确做法:设置父对象,Qt会自动管理内存
  QWidget *parent = new QWidget;
  QLabel *managedLabel = new QLabel("Managed", parent);
  // 4.使用已删除的对象
  QLabel *label2 = new QLabel("Delete Me");
  delete label2;
  // 错误:访问已删除的对象
  label2->setText("Oops");  // 未定义行为
  // 5.字符串编码问题
  QLabel *label3 = new QLabel;
  // 错误:硬编码非ASCII字符串
  label3->setText("中文文本");  // 可能显示乱码,取决于源文件编码
  // 正确做法:使用tr()进行翻译
  label3->setText(tr("中文文本"));
  // 6.容器越界访问
  QList<int> list = {1, 2, 3};
  // 错误:越界访问
  int value = list[3];  // 索引最大为2
  // 正确做法:使用at()并检查边界
  if (list.size() > 3) {
    int safeValue = list.at(3);  // at()会在越界时抛出异常
  }
  // 7.缺失终止条件的For循环
  for (int i = 0;; i++) {  // 错误:无终止条件
                           //循环体
  }
  // 正确写法:
  for (int i = 0; i < 10; i++) {  // 添加终止条件
                                  //循环体
  }
  // 8.误用 = 代替 ==
  if (x = 5) {  // 误将赋值操作当作比较,条件始终为 true(除非 x 是布尔类型)
                // ...
  }
  // 9.事件处理未调用基类实现
  void CustomWidget::paintEvent(QPaintEvent * event) {
    // 错误:未调用基类实现
    QPainter painter(this);
    // 缺少:QWidget::paintEvent(event);
  }
  // 10.信号槽参数不匹配
  // 错误:信号参数与槽参数类型不匹配
  connect(sender, &Sender::valueChanged(int), receiver,
          &Receiver::updateValue(QString));
  // 修复:确保参数类型一致
  connect(sender, &Sender::valueChanged(int), receiver,
          &Receiver::updateValue(int));
  // 11.未实现纯虚函数
  class MyInterface {
   public:
    virtual void pureVirtual() = 0;
  };
  class MyClass : public MyInterface {
    // 错误:未实现纯虚函数
  };
  // 修复:实现纯虚函数
  void MyClass::pureVirtual() { /* 实现 */
  }
  // 12.未正确实现拷贝构造函数
  class MyClass1 {
   public:
    QWidget *widget;
    MyClass1(const MyClass1 &other) { widget = other.widget; }  // 错误:浅拷贝
  };
  // 修复:深拷贝或禁用拷贝构造函数
  MyClass1(const MyClass1 &other) = delete;
}
Test/errorexample.h
New file
@@ -0,0 +1,13 @@
#ifndef ERROREXAMPLE_H
#define ERROREXAMPLE_H
#include <QObject>
class errorexample
{
    Q_OBJECT
public:
    errorexample();
};
#endif // ERROREXAMPLE_H
Test/linestyle.cpp
New file
@@ -0,0 +1,6 @@
#include "linestyle.h"
linestyle::linestyle()
{
}
Test/linestyle.h
New file
@@ -0,0 +1,11 @@
#ifndef LINESTYLE_H
#define LINESTYLE_H
class linestyle
{
public:
    linestyle();
};
#endif // LINESTYLE_H
Test/mainwindow.cpp
@@ -1,66 +1,61 @@
#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;
  /**********类型转换错误**********/
  // 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 指针不能直接转为有符号类型指针
  // 2.强制类型转换错误
  void* void_ptr = nullptr;
  int* int_ptr = static_cast<int*>(
      void_ptr);  // 强制转换错误,void 指针不能直接转为有符号类型指针
    //3.转换类型后最好判断一下是否转换成功,指针是否为空
  // 3.转换类型后最好判断一下是否转换成功,指针是否为空
  /**********逻辑错误**********/
  // 1.循环无退出条件(死循环)
  for (int i = 0;; i++) {
    // 循环体
  }
  // 2.数组越界
  int array[10];
  array[10] = 0;  // 越界访问容器元素
  std::vector<int> vec(5);
  vec[10] = 0;  // 越界访问容器元素
    /**********逻辑错误**********/
    // 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 的值,条件为真
                // 执行代码块
  }
    //3.条件判断中的逻辑运算符误用(= 代替 ==)
    int a = 5, b = 10;
    if (a = b) { // 误用 = 代替 ==,此处逻辑短路,a 被赋值为 b 的值,条件为真
        // 执行代码块
    }
  // 4.未初始化变量的使用
  int uninitialized;
  if (uninitialized > 0) {  // 使用未初始化变量,可能导致未定义行为
                            // 执行代码块
  }
    //4.未初始化变量的使用
    int uninitialized;
    if (uninitialized > 0) { // 使用未初始化变量,可能导致未定义行为
        // 执行代码块
    }
  /**********内存管理错误**********/
  // 1.引用空指针
  int* ptr = new int[5];
  delete[] ptr;
  ptr[0] = 10;  // 指针已悬空,仍尝试访问
    /**********内存管理错误**********/
    //1.引用空指针
    int* ptr = new int[5];
    delete[] ptr;
    ptr[0] = 10; // 指针已悬空,仍尝试访问
  // 2.内存泄露
  int* pt = new int[10];
  // 没有 delete[] pt
    //2.内存泄露
    int* pt = new int[10];
    // 没有 delete[] pt
    /**********其他错误**********/
    //未使用的变量
    int unused = 0;
  /**********其他错误**********/
  //未使用的变量
  int unused = 0;
}
MainWindow::~MainWindow()
{
    delete ui;
}
MainWindow::~MainWindow() { delete ui; }