我难以理解,为什么我在使用用户定义的类“矩形”Visual Studio 2015错误C3867'Rectangle :: get_area':非标准语法;使用“&”来创建一个指针成员
我做时,使用矩形类创建简单的程序的情况下得到这个错误矩形输入长度/宽度,然后打印l/w /面积。
我已经查看了这些位置,目的是试图了解问题,但仍然无法理解问题。 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"
(我不明白指针是什么,我没有在书中斯特劳斯了解他们尚未:编程 - 原理与实践使用C++第二版@章0.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;
};
My Rectangle.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'中,您创建了与该类的成员变量具有相同名称的全新变量。为什么?而不是'double dbl_width = dblp_width;'do'dbl_width = dblp_width;'。 – DeiDei