Windows.h 导致 C++ 类出现问题

Des*_*cie 2 c++ windows c++11 visual-studio-2019

我使用的是 Visual Studio 2019,当我用 C++ 编写第一堂课时,发生了错误。当我删除它时它消失了#include <Windows.h>。我的问题是,为什么Windows.h会与 C++ 类发生冲突,是否可以同时使用两者(我几乎可以肯定)。

#include <iostream>
#include <locale>
using std::cout;
using std::cin;

class Rectangle {
public:
    Rectangle() = default;

    Rectangle(double width, double height)
        : width_{ width }, height_{ height }
    {}
    double Width() const { return width_; }
    double Height() const { return height_; }

    double Area() const {
        return width_ * height_;
    }
    double Perimeter() const {
        return 2 * (width_ + height_);
    }
    void Scale(double scaleFactor) {
        width_ *= scaleFactor;
        height_ *= scaleFactor;
    }
private:
    double width_{};
    double height_{};
};

void printInfo(const Rectangle & r) {
    cout << "Width" <<r.Width() << '\n';
    cout << "Height" << r.Height() <<   '\n';
    cout << "Area" << r.Area() << '\n';
    cout << "Per" << r.Perimeter() << '\n';
}

int main() {
    setlocale(LC_ALL, "pl_PL.UTF8");
    Rectangle rect;
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*ers 9

Windows.h 定义Rectangle为自由函数,请参见此处

解决方案:更改您的类的名称或将其放在自己的命名空间中。