0

This occurs for my first member function for the class 'complex_test' (the constructor) so I assume I'm getting the same error for the rest of the functions. FULL ERROR:error: 'complex_test' does not name a type. The class implementation file below:

#include <fstream>           // For ifstream, ofstream classes
#include "Complex.hpp"       // For complex class declaration
#include "ComplexTest.hpp"   // For complex_test class declaration

complex_test::complex_test()
{
}

void complex_test::run()
{
    std::ifstream fin("complex-in.txt");

    std::ofstream fout("complex-out.txt");

    int n; fin >> n;

    for (int testcase = 0; testcase < n; ++testcase) {
        run_test(fin, fout);
    }

    // Close the input and output files.
    fin.close();
    fout.close();
}

void complex_test::run_test(ifstream& fin, ofstream& fout)
{
    // Read the four double values.
    double real1, imag1, real2, imag2;
    fin >> real1 >> imag1 >> real2 >> imag2;

    complex c1(real1, imag1);

    complex c2(real2, imag2);

    complex sum      = c1.add(c2);
    complex diff     = c1.sub(c2);
    complex product  = c1.mul(c2);
    complex quotient = c1.div(c2);

    fout << "c1 = " << c1.to_string() << ", c2 = " << c2.to_string() << std::endl;
    fout << "c1 + c2 = " << sum.to_string() << std::endl;
    fout << "c1 - c2 = " << diff.to_string() << std::endl;
    fout << "c1 * c2 = " << product.to_string() << std::endl;
    fout << "c1 / c2 = " << quotient.to_string() << std::endl;
    }

The class header for complex_test class:

#ifndef COMPLEX_HPP 
#define COMPLEX_HPP

#include <fstream>     

using namespace std;

class complex_test {
public:
    complex_test();
    void run();
private:
    void run_test(ifstream& fin, ofstream& fout);
};

#endif  

In addition to this I get an error in the function implementation for the class 'complex' in line 62 for the get_real(), the compiler says it is not in scope, yet I defined it in the very same file?

Exact wording of error: 'get_real' was not declared in this scope.

#include <iomanip>      // For fixed, setprecision()
#include <sstream>      // For stringstream class
#include "Complex.hpp"  // For the complex class declaration
#include <cmath>


    complex::complex()
    {
        init(0,0);
    }

 complex::complex(double init_real, double init_imag)
 {
    init(init_real, init_imag);
 }

 double complex::get_imag()
 {
     return m_imag;
 }

    double complex::get_real()
    {
        return m_real;
    }

void complex::init(double init_real, double init_imag)
    {
        m_real = init_real;
        m_imag = init_imag;
    }

void complex::set_imag(double s)
    {
        m_imag = s;
    }

void complex::set_real(double s)
    {
        m_real = s;
    }

std::string complex::to_string()
{
    std::stringstream sout;
    sout << std::fixed << std::setprecision(4);
    sout << "(" << get_real();
    double imag = get_imag();
    if (imag < 0) {
        sout << " - " << -imag << 'i';
    } else if (imag > 0) {
        sout << " + " << imag << 'i';
    }
    sout << ")";
    return sout.str();
}



complex complex::add(complex& rhs_op)
{
    double sum_real = get_real() + rhs_op.get_real();
    double sum_imag = get_imag() + rhs_op.get_imag();
    complex sum(sum_real, sum_imag);
    return sum;
}


complex complex::div(complex& rhs_op)
{
    complex inverse = rhs_op.invert();
    complex quotient = mul(inverse);
    return quotient;
}

complex invert()
{
    double denom = std::pow(get_real(), 2) + std::pow(get_imag(), 2);
    double inv_real = get_real() / denom;
    double inv_imag = get_imag() / denom;
    complex inverse(inv_real, inv_imag);
    return inverse;
}


complex complex::mul(complex& rhs_op)
{
    double prob_real = get_real() * rhs_op.get_real() - (get_imag() * rhs_op.get_imag());
    double prod_imag = get_imag() * rhs_op.get_real() + (get_real * rhs_op.get_imag());
    complex product(prod_real, prod_imag);
    return product;
}


complex complex::negate()
{
    complex neg(-get_real, -get_imag);
    return neg;

}


complex complex::sub(complex& rhs_op)
{
    complex negation = rhs_op.negate();
    complex diff = add(negation);
    return diff;
}

The header file for class 'complex':

#ifndef COMPLEX_HPP  
#define COMPLEX_HPP

#include <string> 

class complex

{
    public:

    complex();

    complex(double,double);

    complex add(complex&);

    complex div(complex&);

    complex invert();

    complex mul(complex&);

    complex negate();

    complex sub(complex&);

    double get_imag();

    double get_real();

    void set_imag(double);

    void set_real(double);

    std::string to_string();

    private:

    void init(double , double );

    double m_real;

    double m_imag;
};


#endif
Elmer
  • 75
  • 5

1 Answers1

4

The first two lines of both of the shown header files appear to be:

#ifndef COMPLEX_HPP 
#define COMPLEX_HPP

Which means that including either header file will make subsequent inclusion of the other header file a big fat nothing.

P.S. You will also remove more possibilities of other mysterious compilation errors by immediately forgetting that "using namespace std;" is a part of the C++ language. Do yourself a favor and completely get rid of it, especially in header files. It will take a little bit of time getting used it to it, but explicitly referencing the std namespace every time it's needed will quickly become second nature, and mostly an automatic, subconscious process, very quickly.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148