編譯出現的問題解決


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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM