#include<iostream>
using namespace std;
class testClass
{
public:
int sum();
//Postcondition: Returns the sum of the
// private data members.
void print();
//Prints the values of the private data members.
testClass();
//default constructor
//Postcondition: x = 0; y = 0;
testClass(int a, int b);
//Constructor with parameters
//Initializes the private data members to the
//values specified by the parameters.
//Postconditon: x = a; y = b
private:
int x;
int y;
};
int sum()
{
int a, b, total;
total = a + b;
return total;
}
void print()
{
cout<< "sum = " << sum() << endl;
}
testClass::testClass()
{
x = 0;
y = 0;
}
testClass::testClass(int a, int b)
{
x = a;
y = b;
}
このプログラムは100%コンパイルしますが、実行すると次のエラーが発生します。 -------------- ------設定:mingw5 - CUIデバッグ、ビルダータイプ:MinGW --------------------:未定義の `testClass :: sum() '
ファイルの依存関係をチェックしています... リンクしています。 ... [エラー] C:\のDev-CPP \ MAlikChapter1 \ Exercise14.cpp:56:testClass::sum()' [Error] C:\Dev-Cpp\MAlikChapter1\Exercise14.cpp:58: undefined reference to
TestClassを未定義の参照::プリント()」 [エラー] collect2は:ldは
int main()
{
int m, n;
testClass mySum;
testClass myPrint;
mySum.sum();
myPrint.print();
}
これはからのプログラム例です:マリク「Cを使用してデータ構造++」クラス外でそれらを実装する際に
ここで、 'testClass :: sum'と' testClass :: print'を実装しましたか? –
'sum'と' print'関数を宣言しますが、 'sum'と' print'の前に 'testClass ::'を追加するのを忘れて –
と決して定義しないでください。クラス外で定義する場合は、追加する必要があります –