無名ネームスペース中のstatic宣言について

このQ&Aのポイント
  • 無名ネームスペース中でstatic定数を宣言することに問題はあるのか? QACという静的コードチェックツールで「無名ネームスペース中のstatic宣言」という警告が出てしまう。
  • 無名ネームスペース内でstatic定数を宣言することについて、QACという静的コードチェックツールが警告を出す問題があるのか。
  • 無名ネームスペース中のstatic宣言について、QACという静的コードチェックツールが警告を出しているが、宣言に問題があるのか。
回答を見る
  • ベストアンサー

無名ネームスペース中のstatic宣言について

お世話になります。 以下のように無名ネームスペース内でstatic定数を宣言することに 何か問題はあるでしょうか? QACという静的コードチェックツールで 「無名ネームスペース中のstatic宣言」と 警告が出てしまいます。 test.h ----------------------- class Test{ void testFunc(); }; test.cpp ------------------------ namespace { static const int i = 3; } void Test::testFunc(){ std::cout << i << std::endl; }

質問者が選んだベストアンサー

  • ベストアンサー
  • jacta
  • ベストアンサー率26% (845/3158)
回答No.1

> 以下のように無名ネームスペース内でstatic定数を宣言することに > 何か問題はあるでしょうか? 問題はありませんが、冗長な記述であることに間違いありません。 無名名前空間は、実質的に(staticによる)内部結合の代わりに使用するものですから。

toshiaki33
質問者

お礼

なるほど。納得しました。 お答えいただきありがとうございました!

関連するQ&A

  • 2つのcppファイルから

    1つのexeファイルを作る必要性が出てきました。 (現在質問中の質問に関連しています。) 例えば file.cpp: #include<iostream> using namespace std; void Output(int x) { cout<<x<<endl; } void main(void) { for(int i=0;i<9;Output(i++)); } をコンパイルリンクすれば済むことを file1.cpp: #include<iostream> using namespace std; void Output(int x) { cout<<x<<endl; } file2.cpp: void main(void) { for(int i=0;i<9;Output(i++)); } という風に分かれているファイルをコンパイルリンクしなければならないのです。 cpp -e file.exe -c file1.cpp -c file2.cpp ではエラーになります。 どうすれば良いのでしょうか?

  • static変数のループ内での処理

    以下のc++ソース内で、static int b = 100;という式ですが、 1回目に、ここの処理を通ったときに静的変数が作成されると思います。 2回目のループのときに、ここでまたb=100と初期化されています。 ですが、ループを重ねるごとに、カウントが1増えていきます。 staticなので、これは期待通りの動作だとは思うのですが、なぜそうなるのかがわかりません。 コンパイラなどが、staticが頭に付いたものは、2度目のループ以降はこの行を無視するように しているのでしょうか? もしそうなら、他に暗黙のルール?のようなことが他にもあるのでしょうか? 考えるとさらにc++がわからなくなってきました。 #include <windows.h> #include <iostream> using namespace std; void test(){ int a = 100; cout << a << " " << &a << endl; static int b = 100; cout << b << " " << &b << endl; b++; int * abc = new int [10]; abc[0] = 9999; cout << abc <<" "<< &abc << " " << abc[0] << endl; delete [] abc; } int main(){ while (1){ Sleep(2000); test(); } } 教えてください。 よろしくお願いいたします。

  • C++のファイルの分割について教えてください。

    分割したプログラムの書き方を練習中なのですが、 下のようなプログラムを書いて、ビルドしようとしたら失敗します。 どこが悪いのか、教えていただけないでしょうか? (5)\test30\Debug\test30.exe : fatal error LNK1120: 外部参照 1 が未解決です。 1>test30 - エラー 2、警告 0 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== //test30.cpp #include <iostream> #include "sub.h" int main(void) { std::cout<<"メインプログラムです。"<<std::endl; sub1(); sub2(); return 0; } ------------------------------------------------------------- //sub1.cpp #include <iostream> void sub1(void) { std::cout<<"サブ1です。"<<std::endl; } ------------------------------------------------------------- //sub2.cpp #include <iostream> void sub1(void) { std::cout<<"サブ2です。"<<std::endl; } --------------------------------------------------------------- //sub.h void sub1(void); void sub2(void);

  • Visual C++ 2008 Express Editionで作ったプログラムがコンパイルできない

    http://www.asahi-net.or.jp/~yf8k-kbys/newcpp3.html このサイトに掲載されていたプログラムをまねて以下のプログラムをかいてみたのですがコンパイルが通りません。 原因などはどこにあるのか教えていただけないでしょうか。 // test003.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" //using namespace System; using namespace std; int main(array<System::String ^> ^args) { string name; //Console::WriteLine(L"Hello World"); //return 0; cout << "こんにちは。私はコンピュータです。" << endl; cout << "あなたの名前を入力してください。" << endl; cin >> name; cout << name <<"さん。よろしく。" << endl; }

  • C++のソースに関する質問

    以下のコードをコンパイラのテスト目的でコンパイルしました //test.cpp #include <iostream> using namespace std; void main() { cout << "test" << endl; } 使っているコンパイラはBCC Developerと言うソフトです 上に書いているコードは間違っては居ないと思うのですが エラー E2266: ファイル名が指定されていない と言うエラーが出てきます、何処のファイルをどうやって 指定するのかを教えてください

  • コンパイルエラー

    //test.cpp #include<iostream> #include<string> class test { string hoge; public: test(string str); void method() const; } 14行目  test::test(string str) : test(hoge){} void test::method() const { std::cout << "test" << std::endl; } int main() { string s; std::cout << "名前を入力してください。" << std::endl; std::cin >> s; test aaa(s); aaa.method(); } コンパイルしようとすると下記エラーになります。 エラー E2303 test.cpp 7: 型名が必要 エラー E2139 test.cpp 7: 宣言に ; がない エラー E2293 test.cpp 10: ) が必要 エラー E2147 test.cpp 14: 引数宣言は 'string' で始められない エラー E2111 test.cpp 14: ここでは型 'test' は定義できない エラー E2136 test.cpp 14: コンストラクタには戻り型は指定できない エラー E2316 test.cpp 14: 'test::test(int)' は 'test' のメンバーではない エラー E2451 test.cpp 22: 未定義のシンボル string(関数 main() ) エラー E2379 test.cpp 22: ステートメントにセミコロン(;)がない(関数 main() ) エラー E2451 test.cpp 24: 未定義のシンボル s(関数 main() ) *** 10 errors in Compile *** ■1.型名が必要 →string hoge;と宣言してる箇所でエラー発生。 …stringと宣言してるのになぜでしょうか… ■2.宣言に";"がない →string hoge;と宣言してる箇所でエラー発生。 …";"つけてるのですが… ■3.")"が必要 →test(string str);の行でエラー発生。 …")"はどこで必要なのでしょうか ■4.引数宣言はstringでははじめられない →test::test(string str) : test(hoge){}の行でエラー発生。 …C++って引数をstringで受け取れない…?どういうことでしょうか などなど疑問がいっぱいあるのですが、どこが間違っているのかよくわかりません…ご指導よろしくお願いします。

  • 質問です。。。

    いつもすみません。また質問させていただきます。 まずしたのプログラムを見てください・・・ #include <iostream> #include <string> using namespace std; int main() { string name; int tosi; cout << "こんにちは。私はコンピュータです。" <<endl; cout << "あなたの年齢を教えてください" <<endl; cin >> tosi; cout<<name<<"歳なんですか・・・老けてますね(笑)"<<endl; cout << "どこの学校ですか" <<endl; cin >> name; cout<<name<<"That's right!"<<endl; } このプログラムの8行目にあるint tosi;がありますよね。 この単語がなくても string name があったら12行目のプログラムができたのですが、どういうことなのでしょうか? 表現が一部変ですがよろしくお願いします。

  • 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++11での文字列リテラルの特殊化について

    c++11言語でのテンプレート部分特殊化についての質問です。 コメントアウト部分は出力結果です template<class T> struct VT { static const int type = 1;}; template<class T,int N> struct VT< T[N] > { static const int type = 2;}; template<class T,int N> struct VT< const T[N] > { static const int type = 3;}; template<class T> struct VT< T* > { static const int type = 4;}; template<class T> struct VT< const T*const > { static const int type = 5;}; #include<iostream> #include<typeinfo> int main(){ std::cout<<"A:"<< VT< char >::type << std::endl; // A:1 std::cout<<"B:"<< VT< char[10] >::type << std::endl; // B:2 std::cout<<"C:"<< VT< char* >::type << std::endl; // C:4 std::cout<<"D:"<< VT< char const [1] >::type << std::endl; // D:3 std::cout<<"E:"<< VT< decltype("") >::type << std::endl; // E:1 std::cout<<"G:"<< typeid( char const [1] ).name() << std::endl;// G:char const [1] std::cout<<"H:"<< typeid( "" ).name() << std::endl;// H:char const [1] } 型名を直接記述したD,G、文字列リテラルを記述したE,H。 コンパイラ毎の差はあれど、GとHの型名は同じものが表示されます。 ですが、[D:3] [E:1]と値は違い、別の特殊化テンプレートが使われています。 この部分が分かりません。 また、配列リテラル、文字列リテラルに対し部分特殊化テンプレートを宣言する方法などありましたら、ご教示お願いします。

  • templateのエラー

    すいません、C++始めたばっかりなのですが、 以下のようなエラーが出て困っています。 ClusteringMain.obj : error LNK2001: 外部シンボル ""public: void __thiscall File2Array<double>::disp(void)" (?disp@?$File2Array@N@@$$FQAEXXZ)" は未解決です。 どなたかご教授ください。 環境はWindows XP, Visual Studio.NET 以下はソースです。 --------ClusteringMain.cpp------------ #include < iostream > #include"File2Array.h" using namespace std; int main() { cout << "Hello world" << endl; File2Array<double> test; test.disp(); return 0; } ---------File2Array.cpp----------------- #include"File2Array.h" template <class Type> void File2Array<Type>::disp(void) { cout << "aaaaa" << endl; } ---------File2Array.h------------------- #include<iostream> #include<fstream> using namespace std; #ifndef FILE2ARRAY_H #define FILE2ARRAY_H 1 template <class Type> class File2Array { private: Type var; public: void disp(void); }; #endif // #define FILE2ARRAY_H 1

専門家に質問してみよう