2016-12-07 10 views
-6
#include <iostream> 
#include <string> 
using namespace std; 


    class sequence{ 



    public: 
    sequence(); 
    sequence(int x); 
    sequence& operator*=(const &left, const &right); 
    ~sequence(); 
    friend istream& operator >>(istream&, sequence&); 
    friend ostream& operator <<(ostream&, sequence&); 


    void set_num_samples(); //Set no. of samples 
    float* value;   //pointer to float variable 
    void allocate_memory(); //Allocates memory 
    void set_values_array(); //Sets the values of an array 
    void check_array_input(float l); //Checks the values of the array 
    void reset_input(float j); //Resets the array 
    void de_allocate(); //deallocates memory 

    int get_num_samples(); 
    void set_full_array(int x); 
    void calculate_full(float array1[], float array2[]); 

    void combine_seq_coef(sequence& inputvoltageA); //Combines the passed  in sequence and coefficient 
    void combine_seq(sequence& objectcombine, int sample); // combine the sequences 


    private: 
    int num_samples;   //number of samples in the object sequence 




    }; 

    class FIR{ 

    public: 
    FIR(); 
    ~FIR(); 


    private: 
    int num_coefficients;  //Number of coefficients in an FIR (filter impulse response) 


     }; 



    //Constructor for each object 

    void sequence::set_num_samples() {  //Set_num_sample definition 

    cout << "Please enter the number of values in your sequence: \n"; 

    cin >> num_samples; 
    if(num_samples < 0){ 


     cout << "Value entered must be greater than 0 "<< endl; 
     cout << "Please enter the value again: " << endl; 
     cin >> num_samples; 


    } 


    };  //ok (stream op) 

    void sequence::allocate_memory() { 

    value = new float[num_samples]; //Allocated memory for Array. 


}; //ok 

    void sequence::set_values_array(){ //Set values for array 

    int k; 
    for(k=0; k<num_samples; k++){ 


     cout << "Please enter a positive value for element : " << (k+1) << endl; 
     cin >> value[k]; 
     while(value[k] < 0){ 

      cout << "Enter positive value " << endl; 
      cin >> value[k]; 

     } 
    } 

    cout << "Values have been assigned successfully! " << endl; 

}; //ok 


    //Constructor functions 

    void sequence::check_array_input(float l) { //Checks array values. 


    cout << endl << "If you would like to check input values, enter 'y' otherwise, enter 'n' to continue..." << endl; 
    char check_value; 
    cin >> check_value; 
    if (check_value == 'y') { 

     int i; 
     for (i = 0; i < l; i++) { 

      cout << "Value no. " << (i + 1) << " is:" << endl; 
      cout << value[i] << endl; 


     } 
    } 
    } 
    void sequence::reset_input(float j) { //Reset voltage value and set to 0; 

    cout << endl << "If you would like to reset all input values, enter 'y' otherwise, enter 'n' to continue..." 
    << endl; 

    char check_value2; 
    cin >> check_value2; 
    if (check_value2 == 'y') { 
     int i; 
     for (i = 0; i < j; i++) { 

      cout << "Value no." << (i + 1) << ": " << value[i]; 
      value[i] = 0; 
      cout << " is set to 0!" << endl; 


     } 
    } 
    } 
    void sequence::de_allocate(){ 

    delete[] value; //De-allocate memory 
    num_samples = 0; 
    cout << "De-allocation of input array successful, num of samples reset to 0! " << endl; 

} 

int sequence::get_num_samples(){ 

    return num_samples; 

} 

/* void sequence::calculate_full(float array1[], float array2[]){ 


    int loop; 
    for(loop=0; loop<num_samples; loop++){ 

     cout << "CoefficientA value no: " << (loop+1) << ": " << array1[loop].value[loop] << endl; 
     cout << "InputvoltageA value no." << (loop+1) << ": " << array2[loop].value[loop] << endl; 

     value[loop] = (array1[loop].value[loop])*(array2[loop].value[loop]); 
     cout << "Combined value no. " << (i+1) << ": " << value[loop] << endl; 



     cout << "The combined value gives" << full[loop] << endl; 

    } 


}; */ 

void sequence::set_full_array(int x){ 

    num_samples = x; 


} 


void sequence::combine_seq(sequence& object_combine, int sample_num){ 

    int loop; 
    for(loop=0; loop<sample_num; loop++){ 




    } 

}; 

sequence& sequence::operator*=(const &left, const &right){ 


    int y = left.get_num_samples(); 
    int x; 
    for (x=0;) 
    sequence = left.value * right.value 
    return sequence; 

} 

sequence::sequence(){ //SEQUENCE CONSTRUCTOR 

    set_num_samples(); 
    allocate_memory(); 
    set_values_array(); 
    check_array_input(num_samples); 
    reset_input(num_samples); 
    de_allocate(); 
    cout << endl << "Constructor complete!" << endl; 

}; 

sequence::sequence(int a){ //sequence constructor 2 

    set_full_array(a); 
    allocate_memory(); 

} 


/* sequence::sequence(int a){ 

    set_full_samples(); 
    allocate_memory(); 

    int i; 
    for(i=0; i<num_samples; i++){ 

     cout << "CoefficientA value no: " << (i+1) << ": " << coefficientA().value[i] << endl; 
     cout << "InputvoltageA value no." << (i+1) << ": " << inputvoltageA.value[i] << endl; 
     cout << "Combined value no. " << (i+1) << ": " << value[i] << endl; 

    } 
} 
*/ 






sequence::~sequence(){   //Destructor 


    cout << "Destructor is called" << endl; 



}; //destructor 



int main(){ 

    // Create object, constructor called 
    // Constructor calls, set_num_sample, allocate_memory, set_values_array 
    // Enters values for voltage Inputs to the sequence into an array 
    // Checks values of the array 
    // Asks user if they want to reset values and set num samples = 0. 


    do { 
     cout << "Press the Enter key to continue:" << endl; 
    } while (cin.get() != '\n'); 

    cout << "Input voltage sequence created!" << endl; 
    sequence inputvoltageA; 

    cout << endl << "CoefficientA sequence created!" << endl; 
    sequence coefficientA; 

    //Combines sequence and coefficients 

    cout << "If you would like to combine the coefficients with the input sequence A enter 'y', otherwise enter 'n'" << endl; 

    char prompt4; 
    cin >> prompt4; 
    if(prompt4 == 'y'){ 

     int x = coefficientA.get_num_samples(); 
     sequence full(x); 
     full = coefficientA*inputvoltageA; 
    } 





    /* Ask the user if they want to create new object 
    cout << "If you would like to create a new input voltage sequence enter 'y', otherwise enter 'n'" << endl; 
    char prompt3; 
    cin >> prompt3; 
    if(prompt3 == 'y'){ 
    sequence inputvoltageB; 

    } 


    cout << "CoefficientA sequence created!" << endl; 
    sequence coefficientB; 


    */ 



    /* 

    cout << "If you would like to combine this sequence with the sequence before enter 'y', otherwise enter 'n'" << endl; 
    char prompt5; 
    cin >> prompt5; 
    if(prompt5 == 'y'){ 


    combine_seq(inputvoltageA, num_samples); 



    } */ 



    return 0; 
} 

私は*演算子をオーバーロードはなぜカント? コンパイラはエラーを返します。C++には型指定子が必要です。 私の型指定子は、シーケンスオブジェクトへの参照です... 私は、クラス内でオーバーロード演算子*関数が正しく定義されていると思います。なぜ*演算子をオーバーロードできないのですか?

+1

これはまさにそのことです。オーバーロード内のパラメータには、指定された型はありません。 – Carcigenicate

+1

ビルドエラーに関する質問を投稿するときは、*完全な*エラーを含めてください。完全ビルドログは完全で未編集であり、可能な情報メモを含むことが好ましい。また、コード内のどこでエラーが発生したかを指摘することもできます(例:コメント付き)。最後に、[*** Minimal ***、Complete、Verifiable Example](http://stackoverflow.com/help/mcve)の作成方法を学んでください。 –

+0

'const&left'に型指定子はどこにありますか? – molbdnilo

答えて

0

あなたはoperator*=ないoperator*をオーバーロードしています。 ;

  • はクラス本体の内部friendバイナリ関数として定義:あなたのいずれかがする必要がある - operator*=はバイナリ演算子です

    friend auto& operator*=(sequence& l, sequence& r) { /* ... */ } 
    
  • する(左側が暗黙*thisある)クラス本体内部非friend単項関数として定義します。

    auto& operator*=(sequence& r) { /* ... */ } 
    
  • クラス体外フリーバイナリ関数として定義します。

    auto& operator*=(sequence& l, sequence& r) { /* ... */ } 
    

また、あなたの関数のパラメータは、タイプを必要としています。 const& leftにはタイプがありません。おそらくconst sequence& leftが必要です。

+0

クラス内でバイナリ関数として宣言することはできませんし、スコープ解決演算子::を使用してクラスをoutisde定義することはできますか? –

+0

パラメータが修正されましたが、エラーが発生しました: –

+0

/Users/Michael/Desktop/C++ Xcode/Michael C++/Michael C++/main.cpp:200:21:オーバーロードされた '演算子*'は、 3つのパラメータを持っています) –

関連する問題