C++のプログラムのエラーを解決する方法

このQ&Aのポイント
  • C++のプログラムでコンパイル時にUndefined symbolsエラーが発生する問題の解決方法を教えてください。
  • Mac OS X 10.6.4でC++のプログラムをコンパイルする際に発生するUndefined symbolsエラーの原因と解決方法を教えてください。
  • C++のプログラムにおいて、g++ -o main main.cppでコンパイルすると、Undefined symbolsエラーが発生する場合の対処方法を教えてください。
回答を見る
  • ベストアンサー

C++のプログラムについて教えてください。

C++のプログラムについて教えてください。 以下のようなプログラムでコンパイルを行うと、エラーが起きてしまい、原因が分かりません。 OSはMac OS X 10.6.4です。 プログラム //************************************ //main.cpp #include <iostream> class test{ private: static int num; public: test(); int get_num(); }; test::test(){ num=0; } int test::get_num(){ return num; } int main(){ test t; int num = t.get_num(); std::cout<<num<<std::endl; return (0); } //*********************************** g++ -o main main.cppでコンパイルした結果 Undefined symbols: "test::num", referenced from: test::test() in ccYWvIdB.o test::test() in ccYWvIdB.o test::get_num() in ccYWvIdB.o ld: symbol(s) not found collect2: ld returned 1 exit status のようにエラーが起きます。 どなたか分かる方がいればお願いします。

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

  • ベストアンサー
  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.3

staiticなメンバ変数numの実体がどこにもないからでしょう。 #include <iostream> class test{ private: static int num; public: test(); int get_num(); }; test::test(){ num=0; } int test::get_num(){ return num; } int test:num ; // static test::numの実体 int main(){ test t; int num = t.get_num(); std::cout<<num<<std::endl; return (0); }

その他の回答 (2)

回答No.2

> private: > static int num; staticなメンバ変数は明示的に変数の実体を宣言する必要があるようです。 メンバ関数の実体と並べて int test::num; という宣言を追加してください。

  • D-Matsu
  • ベストアンサー率45% (1080/2394)
回答No.1

staticメンバ変数は、クラス内宣言以外に実体宣言が必要です。

関連するQ&A

  • gccでc++プログラムをコンパイルできません

    gccで>gcc -o test.exe test.cppとしても'main' must return 'int' と出てコンパイルできません。ファイルは本のCDに入っていた物なので間違えは無いはずなのですが・・・

  • c++ファイルのコンパイル

    OSX(10.3)を使用しているのですが、ターミナルを使用してc++ファイルをコンパイルしようとすると以下のように表示されます。 ld: Undefined symbols: std::ios_base::Init::Init() std::ios_base::Init::~Init() ___gxx_personality_v0 c++ファイル自体は #include <iostream> int main(void) { return 0; } なので間違っていないと思うのですが (> <) コンパイルはgcc -o a.exe test.cpp で行いました。 どうすれば良いのでしょうか?分かる方お願いします。

    • ベストアンサー
    • Mac
  • linuxでリンクを貼るには

    linuxでリンクを貼るには 本に書いてある通りにプログラムをつくったのですが、 うまくコンパイルできません。 本で書いてあるのはwindowsのvisual C++で、 僕の使っている環境はlinuxのg++です。 コンパイルした結果を以下に示します。 -- ファイル名:myfunc.h //declaration of the max function int max(int x, int y); -- ファイル名:myfunc.cpp //a definition of the max function int max(int x, int y) { if(x > y) return x; else return y; } -- ファイル名:sample10.5.cpp #include <iostream> #include "myfunc.h" using namespace std; int main() { int num1, num2, ans; cout << "Please input the 1st integer.\n"; cin >> num1; cout << "Please input the 2nd integer.\n"; cin >> num2; ans = max(num1, num2); cout << "The maximum value is " << ans << '\n'; return 0; } -- bash-2.05b$ g++ myfunc.cpp /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o(.text+0x18): In function `_start': : undefined reference to `main' collect2: ld はステータス 1 で終了しました -- bash-2.05b$ g++ sample10.5.cpp /tmp/ccWJNOkX.o(.text+0x6c): In function `main': : undefined reference to `max(int, int)' collect2: ld はステータス 1 で終了しました

  • プログラムのファイル分割。

    現在C++を学んでいるものです。 ソースファイルの分割についての質問です。よろしくお願いします。 環境はVisual Studio 2005です。 ファイルを      myheader.h      main.cpp      function.cpp と分ける事を考えると、例えば単純な int max(int num1, int num2){ if(num1 > num2) return num1; else return num2; } という関数を考えた場合、 myheader.hには関数maxの宣言int max(int num1, int num2);を。 function.cppには上記の関数maxの内容を。 そしてmain.cppで関数maxを使用するといったように分割し、実行することまでは自分でできます。 ですが、上記の関数をテンプレートを使って、どの型でも使用可能にするには、myheader.h、function.cppにはどのように記述したらよいでしょうか? テンプレートを使った関数は普通ですが、 template <class T> T max(T num1, Tnum2){ if(num1 > num2) return num1; else return num2; } というように記述したいと考えています。 よろしくお願いします

  • 外部ファイルでのtemplate関数の実装方法

    外部ファイルでのtemplate関数の実装方法 sub.cppにtemplate関数を実装し、 main.cppで、sub.cppのtemplate関数を呼び出す、 みたいなことをやりたいのですが、 コンパイルは通りますが、リンクエラー?になってしまいます。 以下が上記のサンプルプログラムです。 //main.cpp #include <vector> #include <iostream> using namespace std; template <typename t_ret, typename t_array> t_ret sub_t(const t_array&); int main (int argc, char *argv[]) { vector<double> v; v.push_back(1.1); v.push_back(2.2); cout << sub_t<double, vector<double> >(v) << endl; return 0; } // sub.cpp #include <vector> using namespace std; template <typename t_ret, typename t_array> t_ret sub_t(const t_array& array) { return array[0]+array[1]; } //コンパイルログ main.o: In function `main': main.cpp:(.text+0x134): undefined reference to `double sub_t<double, std::vector<double, std::allocator<double> > >(std::vector<double, std::allocator<double> > const&)' collect2: ld returned 1 exit status make: *** [main] Error 1 どなたか、対処方法を教えてください。 宜しくお願いします。

  • gccでコンパイル時のエラー

    // test.cpp #include <stdio> using namespace std; int main(int argc, char *argv[]) { printf("test\n"); return 0; } 上記コードをコンパイルしたいのですが make -k g++ -g -O2 -Wall -I. -c test.cpp -o test.obj test.cpp:2:17: stdio: No such file or directory test.cpp: In function `int main(int, char**)': test.cpp:7: error: `printf' was not declared in this scope test.cpp:7: warning: unused variable 'printf' make: *** [test.obj] Error 1 make: Target `all' not remade because of errors. となります。 #include <stdio> using namespace std; を #include <stdio.h> // using namespace std; にすれば正常に終了するのですがなぜでしょうか? gccはMingw5.1.6からインストールしたもので、 バージョンはgcc3.4.5です。 Meadow上から実行しました。 回答よろしくお願い致します。

  • boost::filesystemのコンパイル

    こんにちは。 現在boost::filesystemについて勉強しているのですが、サンプルプログラムを用いてコンパイルをしようとするとエラーが発生します。 OSは、Mac OS Xで、boostは1.46.1です。 インストール(bjamなど)は、完了しているはずです(できてなかったらごめんなさい) g++ test.cpp でコンパイルしようとすると以下のエラーメッセージが表示されます。 Undefined symbols: "boost::system::generic_category()", referenced from: __static_initialization_and_destruction_0(int, int)in ccv6DmNs.o __static_initialization_and_destruction_0(int, int)in ccv6DmNs.o "boost::system::system_category()", referenced from: __static_initialization_and_destruction_0(int, int)in ccv6DmNs.o "boost::filesystem3::detail::file_size(boost::filesystem3::path const&, boost::system::error_code*)", referenced from: boost::filesystem3::file_size(boost::filesystem3::path const&)in ccv6DmNs.o "boost::filesystem3::path::wchar_t_codecvt_facet()", referenced from: boost::filesystem3::path::codecvt() in ccv6DmNs.o ld: symbol(s) not found collect2: ld returned 1 exit status また、上記のエラーの一部を検索をかけて調べたところ g++ -lboost_system-mt -lboost_filesystem-mt test.cpp とコンパイルすればできると書かれていたのですが、 ld: library not found for -lboost_system-mt collect2: ld returned 1 exit status と表示されて、コンパイルできませんでした。 どうすればいいのかわからないので、教えて下さい。 よろしくお願いします。

  • Mac C Undefined x86_64

    C言語の勉強中です。 大学にいたころにLinuxで作ったプログラミングを自分のMacで動かしてみようと思い、コンパイルをかけると、Undefined symbols for architecture x86_64とでました。これを解決したいです。MacOSX LionのIntel Core 2 duo です。僕の試したことを一通り書きます。 僕の動かしたいプログラムのソースの#includeに <FTGL/ftgl.h><GL/glfw.h> とか見慣れないのがあったので、 FTGLをmacportで入れて、glfwをダウンロードし、GLをGLUTに変えてコンパイル cc -I /opt/local/include project.c -o project -framework OpenGL -framework GLUT -lobjc (-I /opt/local/includehはFTGLの入っている場所です) project.c:10:23: error: GLUT/glfw.h: No such file or directory うん?glfw.hをGLUTの中にコピーしたのになぁ?とりあえず現在のディレクトリにglfw.hをコピーし#include "glfw.h"に変更し再びコンパイル Undefined symbols for architecture x86_64: "_ftglRenderFont", referenced from: _outtextxy in ccvINkhN.o "_glfwInit", referenced from: _main in ccvINkhN.o "_glfwOpenWindow", referenced from: _main in ccvINkhN.o "_ftglCreateExtrudeFont", referenced from: _main in ccvINkhN.o "_ftglSetFontFaceSize", referenced from: _main in ccvINkhN.o "_ftglSetFontDepth", referenced from: _main in ccvINkhN.o "_ftglSetFontOutset", referenced from: _main in ccvINkhN.o "_ftglSetFontCharMap", referenced from: _main in ccvINkhN.o "_glfwGetWindowParam", referenced from: _main in ccvINkhN.o "_glfwGetWindowSize", referenced from: _main in ccvINkhN.o "_glfwGetMouseButton", referenced from: _main in ccvINkhN.o "_glfwSwapBuffers", referenced from: _main in ccvINkhN.o "_glfwTerminate", referenced from: _main in ccvINkhN.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status x86_64はたしか64-bitとかそんな感じの意味だった気が?学校のパソコン古かったしなんか違うのかなー?と思い -m32 -I /opt/local/include project.c -o project -framework OpenGL -framework GLUT -lobjc としてコンパイルを試みると Undefined symbols for architecture i386: "_ftglRenderFont", referenced from: _outtextxy in ccePdBJk.o "_glfwInit", referenced from: _main in ccePdBJk.o "_glfwOpenWindow", referenced from: _main in ccePdBJk.o "_glfwGetKey", referenced from: _main in ccePdBJk.o "_ftglCreateExtrudeFont", referenced from: _main in ccePdBJk.o "_ftglSetFontFaceSize", referenced from: _main in ccePdBJk.o "_ftglSetFontDepth", referenced from: _main in ccePdBJk.o "_ftglSetFontOutset", referenced from: _main in ccePdBJk.o "_ftglSetFontCharMap", referenced from: _main in ccePdBJk.o "_glfwGetWindowParam", referenced from: _main in ccePdBJk.o "_glfwGetWindowSize", referenced from: _main in ccePdBJk.o "_glfwGetMouseButton", referenced from: _main in ccePdBJk.o "_glfwSwapBuffers", referenced from: _main in ccePdBJk.o "_glfwTerminate", referenced from: _main in ccePdBJk.o ld: symbol(s) not found for architecture i386 うん?よく分からないがたしかi386は32-bitの意??どっちでやってもダメ??? うる覚えの知識を乱用し 再起動時に「2と3」を押しっぱなしにして(32-bitになる?)からのコンパイル→失敗 「6と4」を押しっぱなしにしてみる→失敗 sudo systemsetup -setkernelbootarchitecture x86_64 →失敗 sudo systemsetup -setkernelbootarchitecture i386 →失敗 sudo systemsetup -setkernelbootarchitecture default →失敗(そりゃそうですよね) うーん、ためしに gcc -vと打ってみると Target: i686-apple-darwin11とか Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.1~22/src/configure --disable-checking --enable-werror --prefix=/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.1~22/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1 Thread model: posix とでてる よく分からないがi686とx86_64ばかりあるところからターゲットは64-bitで作っている? 試しにコンパイルの通った実行ファイルに file test と打ってみる test: Mach-O 64-bit executable x86_64 おお確かにdefaultは64-bitのようだ!今度は同じファイルに対して-m32をつけてコンパイルして、file test test: Mach-O executable i386 うむ。-m32も仕事をしているよう。 これはbitの違いではなく、ダウンロードしたやつのせいとか、僕の置き場所が悪いとか、あるいは別の原因なのでしょうか?レベルの低い質問ですいません。回答お待ちしております。

  • C++のvectorについて教えてください。

    C++のvectorについて教えてください。 現在悩んでいる問題について簡単に説明するために、テストコードを書きました。 #include <vector> class IntType { private: int num; public: IntType( int n ):num( n ){}; }; std::vector< IntType > IntVector; void main() { } このコードをDebug版でコンパイルすると 1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(285) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'const size_t' から減少できませんでした 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2236) : 'std::operator <' の宣言を確認してください。 のようなエラーが出ます。 しかし、Release版でコンパイルするとコンパイルは通ります。 Debug版でコンパイルを通すにはどのようなコードを追加すればよいでしょうか? 知恵を貸してください。 よろしくおねがいします。 /** VisualStudio2008 AcademiEdition */

  • 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);