2016-05-04 29 views
-1

我难以理解,为什么我在使用用户定义的类“矩形”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; 
} 
+0

修正了错误,在我的帖子中,复制粘贴了我的代码的错误版本。 –

+0

在'get_area'函数调用后错过了'()'。看看它上面的行,'get_width()'被正确调用。 –

+0

在'set_area','set_width'和'set_length'中,您创建了与该类的成员变量具有相同名称的全新变量。为什么?而不是'double dbl_width = dblp_width;'do'dbl_width = dblp_width;'。 – DeiDei

回答

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; 
} 
+0

非常感谢您的简要更正user1997915!我明白你的意思,这将有很大帮助。 –

相关问题