C ++でゼロ除算エラーをキャッチする方法は?
以下は、ゼロ除算エラーをキャッチする例です。
例
#include <iostream>
using namespace std;
int display(int x, int y) {
if( y == 0 ) {
throw "Division by zero condition!";
}
return (x/y);
}
int main () {
int a = 50;
int b = 0;
int c = 0;
try {
c = display(a, b);
cout << c << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
} 出力
Division by zero condition!
上記のプログラムでは、関数display()は引数xとyで定義されています。 xをyで割った値を返し、エラーをスローします。
int display(int x, int y) {
if( y == 0 ) {
throw "Division by zero condition!";
}
return (x/y);
} main()関数で、try catchブロックを使用すると、catchブロックによってエラーがキャッチされ、メッセージが出力されます。
try {
c = display(a, b);
cout << c << endl;
} catch (const char* msg) {
cerr << msg << endl;
} -
C ++を使用してOpenCVで色を追跡する方法は?
カラートラッキングは、カラー検出に似ています。追跡の目的で、検出されたオブジェクトの領域を計算するために数行を追加し、その領域の現在の位置を追跡し、最後にOpenCVのline()関数を使用してオブジェクトの移動経路を表示しました。 次のプログラムは、C++を使用してOpenCVで色を追跡する方法を示しています。 例 #include<iostream> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> using namespace std; using
-
newを使用してC++で2D配列を宣言するにはどうすればよいですか
動的2D配列は、基本的に配列へのポインターの配列です。これは、寸法が3x4の2D配列の図です。 アルゴリズム Begin Declare dimension of the array. Dynamic allocate 2D array a[][] using new. Fill the array with the elements. Print the array. Clear the memory by deleting it. End サンプルコード