Visual Studio 2015错误C3867'Rectangle :: get_area':非标准语法;使用'&'创建指向成员

时间:2016-05-04 02:07:32

标签: c++ function class c++11 visual-studio-2015

我很难理解为什么我在使用用户定义的类“Rectangle”的简单程序的上下文中出现此错误

我制作的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;
};

我的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;
}

1 个答案:

答案 0 :(得分:0)

以下是更正后的版本,其中包含原因和原始代码。

潜在问题:    区域未由init设置,可以设置为get_area() != get_width() * get_length()

的值

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:
    // 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:

#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;
}