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

c++ std::invalid_argument應(yīng)用

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

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

invalid_argument原型是
復(fù)制代碼 代碼如下:

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

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

#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;
}

運(yùn)行結(jié)果為:

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

#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;
}

其運(yùn)行結(jié)果為:

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

#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;
}

其運(yùn)行結(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*類型,并且通過(guò)#define TOSTRING(x)轉(zhuǎn)換后的類型為const char*類型,編譯器是gun那么獲取所在的函數(shù)名稱就是__PRETTY_FUNCTION__也是const char*類型。
可以看到__LINE__并沒(méi)有顯示出行號(hào),這里原因編譯器直接將__LINE__其轉(zhuǎn)換為"__LINE__"這個(gè)字符串了那么這里如何解決呢?筆者試過(guò)好多方法,最終找出來(lái)了,我們可以在#define一次,就可以正?,F(xiàn)實(shí)了。如下代碼
復(fù)制代碼 代碼如下:

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

其運(yùn)行結(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è)計(jì)模式之解釋器模式

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

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

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

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

    C語(yǔ)言雙指針?biāo)惴ㄅ笥堰^(guò)情人節(jié)我過(guò)算法

    這篇文章主要為大家介紹了C語(yǔ)言中雙指針?biāo)惴ǖ氖纠斀?,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • Qt與Web混合開(kāi)發(fā)實(shí)現(xiàn)雙向通信的示例

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

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

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

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

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

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

    深入理解c++20 concepts

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

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

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

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

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

    解決scanf_s輸入%d%c%d格式錯(cuò)誤的問(wèn)題

    這篇文章主要介紹了解決scanf_s輸入%d%c%d格式錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12

最新評(píng)論

临洮县| 龙山县| 额尔古纳市| 县级市| 南江县| 普洱| 祁阳县| 桐柏县| 新乐市| 安平县| 普陀区| 五河县| 福贡县| 镇雄县| 延安市| 安岳县| 三原县| 股票| 沿河| 台湾省| 彰化市| 堆龙德庆县| 南阳市| 沛县| 四子王旗| 偃师市| 郑州市| 镇赉县| 海安县| 疏勒县| 凯里市| 新化县| 平谷区| 梓潼县| 东兴市| 广德县| 洞头县| 五台县| 冕宁县| 绥棱县| 额济纳旗|