• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:関数をまとめる)

関数をまとめる方法と、その利点

TT414の回答

  • TT414
  • ベストアンサー率18% (72/384)
回答No.7

>#define add(x,y) (x+y) >とする手もあります。 >このケースではオススメしませんけど。 このケースどころか、人に勧める以前に、発表するだけでも恥ずかしい記述です。 ど素人以外は「#define add(x,y) ((x)+(y))」と記述します。

関連するQ&A

  • switch文のエラーについて

    次のプログラムを実行したら slect.cpp:In member function `void select::setBlood(std::string)': slect.cpp:16:error: switch quantity not an integer が出ました。switch文の何が不完全ですか?教えてください。 #include<iostream> #include<string> using namespace std; class select { private: string blood; public: void setBlood(string b); void show(); }; void select::setBlood(string b){ blood=b; switch(b){ case 'A': cout<<"A is best"<<endl; break; case 'B': cout<<"B is best"<<endl; break; case 'AB': cout<<"AB is best"<<endl; break; case 'O': cout<<"O is best"<<endl; break; default: cout<<"crazy"<<endl; break; } } void select::show(){ cout<<"Your blood type is :"<<blood<<endl; } int main(){ string b; select Q; cout<<"Please Input your Blood type in A or B or AB or O:"<<endl; cin>>b; Q.setBlood(b); Q.show(); return 0; }

  • 【C++】関数ポインタの使い方

    関数ポインタの使い方で悩んでいます。 下記の (1)のようにグローバルメソッドとして定義したメソッドを関数ポインタに代入することは出来るのですが、 (2)のようにクラスのメンバメソッドとして定義したメソッドは関数ポインタに代入することは出来ませんでした。 Error:バインドされた関数へのポインターは関数の呼び出しにのみ使用できます。 というエラーが発生します。 関数ポインタに外部参照でメソッドを代入することは出来ないのでしょうか? -----(1)------------------------------------------------------------------ #include "stdafx.h" #include <iostream> using namespace std; int f(int a, int b){ return a * b; } int _tmain(int argc, _TCHAR* argv[]) { typedef int (* FUNC_POINTER)(int, int); FUNC_POINTER fp; fp = f; cout << fp(1,2) <<endl; getchar(); return 0; } ------------------------------------------------------------------------- -----(2)------------------------------------------------------------------ #include "stdafx.h" #include <iostream> using namespace std; class MPointerList{ public: int f(int a, int b){ return a * b; } }; int _tmain(int argc, _TCHAR* argv[]) { typedef int (* FUNC_POINTER)(int, int); FUNC_POINTER fp; //fp = f; MPointerList mP; fp = mP.f; cout << fp(1,2) <<endl; getchar(); return 0; } -------------------------------------------------------------------------

  • 添字演算子

    #include <iostream> class hoge{ private: int a; public: hoge(){ a = 0; } int operator+(int fuga){ a = a + fuga; return a; } int operator[](int fuga){ return 1; } }; int main(){ hoge* p; p = new hoge; std::cout << ((*p) + 5) << std::endl; std::cout << ((*p)[1] ) << std::endl; // 5 // 1 // std::cout << ((*p) [] 1); エラーです。何故ですか? }

  • c++テンプレート関数名

    #include <iostream> #include <string> using namespace std; template <class T> T maxdt std(T a,T b){ if (a>b) return a;else return b; } int main (){ int id1,id2,n1 = 1000,n2 = 2000; double ddt; string sdt,s1 = "abcd",s2 = "jklm"; id1 = maxdt(n1,n2); id2 = maxdt(3000,4000); ddt =maxdt (55.55,66.66); sdt = maxdt(s1,s2); cout << "id1" << id1 <<endl; cout << "id2" << id2 <<endl; cout << "ddt" << ddt <<endl; cout << "sdt" << sdt <<endl; return 0; } というプログラムが例題であり、僕は横着をしてmaxdtをmaxという名前としてプログラムを作ったところ、 オーバーロード関数の呼び出しがあいまいです。 というエラーが表示されコンパイルできませんでした。 環境はubuntu 12.04,gcc 4.6.3 g++でコンパイルしました。 ググってもテンプレート関数の名前の付け方に規則は存在していないようなのですが、 これは直前に作ったプログラムの影響でしょうか?(maxという関数を作っていたので) それとも別の規則が存在したりするのでしょうか?

  • 関数ポインタ?

    下記のようにstaticでないメンバ関数を 関数ポインタのように指定できることを最近知ったのですが 下記コードにでてくるfpは一般的に何と呼ばれるのでしょうか? これも「関数ポインタ」で良いのでしょうか? この事について調べたかったのですが、呼び方がわからず 検索できなかったのでここで質問している次第です。宜しくお願い致します。 #include <iostream> class CTest{ public:   int a;   CTest(int _a){ a=_a;}   int fnc(int test){     return a + test;   } }; int main() {   CTest* test = new CTest(3);   int (CTest::*fp)(int) = &CTest::fnc;   std::cout << (test->*fp)(5) << std::endl;   return 0; }

  • C++でどうすればcoutで表示できるのですか?

    C++についての質問です coutで表示するときに kannsuu関数の返り値を main関数とmain関数の cout << ++kannsuu(i) << endl; の部分を変更せずに kannsuu関数のみを変更して インクリメントして「2」と表示したいのですが どのようにすればいいですか? #include <iostream> using namespace std; int kannsuu(int i) {return i;} int main() { int i = 1; cout << ++kannsuu(i) << endl; return 0; }

  • do-while 文でコンパイルエラーがでます。。

    下記グローバル関数Run内のdo-while文で、 error C2059: syntax error : '}' というコンパイルエラーが起きるのですが、どうしても理由がわかりません; do-whileをコメントアウトしコンパイルしてみたところ正常に動くので、そこがおかしいのは間違いないと思います。 どなたかお力添えをお願いいたします。。 #include <iostream> #include "BinaryTree.cpp" using namespace std; void Run(); int main() { Run(); return 0; } void Run() { BinaryTree<int> *bt; char input; do { cout<<"Menu Display"<<endl; cout<<"---------------------"<<endl; cout<<"1> Enter data"<<endl; cout<<"2> Print Tree"<<endl; cout<<"3> Reverse Tree"<<endl; cout<<"4> Quit"<<endl; cout<<"Enter your choice: "; cin>>input; switch (input) { case '1': int elem; cout<<"Enter data for tree: "; cin>>elem; if (bt == NULL) bt = new BinaryTree<int>(elem); else bt->Insert(elem); break; case '2': if (bt == NULL) cout<<"The tree is empty."<<endl; else { cout<<"Tree size = "<<bt->Size(bt)<<endl; cout<<"---------------------"<<endl; bt->Inorder(bt); } break; case '3': if (bt == NULL) cout<<"The tree is empty."<<endl; bt->Reverse(bt); break; }while (input != '4'); } }

  • 条件の配列

    表題の件に付いて質問させていただきます。 複数の条件分岐を簡便に記述する方法を探しております。 簡単な例でいいますと、 int main(){  int value=3;  if(value==0)std::cout <<"value="<<0 <<std::endl;  if(value==1)std::cout <<"value="<<1 <<std::endl;  if(value==2)std::cout <<"value="<<2 <<std::endl;  if(value==3)std::cout <<"value="<<3 <<std::endl; } 上記プログラムの複数の条件分岐をfor文をもちいてかきかえる場合、conditionクラスを作成して class condtion{ private:  int *a;  int b; public  void SetPointer(int* value){a=value}  void SetVal(int val){b=val}  bool isTrue(){   if(*a==b){return true;}   else{ return faulse;}  } } int main(){  int value=3;  condition c[4]; //クラス  for(int i=0;i<4;i++){   c[i]->SetPointer(&value);   c[i]->SetVal(i);  }  for(int i=0; i<4; i++){   if(c[i].isTrue())std::cout<<"i="<<i<<std::endl;  } } と実装できそうな気がするのですが、(上記プログラムは動作確認しておりません。) 変数が増えたり、条件が複雑になると少々繁雑になってしまいそうな気がしております。 もっと簡便に条件分岐を配列として処理する方法は存在しますか? 関数ポインタなどがつかえるのでしょうか? アドバイスよろしくお願いいたします。

  • javaプログラミング

    以下のプログラムがコンバイルできません なぜでしょうか? #include<iostream> #include<cmath> using namespace std; int main() { double a, b; cout << "実数 a の値を入力してください " << endl; cin >> a; cout << "実数 b の値を入力してください " << endl; cin >> b; cout << a << " の " << b << " 乗は " << pow(a, b) << "です" << endl; return 0; }

    • ベストアンサー
    • Java
  • 仮想関数と継承について

    #include <iostream> using namespace std; class AAA { public: virtual aaa() { cout <<"aaa(void)"<<endl;} }; class BBB : public AAA { public: virtual aaa(int a) { cout <<"aaa(int)"<<a<<endl;} }; int main() { BBB bbb; bbb.aaa();<--これがエラーになります。 return 0; } どうして、既定クラスの引数なしのaaa()はよぶことが できないのでしょうか?