最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++ std::invalid_argument應用

 更新時間:2013年01月02日 16:30:03   作者:  
想研究std::invalid_argument的朋友可以參考下
首先說明invalid_argument是一個類(class invalid_argument;),它的繼承關(guān)系如下

exception-------->logic_error--------->invalid_argument

invalid_argument原型是
復制代碼 代碼如下:

class invalid_argument:public logic_error {
public:
explicit invalid_argument (const string& what_arg);
};

它在stdexcept頭文件中,在std命名空間內(nèi)。下面舉一個例子來使用它
復制代碼 代碼如下:

#include <iostream>
#include <stdexcept>

int main(int argc,char ** argv)
{
try
{
bool errorArgument;
errorArgument=true;
if(errorArgument)
{
throw std::invalid_argument("occur error!");
}
}
catch(std::invalid_argument &ia)
{
//what()為invalid_argument繼承exception類的函數(shù)
std::cerr<<" Invalid_argument "<< ia.what()<<std::endl;
}

return 0;
}

運行結(jié)果為:

Invalid_argument occur error!那么上面的例子是一個最簡單的應用了。invalid_argument顧名思義指無效參數(shù),這個應該應用在檢查參數(shù)是否是無效的,一般檢查參數(shù)用于特定的函數(shù)以及類,那么就應該是給類的成員變量賦值或者函數(shù)參數(shù)賦值時,檢查其賦給它們的值是否有效,例如有一個類(people,有三個成員變量name,age,height)那么我們知道人的年齡在0~150歲之間(ps:如果對于程序員可以直接定義為0~75)。身高的話0~300cm,名字的長度不會超過20。如果都超過這些范圍,就可以認定是無效數(shù)據(jù)。那么這個類可以如下定義:
復制代碼 代碼如下:

#include <stdexcept>
#include <iostream>
#include <string>

class People
{
public:
People(const std::string& n,const int& a,const int& h)
:name(n),age(a),height(h)
{}

inline void set(const std::string& n,const int& a,const int& h)
{
if(!valid(n,a,h))
{
throw std::invalid_argument("People's argument is error");
}
name = n;
age = a;
height = h;
}

inline bool valid(const std::string& n, const int& a, const int& h)
{
return ( n.length() == 0 ||n.length() > 20 )&& a >= 0 && a< 150 && h > 0 && h < 300 ;
}
private:
std::string name;
int age;
int height;

};

int main(int argc, char** argv)
{
People p("Li San", 20 , 170);
try
{
p.set("Li San" , 20 ,1700);
}
catch (std::invalid_argument & ia)
{
std::cerr << "Error: " << ia.what() << std::endl;
}
return 0;
}

其運行結(jié)果為:

Error: People's argument is error上面程序只要輸入無效數(shù)據(jù),就會輸出錯誤。但是僅僅這樣是不夠的,我們還無法定位無效參數(shù)在哪個文件與哪個一行或者在哪個函數(shù)中,如果在打印錯誤的時候連這些信息一并輸出相信定位問題就方便多了。那么我們在報出錯誤信息的時候連這些信息也附加上就明確多了。
復制代碼 代碼如下:

#include <stdexcept>
#include <iostream>
#include <string>
#define TOSTRING(x) #x

//class ErrorInfo
//{
// public:
// ErrorInfo(const std::string& f,const std::string& l,const std::string& fun)
// : file(f), line(l), func(fun)
// {}
//
// inline const std::string getFile() const
// {
// return this->file;
// }
//
// inline const std::string getLine() const
// {
// return this->line;
// }
//
// inline const std::string getFunc() const
// {
// return this->func;
// }
//
// private:
// const std::string file;
// const std::string line;
// const std::string func;
//};

class ErrorInfo
{
public:
ErrorInfo(const char * f, const char * l, const char * fun)
:file(f), line(l), func(fun)
{}

inline std::string getFile() const
{
return this->file;
}

inline std::string getLine() const
{
return this->line;
}

inline std::string getFunc() const
{
return this->func;
}
private:
const char* file;
const char* line;
const char* func;
};

std::string operator +(const std::string & str, const ErrorInfo& ei)
{
std::string strTemp(ei.getFile() + ":" + ei.getLine() + ":" + ei.getFunc());
strTemp +=str;
return strTemp;
//return str::string(ei.getFile() + ":" + ei.getLine() + ":" + ei.getFunc() += str );
}

class InvalidPeople:public std::invalid_argument
{
public:
InvalidPeople(ErrorInfo & ei)
: std::invalid_argument( "Invalid People " + ei )
{}
~InvalidPeople() throw()
{}
};

class People
{
public:
People(const std::string& n,const int& a,const int& h)
:name(n),age(a),height(h)
{}

inline void set(const std::string& n,const int& a,const int& h)
{
if(!valid(n,a,h))
{
ErrorInfo ei(__FILE__,TOSTRING(__LINE__),__PRETTY_FUNCTION__);
// ErrorInfo ei(__FILE__,#__LINE__,__PRETTY_FUNCTION__);
throw InvalidPeople(ei);
// throw InvalidPeople(ErrorInfo(__FILE__,TOSTRING(__LINE__),__PRETTY_FUNCTION__));
}
name = n;
age = a;
height = h;
}

inline bool valid(const std::string& n, const int& a, const int& h)
{
return ( n.length() == 0 ||n.length() > 20 )&& a >= 0 && a< 150 && h > 0 && h < 300 ;
}
private:
std::string name;
int age;
int height;

};

int main(int argc, char** argv)
{
People p("Li San", 20 , 170);
try
{
p.set("Li San" , 20 ,1700);
}
catch (std::invalid_argument & ia)
{
std::cerr << "Error: " << ia.what() << std::endl;
}
return 0;
}

其運行結(jié)果為:

TestError: invalid_a.cpp:__LINE__:void People::set(const std::string&, const int&, const int&)Invalid People注意:
(1)上面#define TOSTRING(x) #x就可以將int類型轉(zhuǎn)換為const char *類型。
(2) __FILE__是const char*類型,并且通過#define TOSTRING(x)轉(zhuǎn)換后的類型為const char*類型,編譯器是gun那么獲取所在的函數(shù)名稱就是__PRETTY_FUNCTION__也是const char*類型。
可以看到__LINE__并沒有顯示出行號,這里原因編譯器直接將__LINE__其轉(zhuǎn)換為"__LINE__"這個字符串了那么這里如何解決呢?筆者試過好多方法,最終找出來了,我們可以在#define一次,就可以正常現(xiàn)實了。如下代碼
復制代碼 代碼如下:

#include <stdexcept>
#include <iostream>
#include <string>
#define TTOSTRING(x) #x
#define TOSTRING(x) TTOSTRING(x)
...
...
//后面代碼與上面一樣

其運行結(jié)果為:

TestError: invalid_a.cpp:91:void People::set(const std::string&, const int&, const int&)Invalid People
至于里面原理,為什么兩次就能夠?qū)_LINE__表示的數(shù)字例如(71)轉(zhuǎn)換成const char* “71”,而不是轉(zhuǎn)換成“__LINE__"const char*字符串,希望清楚的園友,給出答案,謝謝!
您可能感興趣的文章:

相關(guān)文章

  • C++設(shè)計模式之解釋器模式

    C++設(shè)計模式之解釋器模式

    這篇文章主要介紹了C++設(shè)計模式之解釋器模式,本文講解了什么是解釋器模式、文法規(guī)則和抽象語法樹、解釋器模式的使用場合等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • MFC控件大小隨窗體大小而改變

    MFC控件大小隨窗體大小而改變

    本文給大家分享的是使用VC++根據(jù)對話框大小調(diào)整控件大小的方法和示例代碼,有需要的小伙伴可以參考下。
    2015-06-06
  • C語言雙指針算法朋友過情人節(jié)我過算法

    C語言雙指針算法朋友過情人節(jié)我過算法

    這篇文章主要為大家介紹了C語言中雙指針算法的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • Qt與Web混合開發(fā)實現(xiàn)雙向通信的示例

    Qt與Web混合開發(fā)實現(xiàn)雙向通信的示例

    本文主要介紹了Qt與Web混合開發(fā)實現(xiàn)雙向通信的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • 利用C語言實現(xiàn)順序表的實例操作

    利用C語言實現(xiàn)順序表的實例操作

    順序表是線性表中的一種重要的數(shù)據(jù)結(jié)構(gòu),也是最基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),所以他不僅是學習中的重點,也是應用開發(fā)非常常用的一種數(shù)據(jù)結(jié)構(gòu)。這篇文章介紹如何利用C語言實現(xiàn)順序表。
    2016-08-08
  • C語言求兩個正整數(shù)的最大公約數(shù)示例代碼

    C語言求兩個正整數(shù)的最大公約數(shù)示例代碼

    在C語言中求兩個數(shù)的最大公約數(shù)是學習循環(huán)語句的非常經(jīng)典的問題,下面這篇文章主要給大家介紹了關(guān)于C語言求兩個正整數(shù)的最大公約數(shù)的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • 深入理解c++20 concepts

    深入理解c++20 concepts

    本文主要介紹了深入理解c++20 concepts,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • C++模擬實現(xiàn)string的示例代碼

    C++模擬實現(xiàn)string的示例代碼

    這篇文章主要為大家詳細介紹了C++模擬實現(xiàn)string的相關(guān)資料,文中的示例代碼講解詳細,對我們學習C++有一定的幫助,需要的可以參考一下
    2022-11-11
  • C語言之把數(shù)組名作函數(shù)參數(shù)的四種情況說明

    C語言之把數(shù)組名作函數(shù)參數(shù)的四種情況說明

    這篇文章主要介紹了C語言之把數(shù)組名作函數(shù)參數(shù)的四種情況說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 解決scanf_s輸入%d%c%d格式錯誤的問題

    解決scanf_s輸入%d%c%d格式錯誤的問題

    這篇文章主要介紹了解決scanf_s輸入%d%c%d格式錯誤的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12

最新評論

万荣县| 凭祥市| 桃园县| 嵩明县| 嵩明县| 集贤县| 托克逊县| 杭州市| 霸州市| 江孜县| 巴彦淖尔市| 福清市| 涿鹿县| 禹城市| 额敏县| 庐江县| 凯里市| 靖远县| 金溪县| 家居| 茂名市| 大理市| 威海市| 团风县| 天等县| 义马市| 德安县| 昂仁县| 监利县| 崇明县| 阿勒泰市| 额济纳旗| 崇礼县| 南丹县| 丹阳市| 屏边| 敖汉旗| 饶阳县| 炉霍县| 利津县| 白玉县|