计算三角形面积的错误?
我正在研究一个程序,该程序读取三角形的三角形的底部和高度,然后报告该区域。这是我到目前为止有:计算三角形面积的错误?
#include<iostream>
using namespace std;
// a simple triangle class with a base and a height
class Triangle {
public:
double setbase(double x) { //was void but changed it to double
base = x;
}
double setheight(double y) {
height = y;
}
double getbase() {
return base;
}
double getheight() {
return height;
}
private:
double base, height;
};
double area(double, double);
int main() {
Triangle isoscoles1;
isoscoles1.setbase;
isoscoles1.setheight;
cin >> isoscoles1.setheight;
cin >> isoscoles1.setbase;
double x = isoscoles1.getbase;
double y = isoscoles1.getheight;
cout << "Triangle Area=" << area(x,y) << endl;
system("pause>nul");
}
double area(double x, double y) {
double a;
a = (x*y)/2;
return a;
}
这是错误它给了我: -
Severity Code Description Project File Line Suppression State
Error C3867 'Triangle::setbase': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 50
Error C3867 'Triangle::setheight': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 51
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Project1 c:\users\ku\desktop\test\project1\main.cpp 54
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Project1 c:\users\ku\desktop\test\project1\main.cpp 55
Error C3867 'Triangle::getbase': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 56
Error C3867 'Triangle::getheight': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 57
我在做什么错在这里?
的问题是,在这些线路:
cin >> isoscoles1.setheight;
cin >> isoscoles1.setbase;
记住setheight
和setbase
是成员函数,而不是实际的变量。这就像写这样的:
void doSomething(double arg) {
...
}
cin >> doSomething; // Error - can't read into a function!
在上述情况下,你真的做这样的事情:
double value;
cin >> value;
doSomething(value);
这个分离出来,从调用该函数读取值。
基于此,看看您是否可以考虑如何使用类似的解决方案来解决自己的问题。
这里还有一个问题,正如评论中指出的那样。看看这些行:
isoscoles1.setbase;
isoscoles1.setheight;
回想一下我们的doSomething
函数。你通常会有这样的代码行吗?
doSomething;
您可以安全地删除这两行;他们实际上没有做任何事情,并且导致了一些你看到的错误。
错误出现在_attempted_函数'getbase' /'getheight'调用中的缺失圆括号中。 –
@AlgirdasPreidžius好的。答案已更新! – templatetypedef
谢谢,这澄清了一些事情。下面的代码现在完美地工作 – LogomonicLearning
这很好。
class Triangle {
public: void setbase(double x) { //was void but changed it to double base = x; }
void setheight(double y) {
height = y;
}
double getbase() {
return base;
}
double getheight() {
return height;
}
private: double base; double height; };
double area(double, double);
int main() {
Triangle isoscoles1;
double x;
double y;
cin >> x >> y;
isoscoles1.setheight(y);
isoscoles1.setbase(x);
double b = isoscoles1.getbase();
double h = isoscoles1.getheight();
cout << "Triangle Area=" << area(x,y) << endl;
system("pause>nul");
return 0;
}
double area(double x, double y) { double a; a = (x*y)/2;
return a;
}
考虑从[好书]学习(https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list ),而不是随机编码。 –
不是很有用。但感谢您访问 – LogomonicLearning
为什么它没有用?你所犯的错误如此微不足道,关于这些错误的语法应该在任何好书的第一章中讨论。通过建议读一个,我可以防止你在将来制造更多类似的问题。 –