[英]Visual Studio 2015 Error C3867 'Rectangle::get_area': non-standard syntax; use '&' to create a pointer to member
I am having difficulty understanding why I get this error in the context of my simple program using a user defined class "Rectangle"我很难理解为什么在使用用户定义的类“矩形”的简单程序的上下文中出现此错误
The Rectangle class I made is used to create rectangles by inputting length/width, then printing l/w/area.我制作的 Rectangle 类用于通过输入长/宽,然后打印 l/w/area 来创建矩形。
I have looked in these locations so far in an attempt to understand the issue, and still can not understand the problem.到目前为止,我已经查看了这些位置以试图了解问题,但仍然无法理解问题。 https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C3867)&rd=true 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“非标准语法;使用‘&’创建指向成员的指针”
Visual Studio 2015 "non-standard syntax; use '&' to create pointer for member" Visual Studio 2015“非标准语法;使用‘&’为成员创建指针”
(I do not understand what pointers are, I have not learned about them yet in the book Stroustrup: Programming -- Principles and Practice Using C++ 2nd Ed.@ Ch.10) (我不明白什么是指针,我还没有在 Stroustrup: Programming -- Principles and Practice Using C++ 2nd Ed.@ Ch.10 一书中了解它们)
Here is my Rectangle.h这是我的 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我的矩形.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;
}
Here is the corrected version, with reasons and original code commented.这是更正后的版本,并注释了原因和原始代码。
Potential problem: area has not set by init, and can be set to a value that get_area() != get_width() * get_length()
潜在问题:area没有被init设置,可以设置为get_area() != get_width() * get_length()
Rectangle.h矩形.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:
// Remove "Rectangle::" from here
// This is not work for gcc and clang
// void Rectangle::init(double dblp_length, double dblp_width);
void init(double dblp_length, double dblp_width);
double dbl_length, dbl_width, dbl_area;
};
Rectangle.cpp:矩形.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" is not needed, it introduced a local variable instead of
// changing the instance variable.
// double dbl_length = dblp_length;
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" is not needed, it introduced a local variable instead of
// changing the instance variable.
// double dbl_width = dblp_width;
dbl_width = dblp_width;
}
double Rectangle::get_width() const {
return dbl_width;
}
bool Rectangle::is_square() const {
// missing the false part
// if (get_length() == get_width()) {
// return true;
// }
// return the boolean value directly instead
return get_length() == get_width();
}
void Rectangle::set_area(double dblp_length, double dblp_width) {
// this line is not needed, it introduced a local variable,
// making future assignment assigns to local instead of instance variable
// 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() << ", " <<
// missing () after get_area
// "Area: " << get_area << endl;
"Area: " << get_area() << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.