【问题标题】:Visual Studio 2015 Error C3867 'Rectangle::get_area': non-standard syntax; use '&' to create a pointer to memberVisual Studio 2015 错误 C3867 'Rectangle::get_area':非标准语法;使用 '&' 创建指向成员的指针
【发布时间】: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_areaset_widthset_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


【解决方案1】:

这里是更正的版本,有原因和原始代码注释。

潜在问题: area没有被init设置,可以设置为get_area() != get_width() * get_length()的值

矩形.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;
};

矩形.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;
}

【讨论】:

  • 非常感谢user1997915的简洁更正!我明白你的意思,这将有很大帮助。
猜你喜欢
  • 2015-11-10
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
相关资源
最近更新 更多