如何解决代码中的“未声明的标识符”错误?

时间:2019-05-22 20:28:46

标签: c++

我正在尝试学习C ++,并且在Visual Studio 2019中运行了以下代码。 我遇到12个错误,但是代码在网站上运行正常。

您可以在此处找到代码: https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm

Alert alert = new Alert(AlertType.CONFIRMATION);
            alert.setTitle("Confirmation Dialog with Custom Actions");
            alert.setHeaderText(null);
            alert.setContentText("Choose your option.");
            ButtonType buttonTypeOne = new ButtonType("One");
            ButtonType buttonTypeTwo = new ButtonType("Two");
            ButtonType buttonTypeThree = new ButtonType("Three");
            ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);

            alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);
            alert.getDialogPane().lookupButton(buttonTypeCancel).setVisible(false);

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == buttonTypeOne){
                // ... user chose "One"
            } else if (result.get() == buttonTypeTwo) {
                // ... user chose "Two"
            } else if (result.get() == buttonTypeThree) {
                // ... user chose "Three"
            } else {
                // ... user chose CANCEL or closed the dialog
            }

enter image description here

1 个答案:

答案 0 :(得分:1)

只需修改Box类的构造函数。您只是错过了,参数和名称之间的b

Box(double l = 2.0, double b = 2.0, double h = 2.0) {

请查看完整的代码,如下所示:

#include <iostream>

using namespace std;

class Box {
public:
    static int objectCount;

    //Constructor definition
    Box(double l = 2.0, double b = 2.0, double h = 2.0) {
        cout << "Constructor Called." << endl;
        length = l;
        breadth = b;
        height = h;

        //Increase everytime the object is created
        objectCount++;
    }
    double Volume() {
        return length * breadth* height;
    }
private:
    double length;
    double breadth;
    double height;
};
// Initialize static member of class Box
int Box::objectCount = 0;

int main(void) {
    Box Box1(3.3, 1.2, 1.5);
    Box Box2(8.5, 6.0, 2.0);

    cout << "Total objects: " << Box::objectCount << endl;

    return 0;
}

请获得以下结果:

Constructor Called.
Constructor Called.
Total objects: 2