-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;
}

Why cant i overload the * operator? The compiler gives me the error c++ must have a type specifier. My type specifier is a reference to a sequence object... I think ive defined the overload operator * function correctly in the class and outside of it.

cL Maine
  • 77
  • 7
  • 1
    It's exactly what it says. Your parameters in the overload don't have any types specified. – Carcigenicate Dec 07 '16 at 15:44
  • 1
    When posting questions about build errors, please include the *complete* error. Preferably the full build log, complete, unedited and including possible informational notes. Also point out where in the code you get the error (for example with a comment). Lastly, please learn how to create a [***Minimal***, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Dec 07 '16 at 15:45
  • Where do you see a type specifier in `const &left`? – molbdnilo Dec 07 '16 at 15:45
  • BTW: is all that code really necessary to demonstrate the problem? – molbdnilo Dec 07 '16 at 15:47
  • Also what do you think of my code structure? How can i improve? – cL Maine Dec 07 '16 at 16:01

1 Answers1

0

You're overloading operator*= not operator*. operator*= is a binary operator - you either need to:

  • define it as a friend binary function inside the class body;

    friend auto& operator*=(sequence& l, sequence& r) { /* ... */ }
    
  • define it as a non-friend unary function inside the class body (where the left-hand side is implicitly *this);

    auto& operator*=(sequence& r) { /* ... */ }
    
  • define it as a free binary function outside the class body.

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

Also, your function parameters need types. const& left does not have a type - you probably want const sequence& left.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • Can i not declare it as a binary function inside the class and then define it outisde the class using the scope resolution operator :: ? – cL Maine Dec 07 '16 at 15:56
  • The paramters have been fixed but im getting the error: – cL Maine Dec 07 '16 at 15:57
  • /Users/Michael/Desktop/C++ Xcode/Michael C++/Michael C++/main.cpp:200:21: Overloaded 'operator*' must be a unary or binary operator (has 3 parameters) – cL Maine Dec 07 '16 at 15:57
  • @cLMaine: it needs to be a `friend` in that case. Look for operator overloading tutorial/resources on Google, they explain everything you need to know – Vittorio Romeo Dec 07 '16 at 15:58
  • This is for defining it outside of the class when i have declared it inside the class. – cL Maine Dec 07 '16 at 15:58
  • I have declared it as a friend function inside the class, but when i am defining it outside the class using scope resolution operator im getting the above error. – cL Maine Dec 07 '16 at 15:59