• 编译出现的问题解决


    1.exception handling 异常处理

    知识点的补充

    • 1.了解抛出异常时发生了什么 throwing raised
    • 2.捕获异常时的情况 catch
    • 3.传递错误对象的意义 (抛出表达式的类型,调用链决定处理那段代码-handler)

    异常处理:

    1.让一个函数发现了自己无法处理的错误时throw抛出异常。一个库的作者可以检测出发生怎样的错误,却不知道如何处理;库的使用者处理错误,却无法检测何时发生。这就需要最基本的异常检测


    2.C++中的错误:

    • 语法错误(编译错误):变量为定义、缺少括号、关键字缺失等错误可以在函数进行编译的时候发现错误。编译器可以告知发生错误的位置、原因。容易去改正代码
    • 运行时错误:内存越界、数组下标等问题,但是可以进行编译进行运行,运行时会出现错误导致程序的崩溃。就是为了更好的处理程序在运行时候的错误,引入了异常处理机制来解决问题。

    3.C语言使用的方法

    • 1.int整形值来标识错误(整形的返回值 0or 1)
    • 2.error的宏来实现

    exception:

    • bad_cast
    • runtime_error:
      • overflow_error
      • underflow_error
      • range_error
    • logic_error:
      • domain_error
      • invalid_argument
      • out_of_range
      • length_error
    • bad_alloc

    代码片段截取

    //剑指offer(面试题11 旋转数组中的最小数字)
    #include <iostream>
    #include <cstdlib>
    #include <exception>
    // using namespace std;
    
    int MinInOrder(int* numbers, int index1, int index2);
    
    int Min(int* numbers, int length)
    {
        if(numbers == nullptr || length <= 0)
            throw new std::exception("Invalid parameters"); // 出现错误
        
        int index1 = 0;
        int index2 = length - 1;
        int indexMid = index1;
            // 如果数组旋转了,则进入数组,如果数组没有旋转 直接返回 index1 就是最小元素
        while(numbers[index1] >= numbers[index2])
        {
                // 如果index1和index2指向相邻的两个数,
                // 则index1指向第一个递增子数组的最后一个数字,
                // index2指向第二个子数组的第一个数字,也就是数组中的最小数字
            if(index2 - index1 == 1)
            {
                indexMid = index2;
                break;
            }
            
                // 如果下标为index1、index2和indexMid指向的三个数字相等,
                // 则只能顺序查找
            indexMid = (index1 + index2) / 2;
            if(numbers[index1] == numbers[index2] && numbers[indexMid] == numbers[index1])
                return MinInOrder(numbers, index1, index2);
            
                // 缩小查找范围
            if(numbers[indexMid] >= numbers[index1])
                index1 = indexMid;
            else if(numbers[indexMid] <= numbers[index2])
                index2 = indexMid;
        }
        
        return numbers[indexMid];
    }
    
    

    直接修改

    int Min(int* numbers, int length)
    {
        if(numbers == nullptr || length <= 0)
            throw new std::invalid_argument("Invalid parameters"); 
    
    // 都是直接表示是什么错误
    
    // 例如
    throw std::overflow_error("too big")
    // 或者是
    const std::exception& e
    
    

    关于 exception handling 异常处理还是需要学习
    慢慢积累

    2.头文件定义问题 “__declspec” 属性不能使用

    报错问题

    error: '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes

    看一看代码片段

    // 结构化定义一个链表结点
    // 并定义结点的基本函数
    struct ListNode
    {
        int       m_nValue;
        ListNode* m_pNext;
    };
    
    // 这个问题一直没有解决,可能是xcode的配置问题
    /*
    error: '__declspec' attributes are not enabled; use
          '-fdeclspec' or '-fms-extensions' to enable support for __declspec
          attributes
    
     */
    // 注释一下
    // 也找不到解答,问题看不懂
    __declspec( dllexport ) ListNode* CreateListNode(int value);
    __declspec( dllexport ) void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
    __declspec( dllexport ) void PrintListNode(ListNode* pNode);
    __declspec( dllexport ) void PrintList(ListNode* pHead);
    __declspec( dllexport ) void DestroyList(ListNode* pHead);
    __declspec( dllexport ) void AddToTail(ListNode** pHead, int value);
    __declspec( dllexport ) void RemoveNode(ListNode** pHead, int value);
    

    解决办法

    哈哈哈,我能告诉你我没有找到解决的办法吗?
    没有具体的文档,而且不清楚这个是什么含义,这个问题暂且搁置,后面解决

    更新解决办法

    //解决的办法也比较暴力,直接在头文件中进行修改
    //直接将前面的删除掉
    //编译环境 clang 
     ListNode* CreateListNode(int value);
     void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
     void PrintListNode(ListNode* pNode);
     void PrintList(ListNode* pHead);
     void DestroyList(ListNode* pHead);
     void AddToTail(ListNode** pHead, int value);
     void RemoveNode(ListNode** pHead, int value);
    
    不要用狭隘的眼光看待不了解的事物,自己没有涉及到的领域不要急于否定. 每天学习一点,努力过好平凡的生活.
  • 相关阅读:
    find module providing package github.com/go-sql-driver/mysql: working directory is not part of a module
    深度学习中的epoch、batchsize、iterations的理解
    淘宝软件质量属性分析
    Git
    多线程
    Spark基础之Scala
    机器学习十讲第十讲
    机器学习十讲第九讲
    机器学习十讲第六讲
    本地MarkDown优雅发表
  • 原文地址:https://www.cnblogs.com/GeekDanny/p/10020166.html
Copyright © 2020-2023  润新知