2016-10-14 7 views
0

だから私は、私は私のIDEとしてVS 2015を使用していると私はとの問題が発生したC++エラー()関数2015

を使用したプログラミングの原則と実践では、第5章の演習に到着しましたエラー。 Codeblocks error

として:私はコードブロックで同じコードを実行する場合、私はこれを取得、しかしVisual studio error

:私は、Visual Studio 2015でコードを実行すると一時をチェックするために-280を入力した場合

私はこれを取得しますあなたが見ることができますが、VS2015はエラーメッセージを出力します。私はこれがなぜ起こっているのかを知ろうとしましたが、今まで成功していませんでした。ここで

は、私が使用しているコードです:

#include "../../std_lib_facilities.h" 

double ctok(double c) //converts c to k 
{ 

    if (c<-273.15) error("Temperature below absolute zero!"); // checks that temp isn't below absolute zero 
    double k = c + 273.15; 
    return k; 
} 

double ktoc(double k) 
{ 
    double c = k - 273.15; 
    return c; 
} 

int main() { 

    double c = 0;  //declare input variable 
    cin >> c;   //input temp 
    double k = ctok(c); //convert temp 
    cout << k << '\n';  //print out tmep 

    return 0; 
} 

誰もが二つの違いを説明することができるだろう場合、私は感謝するだろう。あなたの時間をありがとう。

+1

'error()'はどこに定義されていますか? –

+0

私がインクルードしているヘッダファイルstd_lib_facilities.hにあります。ここでは、ヘッダーファイルへの直接リンクです:http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h – Bayaz

+1

質問を編集して追加できますか? – drescherjm

答えて

2

error()関数は、どこでも処理されないstd::runtime_error例外をスローします。例外が処理されない場合、terminate()が呼び出されます。 terminate()std::terminate_handler()と呼ばれ、abort()を呼び出します。

CodeBlocksに付属している標準ライブラリのterminate_handler()はdifferenltyのように見えます。terminate()を引き起こした例外の種類と、std::exception::what()がコンソールに表示されます。 Visual Studioのstd::terminate_handler()実装ではこれを実行しませんが、必ずしもそうする必要はありません。

1

私は

#include <iostream> 
#include <errno.h> 
#include "std_lib_facilities.h" 

using namespace std; 

double ctok(double c) //converts c to k 
{ 

    if (c<-273.15) { 
     error("Temperature below absolute zero!"); // checks that temp isn't below absolute zero 
     return 0; 
    } 
    double k = c + 273.15; 
    return k; 
} 

double ktoc(double k) 
{ 
    double c = k - 273.15; 
    return c; 
} 

int main() { 

    double c = 0;  //declare input variable 
    cin >> c;   //input temp 
    double k = ctok(c); //convert temp 
    cout << k << '\n';  //print out tmep 

    return 0; 
} 

()Bjarne Stroustropのstd_lib_facilities.hを使用してコードを再コンパイルしようとしましたが、このメッセージが出て

In file included from /usr/include/c++/4.9/ext/hash_map:60:0, 
       from std_lib_facilities.h:34, 
       from test1.cpp:3: /usr/include/c++/4.9/backward/backward_warning.h:32:2: warning: 
#warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use 
-Wno-deprecated. [-Wcpp] #warning \ ^

来る他のすべてのコードを実行すると、入力した後に正常に見えます温度の値は-300

[email protected]:~/Documents/code/testcpp$ ./test1 
-300 
terminate called after throwing an instance of 'std::runtime_error' 
    what(): Temperature below absolute zero! 
Aborted (core dumped) 
+0

私はお詫びしますが、Linux Debianを使って比較しました。 Windowsに何か問題があると思いますか? –

+0

私はそれがビジュアルスタジオに関連していると思います。あなたが見ることができるように、実際にエラーメッセージ(what():絶対的なゼロ以下の温度!)を書き出す '正しい'バージョンをcodeblocksが与えるようです。 – Bayaz

+2

@Bayazデバッグビルドを実行しているVisual Studioは、キャッチされない例外がスローされると、デバッグボタンを押してデバッガを使用して、プログラムの何が問題になっているかを調べることができます。キャッチされない例外は、ほとんど常に何かが起こっていないことを意味し、Visual Studioはあなたに何を見つけようとしていますか? – user4581301

2

Wh例外がプログラムに捕捉されていない場合、異常終了します。ランタイム環境が異なると、その処理が異なります。この規格は、我々が頼りにできる一様な仕組みを保証するものではありません。

これに対処する方法以外は、何もできません。