Home > Mobile >  C classes in Header Files
C classes in Header Files

Time:12-02

I am a real beginner in C and am having some major problems with my current task. The goal is to implement basic Complex arithmetic in C , but all the videos/webistes I used to get in touch with this topic did not include a .hpp (Complex.hpp) that we need to use to run our tests. But adding the Complex{...} class to this header file causes several problems in my code.

#ifndef H_lib_Complex
#define H_lib_Complex

namespace arithmetic {

class Complex {
    public:
    double re, im;

    Complex();                   //init Complex with (0,0)
    Complex(double r);           //init Complex with (r,0)
    Complex(double r, double i); //init Complex with (r,i)
    double real(Complex c);      //return real part of Complex
};
} #endif

And my Complex.cpp looks like this:

#include "lib/Complex.hpp"          <- no complaining about the path
namespace arithmetic {

Complex::Complex() {
    this->re = 0;
    this->im = 0;
}
Complex::Complex(double r) {
    this->re = r;
    this->im = 0;
}
Complex::Complex(double r, double i) {
    this->re = r;
    this->im = i;
}

double Complex::real(Complex c){
    return c.re;
}

//add some more functionality like abs, norm, conj,...

} // namespace arithmetic

Now, If I want to test my code, the test file shows the following error messages:

#include "lib/Complex.hpp"              <-- lib/Complex.hpp: No such file or directory(gcc)
#include <cmath>
#include <sstream>
#include <gtest/gtest.h>

using namespace arithmetic;
using namespace std;

TEST(TestComplex, realImag) {
    Complex a;
    Complex b = 42.0;
    Complex c(1.0, 2.0);

    ASSERT_FLOAT_EQ(a.real(), 0);
    ...(more tests)

At ASSERT_FLOAT_EQ it shows:

#define ASSERT_FLOAT_EQ(val1,val2) ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, val1, val2) 
Expands to: 
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, a.real(), 0)

too few arguments in function call C/C  (165)

But if I understand it correctly, this test received two values a.real and 0, so why did it still not work?

CodePudding user response:

real takes an argument of type Complex. I think you meant

double Complex::real(){ return this->re; }

change the declaration accordingly too.

  • Related