这是我的代码
#include <iostream>
using namespace std;
#include "mykouti.h"
int main()
{
Kouti koutiA(2.0, 3.2, 6.0);
Kouti koutiB(2.5, 4.0, 5.0);
Kouti koutiC;
// Display the volume of each box
cout << "KoutiA volume is: " << koutiA.calculateOgkos()
<< "\nKoutiB volume is: " << koutiB.calculateOgkos()
<< endl;
koutiC = koutiA + koutiB;
cout << "KoutiC volume is: " << koutiC.calculateOgkos() << endl;
return 0;
}
// mykouti.cpp
#include <iostream>
#include "mykouti.h"
//constructor that initialize Box dimensions
Kouti::Kouti(double length, double breadth,
double height)
{
setDimensions(length, breadth, height);
}
void Kouti::setDimensions(double mikos, double platos, double ypsos)
{
setMikos(mikos);
setPlatos(platos);
setYpsos(ypsos);
}
void setMikos(double mikos)
{
length = mikos;
}
void setPlatos(double platos)
{
breadth = platos;
}
void setYpsos(double ypsos)
{
height = ypsos;
}
double Kouti::calculateOgkos() const
{
return length*breadth*height;
}
Kouti::Kouti operator+(const Kouti& b)
{
Kouti kouti;
kouti.length = this->length + b.length;
kouti.breadth = this->breadth + b.breadth;
kouti.height = this->height + b.height;
return kouti;
}
// mykouti.h -- Box class before operator overloading
#ifndef MYKOUTI_H_
#define MYKOUTI_H_
class Kouti
{
public:
//constructor that initialize Box dimensions
explicit Kouti(double = 0.0, double = 0.0, double = 0.0);
void setDimensions(double, double, double);
void setMikos(double); //setlength()
void setPlatos(double); //setwidth()
void setYpsos(double); //setheigth()
double calculateOgkos() const;
Kouti operator+(const Kouti& b);
private:
double length;
double breadth;
double height;
};
#endif // MYKOUTI_H_
我试图将代码分成3个文件。使用deitel book C ++第9版和Stephen prata C ++ Primer Plus 当我编译代码时,我得到了这些错误:
你可以帮我解决这个问题吗?感谢
答案 0 :(得分:1)
您需要使用Kouti ::
限定您的成员函数定义void Kouti::setMikos(double mikos)
^^^^^^^
void Kouti::setPlatos(double platos)
^^^^^^^
void Kouti::setYpsos(double ypsos)
^^^^^^^
您的运营商+定义应如下所示:
Kouti Kouti::operator+(const Kouti& b)