fiolettiのプロフィール

@fioletti fioletti
ありがとう数5
質問数3
回答数7
ベストアンサー数
0
ベストアンサー率
0%
お礼率
66%

  • 登録日2007/12/30
  • 演算子オーバーロードのプログラムで

    今、C++でテンプレートクラスや演算子オーバーロードを使ったプログラムの練習をしているのですが、どうしてもうまく動かなく質問させてもらうことにしました。 大まかに言えば、int型のvalueを持ったDataというクラスを用意し、演算子+と-のオーバーロードによって、クラスの足し算引き算を可能にしよう。ということなのですが、 それぞれのオーバーロードした演算子ともに、addSubtractという+と-両方に対応した関数を呼ぶことで使おうと思っています。 コードは以下のようになっています。 1 #include <iostream> 2 3 using namespace std; 4 5 template<class Comparable> 6 class Data { 7  public: 8   Data(){value = 0;} 9   Data(int i){value = i;} 10   int value; 11   Data<Comparable> operator+(const Data<Comparable> &); 12   Data<Comparable> operator-(const Data<Comparable> &); 13   Data<Comparable> Data<Comparable>::addSubtract(const Data<Comparable> & rhs, int sign); 14 }; 15 16 template<class Comparable> 17 Data<Comparable> Data<Comparable>::addSubtract(const 18 Data<Comparable> & rhs, int sign) { 18   int value1=this->value; 19   int value2=rhs.value; 20   Data<int> result(value1+(sign*value2)); 21   return result; 22 } 23 24 template<class Comparable > 25 Data<Comparable> Data<Comparable>::operator+(const Data<Comparable> & rhs) { 26   return addSubtract(&rhs, 1); 27 } 28 29 template<class Comparable > 30 Data<Comparable> Data<Comparable>::operator-(const 31 Data<Comparable> & rhs) { 31   return addSubtract(&rhs, -1); 32 } 33 34 int main() { 35   Data<int> matrix1(2); 36   Data<int> matrix2(1); 37   cout<<"data1:"<<matrix1.value<<endl; 38   cout<<"data2:"<<matrix2.value<<endl; 39 40   Data<int> result1 = matrix1+matrix2; 41   cout<<"data1+data2"<<result1.value<<endl; 42 43   Data<int> result = matrix1-matrix2; 44   cout<<"data1-data2"<<result2.value<<endl; 45 } このコードを実行したところ、以下のようなエラーメッセージが表示されて動かすことができません。 add.cpp:41: instantiated from here add.cpp:26: error: invalid conversion from `const Data<int>*' to `int' add.cpp:26: error: initializing argument 1 of Data<Comparable>::Data(int) [with Comparable = int]' アドバイスをいただけるとありがたいです。

  • 演算子オーバーロードのプログラムで

    今、C++でテンプレートクラスや演算子オーバーロードを使ったプログラムの練習をしているのですが、どうしてもうまく動かなく質問させてもらうことにしました。 大まかに言えば、int型のvalueを持ったDataというクラスを用意し、演算子+と-のオーバーロードによって、クラスの足し算引き算を可能にしよう。ということなのですが、 それぞれのオーバーロードした演算子ともに、addSubtractという+と-両方に対応した関数を呼ぶことで使おうと思っています。 コードは以下のようになっています。 1 #include <iostream> 2 3 using namespace std; 4 5 template<class Comparable> 6 class Data { 7  public: 8   Data(){value = 0;} 9   Data(int i){value = i;} 10   int value; 11   Data<Comparable> operator+(const Data<Comparable> &); 12   Data<Comparable> operator-(const Data<Comparable> &); 13   Data<Comparable> Data<Comparable>::addSubtract(const Data<Comparable> & rhs, int sign); 14 }; 15 16 template<class Comparable> 17 Data<Comparable> Data<Comparable>::addSubtract(const 18 Data<Comparable> & rhs, int sign) { 18   int value1=this->value; 19   int value2=rhs.value; 20   Data<int> result(value1+(sign*value2)); 21   return result; 22 } 23 24 template<class Comparable > 25 Data<Comparable> Data<Comparable>::operator+(const Data<Comparable> & rhs) { 26   return addSubtract(&rhs, 1); 27 } 28 29 template<class Comparable > 30 Data<Comparable> Data<Comparable>::operator-(const 31 Data<Comparable> & rhs) { 31   return addSubtract(&rhs, -1); 32 } 33 34 int main() { 35   Data<int> matrix1(2); 36   Data<int> matrix2(1); 37   cout<<"data1:"<<matrix1.value<<endl; 38   cout<<"data2:"<<matrix2.value<<endl; 39 40   Data<int> result1 = matrix1+matrix2; 41   cout<<"data1+data2"<<result1.value<<endl; 42 43   Data<int> result = matrix1-matrix2; 44   cout<<"data1-data2"<<result2.value<<endl; 45 } このコードを実行したところ、以下のようなエラーメッセージが表示されて動かすことができません。 add.cpp:41: instantiated from here add.cpp:26: error: invalid conversion from `const Data<int>*' to `int' add.cpp:26: error: initializing argument 1 of Data<Comparable>::Data(int) [with Comparable = int]' アドバイスをいただけるとありがたいです。

  • Bitmapの「biBitCount」の変更について

    ビットマップファイルのヘッダにある 「biBitCount(1 画素あたりのデータサイズ (bit))」 についてですが、 「24 - 1677万色(true color)ビットマップ」のファイルを 「1 - 2 色ビットマップ」 に変更する関数などはないでしょうか? GDI+等に関数がありそうだと思ったのですが見つからず・・・。 もしご存知の方がおられたらご教授お願いいたします。

  • Bitmapの「biBitCount」の変更について

    ビットマップファイルのヘッダにある 「biBitCount(1 画素あたりのデータサイズ (bit))」 についてですが、 「24 - 1677万色(true color)ビットマップ」のファイルを 「1 - 2 色ビットマップ」 に変更する関数などはないでしょうか? GDI+等に関数がありそうだと思ったのですが見つからず・・・。 もしご存知の方がおられたらご教授お願いいたします。

  • コマンドプロンプトからファイルへの出力

    失礼します。 いまテンプレートマッチングを用いた物体追跡を行っています。 画面内に映っている物体のなかでテンプレート画像とマッチした部分を四角で囲み追跡します、さらにその部分のピクセル座標を随時コマンドプロンプトに表示させています。 そこで質問なのですが、このコマンドプロンプトに表示させている座標をtxtファイルに出力させたいのですが可能でしょうか?? ためしに自分でやってみたのですがうまくいきませんでした。。 よろしければアドバイスお願いします。 以下に関係ある部分のソースコードを載せます。 ofstream fout("matchdata.txt"); if(!fout){ cout<<"ファイルをオープンできませんでした。\n"; return 1; }else{ cout<<"ファイルをオープンしました。\n"; } //ループ開始 while(1){ //カメラ・デバイスから画像取得 memcpy(frameImage->imageData, VI.getPixels(device1, false , true), frameImage->imageSize); cvCvtColor(frameImage,grayImage,CV_BGR2GRAY); // テンプレートマッチングを行う cvMatchTemplate( grayImage, tempGray, dstImage, CV_TM_CCOEFF_NORMED ); // テンプレートが元画像のどの部分にあるのかという情報を得る cvMinMaxLoc( dstImage, &min_interlinkage, &max_interlinkage, &min_point, &max_point, NULL ); if(max_interlinkage>0.60){ corner_point=cvPoint(max_point.x+templateImage->width , max_point.y+templateImage->height); //コマンドプロンプトへの表示 printf("point x=%d ",max_point.x+templateImage->width); printf(" y=%d ",max_point.y+templateImage->height); cvRectangle(frameImage , max_point , corner_point , CV_RGB(255,0,0) , LINE_THICKNESS , LINE_TYPE , SHIFT); printf("\n"); } fout<<"point x= "<<max_point.x+templateImage->width<<" y= "<<max_point.y+templateImage->height; fout.close(); // ウィンドウを生成する cvNamedWindow( windowNameTemplate, CV_WINDOW_AUTOSIZE ); cvNamedWindow( windowNameDestination, CV_WINDOW_AUTOSIZE ); // 画像を表示する cvShowImage( windowNameTemplate, templateImage ); cvShowImage( windowNameDestination , frameImage ); //キー入力 key = cvWaitKey(1); if(key=='q'){ break; }else if(key=='c'){ cvSaveImage("image/frame.bmp",frameImage); } }