即使C ++中包含类头文件,类也不会命名类型错误?

时间:2018-11-24 02:48:06

标签: c++ class

在我的类'complex_test'(构造函数)的第一个成员函数中会发生这种情况,因此我认为其余函数都会遇到相同的错误。 完全错误:错误:“ complex_test”未命名类型。  下面的类实现文件:

#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;
    }

complex_test类的类头:

#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  

除此之外,我在get_real()的第62行的类'complex'的函数实现中遇到错误,编译器说它不在范围内,但我在同一文件中定义了它?

确切的错误措辞:未在此范围内声明“ get_real”。

#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;
}

“复杂”类的头文件:

#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

1 个答案:

答案 0 :(得分:4)

两个显示的头文件的前两行似乎是:

#ifndef COMPLEX_HPP 
#define COMPLEX_HPP

这意味着包括其中一个头文件将使随后包含另一个头文件变得毫无意义。

P.S。您还将通过immediately forgetting that "using namespace std;" is a part of the C++ language消除其他神秘的编译错误的可能性。帮自己一个忙,完全摆脱它,尤其是在头文件中。习惯使用它会花费一些时间,但是每次需要时显式引用std名称空间将很快成为第二天性,并且很快就会成为自动的,潜意识的过程。