• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

conditional statements to test overloaded opertaors on class

I am creating a class to test operator functions and my compiler is responding with an error that says condtional statements are not allowed on my class type.  Any ideas?

Thanks,

Cam
0
dauler
Asked:
dauler
  • 2
1 Solution
 
Jaime OlivaresCommented:
will be easier to help you if you post your code
0
 
daulerAuthor Commented:
Here is my code broken up in my ComplexTest Class header and source followed bt my Complex Class header and source

Thanks So much!
# ifndef _COMPLEXTEST_H_
# define _COMPLEXTEST_H_

#include "complex.h"

class ComplexTest
{
public:
      //Constructor
      ComplexTest() {}
      ComplexTest(int real, int imag): _complex(real, imag) {}
      ComplexTest(const ComplexTest& ct) : _complex(ct._complex) {}

      //Destructor
      ~ComplexTest() {}

      //Test Accessors and Modifiers
      void test_Accessors();
      void test_Modifiers();

      //Test Other Functions
      void test_AssignmentOp(const Complex&);
      void test_EqualOp(const Complex&);
      void test_AdditionOp(const Complex&);
      void test_SubtractionOp(const Complex&);
      void test_NegOp(const Complex&);
      void test_MultOp(const Complex&);
      void test_ExtractOp(istream&, Complex&);
      void test_InsertOp(ostream&, const Complex&);

      

private:
      Complex _complex;
};
#endif //_COMPLEX_H_
//Cameron Dauler Lab 5 CS 162
//complextest.cpp
//

#include <iostream>
#include <cstdlib>
#include "complextest.h"
using namespace std;

void ComplexTest::test_Accessors()
{
      cout << "\nTesting Accessors..." << endl
            << "Complex real number = " << _complex.real() << endl
            << "Complex imaginary number = " << _complex.imag() << endl;
}
void ComplexTest::test_Modifiers()
{
      int real, imag;
      real = rand();
      imag = rand();

      cout << "\nTesting modifiers with random integers..." << endl;
             _complex.real(real);
             _complex.imag(imag);
             if (_complex.real() != real || _complex.imag() != imag)
                   cout << "Failed modifier test!" << endl;
             else cout << "Complex random real = " << _complex.real() << endl
                           << "complex random imaginary = " << _complex.imag()
                           << endl;
                    cout << "Passed modifier test!" << endl;
}
void ComplexTest::test_AdditionOp(const Complex& ct)
{
      
      Complex additionTestValue;
      additionTestValue = ct + ct;
      cout << "\nTesting complex addition operator..." << endl;
      cout << additionTestValue << endl;
      cout << "Passed addition test!";

}

      
void ComplexTest::test_SubtractionOp(const Complex& ct)
{

      Complex subtractionTestValue;
      subtractionTestValue = ct - ct;
      cout << "\nTesting complex subtraction operator..." << endl;
      cout << subtractionTestValue << endl;
      cout << "Passed subtraction test!" << endl;
}
void ComplexTest::test_MultOp(const Complex& ct)
{
      Complex multiplierTestValue;
      multiplierTestValue = ct * ct;
      cout << "\nTesting complex multiplier operator..." << endl;
      cout << multiplierTestValue << endl;
      cout << "Passed multiplier test!" << endl;
}
void ComplexTest::test_EqualOp(const Complex& ct)
{
      Complex equalityValue;
      cout << "\nTesting complex equality operator..." << endl;
      if (ct == ct)
      cout << "Passed equality test!" << endl;
      else
      cout << "Failed equality test!" << endl;
}
void ComplexTest::test_AssignmentOp(const Complex& ct)
{
      
      Complex assignmentValue;
      cout << "\nTesting complex assignment operator..." << endl;
      cout << ct << "Value before assignment" << endl;
      assignmentValue = ct;
      cout << assignmentValue << "Value after assignment" << endl;
      cout << "Passed assignment test!" << endl;
}

void ComplexTest::test_NegOp(const Complex& ct)
{
      cout << "\nTesting complex unary negation operator..." << endl;
      cout << -ct << endl;
      cout << "Passed negation test!" << endl;
}
void ComplexTest::test_InsertOp(ostream&, const Complex& ct)
{
      cout << "\nTesting complex insertion operator..." << endl;
      cout << ct << "Passes insertion operator test!" << endl;
}
void ComplexTest::test_ExtractOp(istream&, Complex& ct)
{
      cout << "\nTesting complex extractor operator..." << endl;
      cout << "Enter two numbers; one for real and one for imaginary; ";
      cin >> ct;
      cout << ct << endl;
      cout << "Passed extraction test!" << endl;
}

//Cameron Dauler Lab 4 Operator Overloading CS162 Annie Groeninger
//Header File

#ifndef _COMPLEX_H
#define _COMPLEX_H
#include <iostream>
using namespace std;

class Complex
{
      friend ostream& operator<<(ostream&, const Complex&);//Iostream friend function protos
      friend istream& operator>>(istream&, Complex&);
public:
      
      Complex(int real = 0, int imag = 0) : _real(real), _imag(imag)//Default and param
                                                                                                  //constructor
      {}
      int real() {return _real;}//accessors
      int imag() {return _imag;}
      void real(int real) {_real = real;}//modifiers
      void imag(int imag) {_imag = imag;}
      Complex& operator=(const Complex&);//Assignment op proto
      bool operator==(const Complex&)const;//Equality op proto
      Complex operator+(const Complex&)const;//Addition op proto
      Complex operator-(const Complex&)const;//Subtraction op proto
      Complex operator-()const;//Unary neg op proto
      Complex operator*(const Complex&) const;//Multiplication op proto
      

private:
      int _real, _imag;//Member variables for Real and Imaginary Number
};
#endif //_COMPLEX_H

//Cameron Dauler Lab 4 Operator Overloading CS162 Annie Groeninger
//Object File
#include "complex.h"

Complex& Complex::operator =(const Complex& rhs)//Assigment op function definition
{
_real = rhs._real;
_imag = rhs._imag;

      return *this;//returns entire object with rhs values
}
Complex Complex::operator +(const Complex& rhs)const//Addition op function definition
{
      return Complex(_real + rhs._real, _imag + rhs._imag);
}

bool Complex::operator ==(const Complex& rhs)const//Equality op function definition
{
      return (_real == rhs._real && _imag == rhs._imag);
}
Complex Complex::operator -()const//Unary negative op function definition
{
      return Complex(-_real, -_imag);
}
Complex Complex::operator -(const Complex& rhs)const//Subtraction op function definition
{
      return Complex(_real - rhs._real, this->_imag - rhs._imag);
}
Complex Complex::operator *(const Complex& rhs)const//Multiplication op function definition
{
      return Complex(_real * rhs._real, _imag * rhs._imag);
}
ostream& operator<<(ostream& osObject, const Complex& rhs)//Insertion op function defintion
{
      osObject << "Real Number = " << rhs._real
                   << "; Imaginary Number = " << rhs._imag;

      return osObject;
}
istream& operator>>(istream& isObject, Complex& rhs)//Extraction op function definition
{
      isObject >> rhs._real >> rhs._imag;

      return isObject;
}
0
 
Jaime OlivaresCommented:
I don't see any syntactical error in your code.
If you have message errors, please post them.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now