【发布时间】:2016-05-04 02:07:31
【问题描述】:
我很难理解为什么我在使用用户定义的类“矩形”的简单程序的上下文中出现此错误
我制作的 Rectangle 类用于通过输入长/宽,然后打印 l/w/area 来创建矩形。
到目前为止,我已经查看了这些位置以试图理解问题,但仍然无法理解问题。 https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C3867)&rd=true
Visual Studio 2015 "non-standard syntax; use '&' to create a pointer to member"
Visual Studio 2015 "non-standard syntax; use '&' to create pointer for member"
(我不明白指针是什么,我还没有在 Stroustrup: Programming -- Principles and Practice Using C++ 2nd Ed.@ Ch.10 一书中了解它们)
这是我的 Rectangle.h
#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle();
Rectangle(double dblp_length, double dblp_width);
bool is_square() const;
void set_length(double dblp_length);
double get_length() const;
void set_width(double dblp_width);
double get_width() const;
void set_area(double dblp_length, double dblp_width);
double get_area() const;
void print(ostream & output);
private:
void Rectangle::init(double dblp_length, double dblp_width);
double dbl_length, dbl_width, dbl_area;
};
我的矩形.cpp
#include "stdafx.h"
#include "Rectangle.h"
#include <iostream>
Rectangle::Rectangle() {
init(8, 8);
}
Rectangle::Rectangle(double dblp_length, double dblp_width) {
init(dblp_length, dblp_width);
}
void Rectangle::init(double dblp_length, double dblp_width) {
set_length(dblp_length);
set_width(dblp_width);
}
void Rectangle::set_length(double dblp_length) {
if (dblp_length < 0 || dblp_length > 1024) {
dblp_length = 8;
}
double dbl_length = dblp_length;
}
double Rectangle::get_length() const {
return dbl_length;
}
void Rectangle::set_width(double dblp_width) {
if (dblp_width < 0 || dblp_width > 1024) {
dblp_width = 8;
}
double dbl_width = dblp_width;
}
double Rectangle::get_width() const {
return dbl_width;
}
bool Rectangle::is_square() const {
if (get_length() == get_width()) {
return true;
}
}
void Rectangle::set_area(double dblp_length, double dblp_width) {
double dbl_area;
dbl_area = (dblp_length * dblp_width);
}
double Rectangle::get_area() const {
return dbl_area;
}
void Rectangle::print(ostream & output) {
output << "Length: " << get_length() << ", " <<
"Width :" << get_width() << ", " <<
"Area: " << get_area << endl;
}
【问题讨论】:
-
更正了错误,在我的帖子中,复制粘贴了我的代码的错误版本。
-
您在
get_area函数调用之后错过了()。看上面那行,get_width()被正确调用了。 -
在
set_area、set_width和set_length中创建全新的变量with the same name作为类的成员变量。为什么?而不是double dbl_width = dblp_width;做dbl_width = dblp_width;。 -
谢谢黑暗,我已经错过了一个小时!这使程序运行,现在我只需要修复给出不正确结果的逻辑错误。 DeiDei我也会这样做,谢谢。
-
double dbl_length = dblp_length;无效。也许你的意思是dbl_length = dblp_length;。同样的错误也发生在其他地方
标签: c++ function class c++11 visual-studio-2015