简体   繁体   中英

C++ driver program not see header file

ok, what am I missing now my driver file does not see the header file. here are the files. the first file will see the header, but project1_task2 will not see the header. this is for an assignment im late uugghh im using notepad++ on a centos system. what am i missing. thank you. when i compile project1_task2.cpp it says:

project1_task2.cpp: In function 'int main()': project1_task2.cpp:27:9: error: 'class Complex' has no member named 'add' c3 = c1.add(c2); ^ project1_task2.cpp:30:9: error: 'class Complex' has no member named 'sub' c3 = c1.sub(c2); ^ project1_task2.cpp:33:9: error: 'class Complex' has no member named 'mult' c3 = c1.mult(c2); ^ project1_task2.cpp:36:9: error: 'class Complex' has no member named 'div' c3 = c1.div(c2);

Header:

//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

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

Driver:

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

You need to use

c3 = add(c1, c2);

Instead of

c3 = c1.add(c2);

Because add is not a member function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM