-1

重複の可能性:
未定義の参照/未解決の外部シンボルエラーとは何ですか?それを修正するにはどうすればよいですか?

誰かがクラスとコンストラクターがC++でどのように機能するかについて私に時間を節約したいですか?これは私が得たものです-それは機能しません。ファイルを取得し、ファイルシステムからその名前のファイルを読み取るコンストラクターをクラスに持たせたいです。

これはヘッダーと実装です

#ifndef __narcissism__Histogram__
#define __narcissism__Histogram__

#include <iostream>
#include <sstream>  // for ostringstream
#include <iomanip>  // for setw, setfill
#include <ios>      // for hex stream manipulator
using namespace std;
#include "random.h" // for randomInteger
#include "strlib.h" // for integerToString
#include "error.h"  // for error



class Histogram {
public:

/** Constructor:
  * 
  *  */
Histogram(string filename)
{
    readfile(filename);

}


private:

int readfile(string filename);

};




#endif /* defined(__narcissism__Histogram__) */

* .cpp

 #include "Histogram.h"



 int readfile(string filename)
 {
 return 0;
 }

エラーメッセージ:

Undefined symbols for architecture i386:
"Histogram::readfile(std::string)", referenced from:
  Histogram::Histogram(std::string) in narcissism.o
ld: symbol(s) not found for architecture i386
4

2 に答える 2

2

Histogram::メンバー関数の定義に追加する必要があります。

 int Histogram::readfile(string filename)
 {
 return 0;
 }

そうしないと、同じ名前の新しいグローバル関数が定義され、メンバー関数は未定義のままになります。

于 2012-11-12T10:18:00.253 に答える
2

あなたのエラーは、 readfile が Histogram のメンバーであるため、.cpp ファイルでは次のようにする必要があることです。

int Histogram::readfile( string filename )
{
     // implement
}

あなたが書いた関数は、実際には、この時点で有効な関数です。(ヒストグラムのメンバーにアクセスしようとすると、コンパイルに失敗します。これは、おそらく readfile の適切な実装では、ファイルから読み取ったデータからこれらのメンバーを設定することを目的としています)。

代わりに、クラス Histogram のメンバーである readfile という関数に対して定義された実装がなかったため、リンク エラーが発生しました。

于 2012-11-12T10:18:42.270 に答える