Thank you in advance, i have starred at this for a couple hours now and i have come to no conclusions.

First off, this is an assignment to teach us how to use header files, this is the header that we were provided with:
Complex.h

#ifndef Complex_H
#define Complex_h

#include <iostream>

using namespace std;

class Complex {
 private:
   double real;
   double imaginary;
   
 public:
   Complex();
   Complex(double r);
   Complex(const Complex& otherComplex);
   Complex(double r, double i);
   double getReal() const;
   double getImaginary() const;

};

Complex operator+(const Complex& left, const Complex& right);
Complex operator-(const Complex& left, const Complex& right);
Complex operator*(const Complex& left, const Complex& right);
Complex operator-(const Complex& operand);

bool operator==(const Complex& left, const Complex& right);
bool operator!=(const Complex& left, const Complex& right);

ostream& operator<<(ostream& out, const Complex& number);
istream& operator>>(istream& in, Complex& number);

#endif

This is my implementation, which compiles fine and should have no problems
Complex.cc

#include "Complex.h"
#include <cstdlib>
#include <iostream>

Complex::Complex() : real(0), imaginary(0) {}

Complex::Complex(double r) : real(r), imaginary(0) {}

Complex::Complex(const Complex& otherComplex) {
  real = otherComplex.real;
  imaginary = otherComplex.imaginary;
}

Complex::Complex(double r, double i) : real(r), imaginary(i) {}

double Complex::getReal() const {
  return real;
}
double Complex::getImaginary() const {
  return imaginary;
}

Complex operator+(const Complex& left, const Complex& right)
{
  Complex result(left.getReal() + right.getReal(), left.getImaginary() + right.getImaginary());
  return result;
}

Complex operator-(const Complex& left, const Complex& right)
{
 Complex result(left.getReal() - right.getReal(), left.getImaginary() - right.getImaginary());
 return result;
}

Complex operator*(const Complex& left, const Complex& right)
{
 Complex result(left.getReal() * right.getReal() - left.getImaginary() * right.getImaginary(), left.getReal() * left.getImaginary() + right.getImaginary() * left.getReal());
 return result;
}

Complex operator-(const Complex& operand)
{
 Complex result(-operand.getReal(), -operand.getImaginary());
 return result;
}

bool operator==(const Complex& left, const Complex& right)
{
 if(left.getReal() == right.getReal() && left.getImaginary() == right.getImaginary())
   {
	return true;
   }	
}

bool operator!=(const Complex& left, const Complex& right)
{
 if(left.getReal() != right.getReal() || left.getImaginary() != right.getImaginary())
   {
	return true;
   }
}

ostream& operator<<(ostream& out, const Complex& number)
{
 out << "real = " << number.getReal() << "Imaginary = " << number.getImaginary();
 return out;
}

istream& operator>>(istream& in, Complex& number)
{
  double r, i;
  in >> r;
  in >> i;
  number = Complex(r, i);
  return in;
}

Finally this is the test file that our instructor provided
testComplex.cc

#include "Complex.h"
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
  // Test constructors\
  Complex c1, c2(6.35), c3(-5.5, 3.0);
  Complex c4(c3);
  Complex sum(0), product(1);

  cout << "sum = " << sum << "\\n";
  cout << "product = " << product << "\\n";
  cout << "c1 = " << c1 << "\\n";
  cout << "c2 = " << c2 << "\\n";
  cout << "c3 = " << c3 << "\\n";

  cout << "c2 + c3 = " << (c2 + c3) << "\\n";
  cout << "c2 - c3 = " << (c2 - c3) << "\\n";
  cout << "c2 * c3 = " << (c2 * c3) << "\\n";
  cout << "2 + c3 = " << (2 + c3) << "\\n";
  cout << "c2 + 2 = " << (c2 + 2) << "\\n";

  cout << "Please enter five(5) complex numbers (one per line)."
       << " Example: (1.25, -4.5)\\n";

  for (int i = 0; i < 5; i++) {
    cout << "=> ";
    cin >> c1;
    sum = sum + c1;
    product = product * c1;
  }
  cout << "The sum of those numbers is " << sum <<
    "; their product is " << product << "\\n";

  if (c3 == c4 && -(-c3) == c3)
    cout << "equality test success!\\n";
  else
    cout << "equality test failure!\\n";

  if (c2 != c4)
    cout << "inequality test success!\\n";
  else
    cout << "inequality test failure!\\n";
  
  return(0);
}

when i go to compile his test file into an object it tells me that on line 11, 16, 17; c3, c1, and c2 are not defined in this scope respectively.

I went to the instructor and he had no idea what was wrong with his own code, i have a test on Wednesday so im trying to make sure that i have all the implementations right, which i mean would be easy if his test file worked instead of my thinking up one.

Any ideas why the test file will not comply

thank you

Recommended Answers

All 3 Replies

The problem is line 9: remove the \ (line continuation character) at the end of the line.

Look at the trailed backslash:

// Test constructors\
  Complex c1, c2(6.35), c3(-5.5, 3.0);

It's a continuation-line character in C and C++. The next line now is a part of a single-line comment, so you don't declare c1, c2 and c2 variables!

The 2nd error is hidden (for the present):

#ifndef Complex_H
#define Complex_h

The C++ names are case sensitive ones. So your header file protection does not work: you define the different macros name.

That's all for now...

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.