[英]C++ classes in Header Files
I am a real beginner in C++ and am having some major problems with my current task.我是 C++ 的真正初学者,目前的任务存在一些重大问题。 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.
目标是在 C++ 中实现基本的 Complex 算法,但是我以前接触过这个主题的所有视频/网站都没有包含我们需要用来运行测试的 a.hpp (Complex.hpp)。 But adding the Complex{...} class to this header file causes several problems in my code.
但是将 Complex{...} class 添加到这个 header 文件会导致我的代码出现几个问题。
#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:我的 Complex.cpp 看起来像这样:
#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:在
ASSERT_FLOAT_EQ
它显示:
#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?但是如果我没理解错的话,这个测试收到了两个值a.real和0,为什么还是不行呢?
real takes an argument of type Complex. real 采用 Complex 类型的参数。 I think you meant
我想你的意思是
double Complex::real(){ return this->re; }
change the declaration accordingly too.也相应地更改声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.