conditional statements to test overloaded opertaors on class

Posted on 2005-05-12
Last Modified: 2010-04-17
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?


Question by:dauler
    LVL 55

    Expert Comment

    by:Jaime Olivares
    will be easier to help you if you post your code

    Author Comment

    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
          ComplexTest() {}
          ComplexTest(int real, int imag): _complex(real, imag) {}
          ComplexTest(const ComplexTest& ct) : _complex(ct._complex) {}

          ~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&);


          Complex _complex;
    #endif //_COMPLEX_H_
    //Cameron Dauler Lab 5 CS 162

    #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;
                 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;
          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&);
          Complex(int real = 0, int imag = 0) : _real(real), _imag(imag)//Default and param
          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

          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;
    LVL 55

    Accepted Solution

    I don't see any syntactical error in your code.
    If you have message errors, please post them.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 54
    seriesUp challenge 7 81
    c# combobox autocomplete behavior 6 62
    Arduino EDI - Programming language 3 30
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now