C++驱动程序看不到头文件

C++ driver program not see header file

本文关键字:文件 看不到 驱动程序 C++      更新时间:2023-10-16

好的,我现在缺少什么 我的驱动程序文件看不到头文件。 以下是文件。 第一个文件将看到标头,但project1_task2不会看到标头。 这是针对在 centOS 系统上使用记事本++的作业。 我错过了什么。 谢谢。当我编译project1_task2.cpp时,它说:

project1_task2.cpp:在函数 'int main((' 中: project1_task2.cpp:27:9: 错误:"类复合体"没有名为"添加"的成员 c3 = c1.add(c2(; ^ project1_task2.cpp:30:9: 错误:"类复合体"没有名为"sub"的成员 c3 = c1.sub(c2(; ^ project1_task2.cpp:33:9: 错误:"类复合体"没有名为"mult"的成员 c3 = c1.mult(c2(; ^ project1_task2.cpp:36:9: 错误:"类复合体"没有名为"div"的成员 c3 = c1.div(c2(;

页眉:

//newcomplex1.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double realPart;
double imaginaryPart;
public:
Complex(double real=0, double imag=0); //constructor that initializes the complex number by default arguments
double getReal(); //get function that returns the real part of the complex number
double getImag(); //get function that returns the imaginary part of the complex number
void setReal(double real); //set function that sets the real part of the complex number
void setImag(double imag); //set function that sets the imaginary part of the complex number
void print(); //function that displays the complex number
friend Complex add(const Complex&, const Complex&); //function that returns the sum of two complex numbers
friend Complex sub(const Complex&, const Complex&); //function that returns the difference of two complex numbers
friend Complex mult(const Complex&, const Complex&); //function that returns the product of two complex numbers
friend Complex div(const Complex&, const Complex&); // function that returns the quotient of two complex numbers
};
#endif

文件 1:

//newcomplex1.cpp
#include <iostream>
#include<iomanip>
#include"newcomplex1.h"
using namespace std;
Complex::Complex(double real, double imag)
{
realPart = real;
imaginaryPart = imag;
}
double Complex::getReal() {
return this->realPart;
}

double Complex::getImag() {
return this->imaginaryPart;
}
void Complex::setReal(double real) {
realPart = real;
}

void Complex::setImag(double imag) {
imaginaryPart = imag;
}
Complex add(const Complex& c1, const Complex& c2) {
Complex temp = Complex();
temp.realPart = c1.realPart + c2.realPart;
temp.imaginaryPart = c1.imaginaryPart + c2.imaginaryPart;
return temp;
}
Complex sub(const Complex& c1, const Complex& c2) {
Complex temp = Complex();
temp.realPart = c1.realPart - c2.realPart;
temp.imaginaryPart = c1.imaginaryPart - c2.imaginaryPart;
return temp;
}
Complex mult (const Complex& c1, const Complex& c2) {
Complex temp = Complex();
temp.realPart = (c1.realPart * c2.realPart) - (c1.imaginaryPart * c2.imaginaryPart);
temp.imaginaryPart = (c1.realPart * c2.imaginaryPart) + (c1.imaginaryPart * c2.realPart);
return temp;
}
Complex div (const Complex& c1, const Complex& c2) {
Complex temp = Complex();
temp.realPart = (c1.realPart*c2.realPart + c1.imaginaryPart*c2.imaginaryPart)/(c2.realPart*c2.realPart +c2.imaginaryPart*c2.imaginaryPart);
temp.imaginaryPart = (c1.imaginaryPart*c2.realPart - c1.realPart*c2.imaginaryPart)/(c2.realPart*c2.realPart +c2.imaginaryPart*c2.imaginaryPart);
return temp;
}
void Complex::print() {
cout<<fixed<<setprecision(2);
if(realPart!=0)
cout << realPart;
if(imaginaryPart!=0)
{
if(imaginaryPart==1)
cout << "+i";
else if(imaginaryPart==-1)
cout << "-i";
else if(imaginaryPart>0 && realPart!=0)
cout <<"+"<< imaginaryPart<< "i";
else
cout << imaginaryPart<< "i";
}
cout<<endl;
}

司机:

//project1_task2.cpp
#include <iostream>
#include "newcomplex1.h"
using namespace std;
int main()
{
double real, imag;
cout<<"Enter the first complex real_part imaginary_part : "<<endl;
cin>>real>>imag;
Complex c1(real, imag);
cout<<"Enter the second complex: real_part imaginary_part : "<<endl;
cin>>real>>imag;
Complex c2(real, imag);

cout<<"The two complex numbers entered are :"<<endl;
cout<<"c1 = "; c1.print();
cout<<"c2 = "; c2.print();
Complex c3;
cout<<"The arithmetic operations on these two numbers: "<<endl;
cout<<"c1 + c2 = ";
c3 = c1.add(c2);
c3.print();
cout<<"c1 - c2 = ";
c3 = c1.sub(c2);
c3.print();
cout<<"c1 * c2 = ";
c3 = c1.mult(c2);
c3.print();
cout<<"c1 / c2 = ";
c3 = c1.div(c2);
c3.print();
return 0;
}

你需要使用

c3 = add(c1, c2);

而不是

c3 = c1.add(c2);

因为add不是成员函数:

friend Complex add(const Complex&, const Complex&);