所以这就是问题所在:
创建一个' DISTANCE'上课:
使用main函数编写应用程序以创建2个DISTANCE对象 class,即d1和d2。然后使用对象d3,对对象d1和d2求和并存储 在d3中求和,然后显示对象d3。
我试图这样做,但我收到了错误消息。有人能告诉我我的计划有什么问题吗?我自己学习它。任何帮助将不胜感激。谢谢。 :)
这是我.h文件的代码:
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
private:
double feet_1,feet_2;
double inches_1,inches_2;
public:
double inputfeet1(double feet1);
double inputfeet2(double feet2);
double inputinch1(double inch1);
double inputinch2(double inch2);
double sumFeet(double feets);
double sumInch(double inches);
};
#endif // DISTANCE_H
这个是针对.cpp文件的
#include "../include/Distance.h"
#include <iostream>
using namespace std;
double Distance::inputfeet1(double feet1)
{
if(feet1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_1 = feet1;
}
}
double Distance::inputfeet2(double feet2)
{
if(feet2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_2 = feet2;
}
double Distance::inputinch1(double inch1)
{
if(inch1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inch_1 = inch1;
}
}
double Distance::inputinch2(double inch2)
{
if(inch2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inch_2 = inch2;
}
}
double Distance::sumFeet()
{
return feet_1 + feet_2;
}
double Distance::sumInch()
{
return inches_1 + inches_2;
}
}
这个是main.cpp
#include "include/Distance.h"
#include <iostream>
using namespace std;
int main()
{
Distance d1,d2,d3;
double f1,f2,I1,I2;
double sum1,sum2;
cout<<"Distance 1.";
cout<<"\nEnter the feet: ";
cin>>f1;
cout<<"Enter the inch: ";
cin>>I1;
cout<<"Distance 2.";
cout<<"\nEnter the feet: ";
cin>>f2;
cout<<"Enter the inch: ";
cin>>I2;
d1.inputfeet1(f1);
d1.inputfeet2(f2);
d2.inputinch1(I1);
d2.inputinch2(I2);
sum1 = f1 + f2;
d3.sumFeet(sum1);
cout<<"Feet: "<<d3.sumFeet();
return 0;
}
答案 0 :(得分:1)
我只看到一些小错误,并且它们等同于您将语言中的标点符号列为错误:)
我已经注释了我在下面的代码中所做的更改:主要是您需要缩进代码并确保将所有打开的括号与右括号匹配。
接下来要做的是确保你有正确的返回类型。我回家后会发布更好的基础知识,并且有时间发布一个更好的教程,而不是一个没有什么指导价值的修复。
我有更正确的缩进.cpp文件的距离,如果你更改main.cpp中的代码
sum1 = d3.sumFeet();
而不是
sum1 = f1 + f2;
d3.sumFeet(sum1);
它将对剩余代码进行以下修改:
Distance.h
需要更改为以下内容:
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
private:
double feet_1,feet_2;
double inches_1,inches_2;
public:
void inputfeet1(double feet1);
void inputfeet2(double feet2);
void inputinch1(double inch1);
void inputinch2(double inch2);
double sumFeet();
double sumInch();
};
#endif // DISTANCE_H
和Distance.cpp
需要修改如下:
void Distance::inputfeet1(double feet1)
{
if(feet1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_1 = feet1;
}
}//added this
void Distance::inputfeet2(double feet2)
{
if(feet2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is used.";
}
else
{
feet_2 = feet2;
}
}//added this too
void Distance::inputinch1(double inch1)
{
if(inch1 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inches_1 = inch1;
}
}
void Distance::inputinch2(double inch2)
{
if(inch2 < 0)
{
cout<<"Cannot be less than zero. Default value of 0 is being used.";
}
else
{
inches_2 = inch2;
}
}
double Distance::sumFeet()
{
return feet_1 + feet_2;
}
double Distance::sumInch()
{
return inches_1 + inches_2;
}
//removed curly brace here
为了实现你的目标,我会采用更加面向对象的方法,对于这样一个简单的问题看起来似乎有些过分,但是当你最终开始使用更大的构造和层次结构时,这种做法会得到回报:
基本需求:
Feet
和Inches
,我们不会打扰它们,因为它们实际上只是示例代码中双打的不同名称,因此它们可以兼作双打。Feet
和Inches
组成的距离,因此我们希望对此做些什么....(让我们称之为DistanceBase
)Distance
因此,我们将有一个由DistanceBase
和Feet
组成的名为Inches
的班级和一个由Distance
组成的名为DistanceBase
的班级。
标题文件:
由于这是一个概念上的微不足道的(在C ++意义上不一定是微不足道的 1 )类,我们可以将所有内容都放在头文件中,这样可以使代码更加便携和透明,尽管更少安全
#pragma once
#ifndef DISTANCE_BASE_H
#define DISTANCE_BASE_H
/*
Header file for the DistanceBase class
#pragma once effectively does the same as the #ifndef loop on compilers that support it.
Both are included simply as a belt-and-braces approach
*/
// So that we have a nice simple and clear to read indexing system, lets set up an enum...
enum Units
{
FEET = 0, // typing Units::FEET will be the same as 0 but more readable and maintainable
INCHES = 1 // and likewise Units::INCHES will be the same as 1
};
class DistanceBase
{
private:
double distance[2]; // we can store the 2 doubles in one array (easier to throw around as a pair then:)
public:
void inputFeet(double feet) { distance[0] = feet; } // need a way to set the feet
void inputInches(double inch) { distance[1] = inch; } // and inches
double* getDistance() { return distance; } // a way to get both in one go
double getFeet() { return distance[Units::FEET]; } // a way to get just the feet
double getInches() { return distance[Units::INCHES]; } // a way to get just the inches
};
#endif // DISTANCE_BASE_H
现在我们有一个具有如此简单复杂程度的类,我们可以利用它为您的目的创建一个简单的类,Distance
:
标题文件
#pragma once
#ifndef DISTANCE_H
#define DISTANCE_H
/*
Header file for the distance class
*/
#include "DistanceBase.h"
class Distance
{
private:
DistanceBase *D1, *D2; // 2 instances of the DistanceBase class to use together
public:
Distance() // this is the constructor, where we can initialise
{ // the 2 instances we created earlier
D1 = new DistanceBase(); // initialise D1
D2 = new DistanceBase(); // initialise D2
}
DistanceBase* getD1() { return D1; } // this will be the function we use to access all of the properties and members of D1
DistanceBase* getD2() { return D2; } // this will be the function we use to access all of the properties and members of D2
double sumFeet() { return D1->getFeet()+D2->getFeet(); } // add the feet components of D1 and D2
double sumInches() { return D1->getInches()+D2->getInches(); } // add the inch components of D1 and D2
};
#endif // DISTANCE_H
同样,由于这个类非常简单(并且为了便于发布),我已经使所有内容都符合标题。
现在主要功能,现在更加简单,因为细节由相应的类来处理:
#include "Distance.h"
#include <iostream>
using namespace std;
int main() {
Distance *dist = new Distance(); // instantiate a Distance class as dist
dist->getD1()->inputFeet(3); // set the feet of D1 to 3
dist->getD2()->inputFeet(7); // set the feet component of D2 to 7
cout << dist->getD1()->getFeet() << endl; // check that the values stored match the values input
cout << dist->getD2()->getFeet() << endl; // check that the values stored match the values input
cout << dist->sumFeet(); // add the 2 distances together
// now lets use user inputs:
cout << "Please input the first distance in feet: ";
// we can reuse the same variable to save CPU time and memory for all our inputs....
double tempValue;
cin >> tempValue;
dist->getD1()->inputFeet(tempValue);
cout << "Please input the first distances inch component: ";
cin >> tempValue;
dist->getD1()->inputInches(tempValue);
cout << "Please input the Second distance in feet: ";
cin >> tempValue;
dist->getD2()->inputFeet(tempValue);
cout << "Please input the second distances inch component: ";
cin >> tempValue;
dist->getD2()->inputInches(tempValue);
cout << dist->getD1()->getFeet() << endl; // check that the values stored match the values input
cout << dist->getD2()->getFeet() << endl; // check that the values stored match the values input
cout << dist->sumFeet() << endl; // add the 2 distances together
cout << dist->sumInches() << endl; // add the inches components together
return 0;
}
您可以尝试自己测试的一些事情:
.cpp
和.h
个文件12.0
的值转换为英尺和英寸Feet
类和Inches
类来封装上面的功能int
表示英尺,float
表示英寸)添加错误处理以捕获文本输入而不是数字等内容。
如果这一切看起来不错,请告诉我,或者如果您需要更多信息,我很乐意提供帮助:)
1 Trivial class:在C ++中,一个简单的class
是一个定义的(class
,struct
或union
定义),因为它们既可以简单地构造,又可以轻易地复制,这意味着的是: