#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 ++必须有一个类型说明符。 我的类型说明符是对序列对象的引用... 我认为我已经在类及其外部正确定义了重载运算符*函数。
答案 0 :(得分:0)
您正在超载operator*=
而不是operator*
。 operator*=
是二元运算符 - 您需要:
将其定义为类体内的friend
二元函数;
friend auto& operator*=(sequence& l, sequence& r) { /* ... */ }
将其定义为类主体中的非friend
一元函数(其中左侧是隐式*this
);
auto& operator*=(sequence& r) { /* ... */ }
将其定义为类体外的自由二元函数。
auto& operator*=(sequence& l, sequence& r) { /* ... */ }
此外,您的函数参数需要类型。 const& left
没有类型 - 您可能需要const sequence& left
。