2011-01-31 20 views
1

私はC++を学びたいので、ユーザーに2つの整数を入力し、それらの正弦値と正接値を読み取る無限ループプログラムを書いています。第1の整数は第2の整数の倍数である。出力ファイルを開くことができません:C++での構築

Program1Math.h

#ifndef PROGRAM1MATH_H_ 
#define PROGRAM1MATH_H_ 
class Program1Math { 
public: 
    Program1Math(int, int); 
    void calculateSine(int); 
    void calculateTangent(int); 
    void calculateModulus(); 
}; 
#endif 

Program1Math.cpp

#include "Program1Math.h" 
#include <iostream> 
#include <math.h> 
using namespace std; 
int c; 
int d; 
Program1Math::Program1Math(int a, int b) 
{ 
    c=a; 
    d=b; 
} 
void Program1Math::calculateSine(int a) 
{ 
    cout<<"\nSine("<< a <<")\t=\t"<< sin(a); 
} 
void Program1Math::calculateTangent(int a) 
{ 
    cout<<"\nTan("<< a <<")\t=\t"<< tan(a); 
} 
void Program1Math::calculateModulus() 
{ 
    if (c%d==0) 
    { 
    cout<<"\n"<< c <<" is a multiple of "<< d <<"!"; 
    } 
    else 
    { 
    cout<<"\n"<< c <<" is not a multiple of "<< d <<"."; 
    } 
} 

Program1.cpp

#include <iostream> 
#include "Program1Math.h" 
using namespace std; 
int main() 
{ 
    int num1; 
    int num2; 
    int i=1; 

    while (i>0){ 
    cout<<"Please enter the first integer number:\n"; 
    cin>>num1; 
    cout<<"Please enter the second integer number:\n"; 
    cin>>num2; 
    Program1Math p(num1, num2); 
    p.calculateModulus(); 
    p.calculateSine(num1); 
    p.calculateTangent(num1); 
    p.calculateSine(num2); 
    p.calculateTangent(num2); 
    cout<<"\n\n"; 
    } 
    return 0; 
} 

プログラムを構築し、calculateTangentの書式の問題とは別に(正しく動作します関数)をEclipseで実行します。しかし、私はプログラムをUnix環境で動かすことはできません。プログラムはビルドされますが、実行しようとすると、次のエラーメッセージが表示されます。

Program1Math: In function `_start': 
(.text+0x0): multiple definition of `_start' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.text+0x0): first defined here 
Program1Math: In function `_fini': 
(.fini+0x0): multiple definition of `_fini' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o:(.fini+0x0): first defined here 
Program1Math:(.rodata+0x0): multiple definition of `_IO_stdin_used' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here 
Program1Math: In function `__data_start': 
(.data+0x0): multiple definition of `__data_start' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.data+0x0): first defined here 
Program1Math:(.rodata+0x8): multiple definition of `__dso_handle' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o:(.rodata+0x0): first defined here 
Program1Math: In function `_init': 
(.init+0x0): multiple definition of `_init' 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o:(.init+0x0): first defined here 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__' 
Program1Math:(.dtors+0x8): first defined here 
collect2: ld returned 1 exit status 

問題が起こっている可能性はありますか?

+0

は、フォーマットコードに '' {}ボタン(又は各行4つのスペースをインデント)を使用します。見積もり機能を使用しないでください。 –

+0

ビルドコマンドラインやMakefileなしで言うのは難しいです。 –

+0

これらは実行時エラーではなく、リンカからのものです! Richard Penningtonが示唆しているように、Unix上で構築する方法にはおそらく問題があります。おそらく同じライブラリに対して2回リンクしていますか? –

答えて

1

ビルドの設定に問題がある必要があります。まず、変数cdProgram1Math.hファイルに移動します。

Program1Math.h

#ifndef PROGRAM1MATH_H_ 
#define PROGRAM1MATH_H_ 

class Program1Math { 
    private: 
    int c; 
    int d; 

    public: 
    Program1Math(int, int); 
    void calculateSine(int); 
    void calculateTangent(int); 
    void calculateModulus(); 
}; 
#endif 

が続いMakefileのと呼ばれるファイルを作成し、そこに次の行を追加します。

Makefileの

all: 
    g++ program1.cpp Program1Math.cpp -o Program1 -Wall 

あなたと同じディレクトリにMakefileを保存他のファイル。 これで、この方法で構築して実行することができる:

> make 
> ./Program1 
関連する問題