2011-01-31 7 views
15

私は何時間も間違っていることを理解しようとしていました。 「私の問題のほとんど脱却ましたが、私はmain()に私のコードをコンパイルしようとすると、それは、この同じエラーメッセージを思い付く:メンバーのリクエスト "..." "..."非クラス型 "..."

request for member "..." in "..." which is of non-class type "..." 

、それは私がmain()に呼び出すしようすべての機能のために自分自身を繰り返し、 。何が問題ですか?私はどこでエラーが発生しているのか分かりません。

私はコードをコンパイルするために、MacBookのTe​​rminalを使用しています。ここで

は私の主な機能です:

ここ
//Program1.cpp 
//Program1Math test function 
#include "Program1Math.h" 

int main() 
{ 
    //Create a Program1Math object 
    Program1Math myProgram1Math(); 

    myProgram1Math.setNumber1(); 
    myProgram1Math.setNumber2(); 

    myProgram1Math.displayMultiple(); 
    myProgram1Math.displaySine1(); 
    myProgram1Math.displayTangent1(); 
    myProgram1Math.displaySine2(); 
    myProgram1Math.displayTangent2(); 
} 

は、クラスのメンバ関数の定義は以下のとおりです。ここ

//Program1Math.cpp 
//Program1Math member-function definitions. 
#include <iostream> 
#include <cmath> 
#include "Program1Math.h" 
using namespace std; 

//constructor makes a Program1Math, adds an blank line 
Program1Math::Program1Math() 
{ 
    cout << "/n"; 
} 

//function to assign the first integer to its appropriate location 
void Program1Math::setNumber1() 
{ 
    cout << "Please enter the first integer number /n"; 
    int numberSpot; 
    cin >>numberSpot; 
    static_cast<double>(numberSpot); 
    number1 = numberSpot; 
} 

//function to assign the second integer to its appropriate location 
void Program1Math::setNumber2() 
{ 
    cout << "Please enter the second integer number /n"; 
    int numberSpot; 
    cin >>numberSpot; 
    static_cast<double>(numberSpot); 
    number2 = numberSpot; 
} 

//function to find the sine value for a specified number 
void Program1Math::calculateSine(double inputNumber) 
{ 
    sineValue = sin(inputNumber); 
} 

//function to find the tangent value for a specified number 
void Program1Math::calculateTangent(double inputNumber) 
{ 
    tangentValue = tan(inputNumber); 
} 

//function to determine if the user-inputted numbers are multiples of each other 
void Program1Math::calculateModulus() 
{ 
    int number1Int = static_cast<int>(number1); 
    int number2Int = static_cast<int>(number2); 
    int modulusValue = number1Int % number2Int; 
    if (modulusValue == 0) 
    multiple = true; 
    else 
    multiple = false; 
} 

//function to display the whether the numbers are multiples or not 
void Program1Math::displayMultiple() 
{ 
    if(multiple == true) 
    cout<< number1 << " is a multiple of " << number2 << "!/n"; 
    else 
    cout<< number1 << "is not a multiple of " << number2 << "./n"; 
} 

//function to display the sine value of the first number 
void Program1Math::displaySine1() 
{ 
    calculateSine(number1); 
    cout << "Sine(" << number1 << ") = " << sineValue << "/n"; 
} 

//function to display the sine value of the second number 
void Program1Math::displaySine2() 
{ 
    calculateSine(number2); 
    cout << "Sine(" << number2 << ") = " << sineValue << "/n"; 
} 

//function to display the tangent value of the first number 
void Program1Math::displayTangent1() 
{ 
    calculateTangent(number1); 
    cout << "Tan(" << number1 << ") = " << tangentValue << "/n"; 
} 

//function to display the tangent value of the second number 
void Program1Math::displayTangent2() 
{ 
    calculateTangent(number2); 
    cout << "Tan(" << number2 << ") = " << tangentValue << "/n"; 
} 

は、ヘッダファイルです:

#include <cmath> 
using namespace std; 

class Program1Math 
{ 
public: 
    Program1Math(); 
    void setNumber1(); 
    void setNumber2(); 
    void calculateSine(double); 
    void calculateTangent(double); 
    void calculateModulus(); 
    void displayMultiple(); 
    void displaySine1(); 
    void displaySine2(); 
    void displayTangent1(); 
    void displayTangent2(); 
private: 
    double number1; 
    double number2; 
    double sineValue; 
    double tangentValue; 
    bool multiple; 
}; 
+0

私は答えが私のクラスの宣言と定義を必要と信じています。 –

+0

これは、クラス定義のヘッダーファイルなしでこれを診断することはできません。 –

+0

なぜ数字をintとして入力し、それらをdoubleにキャストしていますか?それらを直接ダブルで読んでみませんか? –

答えて

0

あなたはすべきです次のようにProgram1Mathインスタンスを作成します。

Program1Math myProgram1Math; 

またはヒープ上のオブジェクトを割り当てる、newキーワード使用:

Program1Math *myProgram1Math = new Program1Math(); 
+0

私はC++で "= new"を使うことはできないと思いました。 Javaのためではありませんか? – user596228

+1

-1:これはJava構文です。 C++では、この場合、 'new'は正しくありません。 – Sjoerd

+0

@Sjoerd、確かに、C++ではnewがポインタを返し、ヒープ上にメモリを割り当てます。回答が正しいかどうかを編集しました。 – mizo

17

をラインProgram1Math myProgram1Math();Program1Mathを返す関数宣言myProgram1Math()として解釈されます。

ただ、コンストラクタは(デフォルトの引数なし)のパラメータを受け入れる場合のみ()を使用し

Program1Math myProgram1Math; 

を使用しています。

EDIT:プログラムを構成するすべてのソース(.cpp)ファイルをコンパイルする必要があります。 これは、同じ拡張子を持つ同じ名前のオブジェクトファイルを生成します(Windowsでは.obj、それは.oです)。

その後、すべてのそれらの.oファイルは、実行可能プログラムを作成するためにいくつかのコンパイラ提供のライブラリに接続する必要があります。

+0

最も厄介な解析 – KitsuneYMG

+0

私は、このエラーメッセージを取得することを実行します。未定義のシンボル: "Program1Math :: displaySine1()"、から参照:から参照ccIvBynB.o で _main "Program1Math :: displaySine2を()"、 :ccIvBynB.o で_main "Program1Math :: displayTangent1()"、参照から:記号(:ccIvBynB.o LDに_main:から参照ccIvBynB.o で_main "Program1Math :: Program1Math()"、 s)が見つかりません collect2:ldが1の終了ステータスを返しました – user596228

+0

これらはメンバー関数 –

関連する問題