包含 windows.h 时出现数百个错误

Dee*_*ace 6 c++ winapi visual-studio-2012

我正在运行 Windows 7 64 位和 VS2012。当包含 windows.h 时,我会遇到大约 130 个错误。当然不能编译项目。

做了一个简短的谷歌研究,我看到有些人建议再次安装 SDK。我重新安装了但没有用。

然后我尝试用我朋友的电脑上的相同文件替换 windows.h 文件,但仍然出现错误。

然后我尝试替换整个包含文件夹:D,仍然出现相同的 130 个错误。

我还确保在项目设置中启用了“允许语言扩展”选项。

有任何想法吗?


所以,我做了更多的研究。如果我开始一个新项目并编译:

#include <windows.h>

void main()
{

}
Run Code Online (Sandbox Code Playgroud)

它编译得很好。然后我包含了我的 2 个 .h 文件,然后出现了错误(我会再次提到:虽然不包含 windows.h 我的项目编译完美无缺)。

我的两个 .h 文件包含 2 个类。

数字集.h:

#pragma once

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>

#define SIZE 5

using namespace std;

class NumSet
{
    private:
        int _arr[SIZE];
        int _numOfNumbers;

    public:
        NumSet(void);
        ~NumSet(void);

        int max();                      //returns a player's max number.

        bool insert(int newNum);
        int freeCells();
        bool replace(int index, int newNum);

        int min();

        float average();

        int biggerThan(int num);

        int smallerThan(int num);

        NumSet& operator+=(int num);

        NumSet& operator++();

        NumSet& operator--();

        bool operator==(const NumSet& src)const;

        NumSet operator=(const int * src);

        NumSet& operator=(const NumSet& src);

        bool del(int num);

        friend ostream& operator<<(ostream& out, NumSet& numset);

        friend istream& operator>>(istream& out, NumSet& numset);
};
Run Code Online (Sandbox Code Playgroud)

数字集.cpp:

#include "NumSet.h"

NumSet::NumSet(void)
{

    for (int i=0;i<SIZE;i++)            //generating 5 random numbers between 1-10 and storing them in the array.
    {

        _arr[i] = (rand()%10) +1;
    }

    std::sort(&_arr[0],&_arr[5]);     //sorting the numbers.

    _numOfNumbers = 5;
}

NumSet::~NumSet(void)
{
}

int NumSet::max()
{
    int max = 0;

    for (int i=0;i<SIZE;i++)
    {
        if (_arr[i]>max)
        {
            max = _arr[i];
        }
    }

    return max;
}

bool NumSet::insert(int newNum)
{
    if (freeCells()==0)
        return false;

    _arr[_numOfNumbers-1] = newNum;

    std::sort(&_arr[0],&_arr[5]);
}

int NumSet::freeCells()
{
    if (_numOfNumbers==SIZE)
        return 0;

    return (SIZE-_numOfNumbers);
}

bool NumSet::replace(int index, int newNum)
{
    if ((index<0 || index>SIZE) || (newNum<1 || newNum > 10))
        return false;

    _arr[index] = newNum;

    std::sort(&_arr[0],&_arr[5]);
}

int NumSet::min()
{
    int min = 11;

    for (int i=0;i<SIZE;i++)
    {
        if (_arr[i]<min)
        {
            min = _arr[i];
        }
    }

    return min;
}

float NumSet::average()
{
    int sum = 0;

    for (int i=0;i<SIZE;i++)
    {
        sum += _arr[i];
    }

    return ((float)sum/SIZE);
}

int NumSet::biggerThan(int num)
{
    int count = 0;

    for (int i=0;i<SIZE;i++)
    {
        if (_arr[i]>=num)
            count++;
    }

    return count;
}

int NumSet::smallerThan(int num)
{
    int count = 0;

    for (int i=0;i<SIZE;i++)
    {
        if (_arr[i]<num)
            count++;
    }

    return count;
}

NumSet& NumSet::operator+=(int num)
{
    this->insert(num);

    return *this;
}



NumSet& NumSet::operator++()
{
    for (int i=0;i<SIZE;i++)
    {
        _arr[i]++;

        if (_arr[i]==11)
        {
            _arr[i]= 1;
        }
    }

    return *this;
}

NumSet& NumSet::operator--()
{
    for (int i=0;i<SIZE;i++)
    {
        _arr[i]--;

        if (_arr[i]==0)
        {
            _arr[i]= 10;
        }
    }

    return *this;
}

bool NumSet::operator==(const NumSet& src)const
{
    if (_numOfNumbers != src._numOfNumbers)
        return false;

    for (int i =0;i<_numOfNumbers;i++)
    {
        if (_arr[i] == src._arr[i])
            continue;

        else
        {
            return false;
        }
    }

    return true;
}

NumSet NumSet::operator=(const int *src)
{
    if (sizeof(src)>(sizeof(int)*SIZE))
        return *this;

    NumSet newSet;

    for (int i=0;i<SIZE;i++)
    {
        newSet._arr[i]= src[i];
    }

    return newSet;
}

NumSet& NumSet::operator=(const NumSet& src)
{
    for (int i=0;i<SIZE;i++)
    {
        _arr[i]=src._arr[i];
    }

    _numOfNumbers=5;

    return *this;
}

bool NumSet::del(int num)
{
    if (num>SIZE)
        return false;

    _arr[num]=11;                           //setting the number to be deleted to 11 (there's no another way that 11 can be present in the array).

    std::sort(&_arr[0],&_arr[5]);           //sorting the array so the number to be deleted is in the end.

    _numOfNumbers--;                        //reducing the number of numbers in the array by 1, so the number to be deleted (the last in the array) will be ignored.
}

ostream& operator<<(ostream& out, NumSet& numset)
{
    if (numset._numOfNumbers==0)
        cout << "\n\nEmpty NumSet." << endl;

    else
    {
        for (int i=0;i<numset._numOfNumbers;i++)
        {
            cout << numset._arr[i] << " ";
        }
    }

    return out;
}

istream& operator>>(istream& in, NumSet& numset)
{

    for (int i=0;i<numset._numOfNumbers;i++)
    {
        cout << "\n\nEnter your #" << i+1 << " number:" << endl;
        int newnum;
        cin >> newnum;
        numset.replace(i, newnum);
    }

    return in;
}
Run Code Online (Sandbox Code Playgroud)

游戏.h:

#pragma once

#include "NumSet.h"


using namespace std;

class Game
{
    private:
        NumSet *player1, *player2;

        void humanVShuman();

        void humanVSpc();

        void pcVSpc();

    public:
        Game(void);
        ~Game(void);

        void game(int gameType);


};
Run Code Online (Sandbox Code Playgroud)

游戏.cpp:

#include "Game.h"


Game::Game(void)
{
    player1 = new NumSet;
    srand(time(0));
    player2 = new NumSet;
}


Game::~Game(void)
{
}

void Game::game(int gameType)
{
    if (gameType==1)
        humanVShuman();

    else if (gameType==2)
        humanVSpc();

    else if (gameType==3)
        pcVSpc();
}   

void Game::humanVShuman()
{
    system("cls");
    cout << *player1 << endl;
    //Sleep(200);
    cout << *player2 << endl;
    system("PAUSE");
}

void Game::humanVSpc()
{

}

void Game::pcVSpc()
{

}
Run Code Online (Sandbox Code Playgroud)

现在是有趣的部分:

在 main.cpp 中。只要我在做

#include "NumSet.h"
#include "Game.h"
#include <windows.h>
Run Code Online (Sandbox Code Playgroud)

我在不同的 h 文件(例如 wingdi.h 和 winuser.h)中遇到很多错误。

如果我不包括我的两个 h 文件 NumSet 和 Game,而只包括 windows.h,它编译时不会出错。

如果我只包含我的 2 h 文件,它会编译出错。

所以我的 2 h 文件中的某些内容会被 windows.h 中断。但是什么?

gon*_*aao 6

SIZE中已经定义windef.h。你不想重新定义它。将其更改为MY_SIZE或在你别的东西NumSet.h。然后它在我的机器上编译(vs2012 在 Win7 上)。

而且,你有一些功能的名称,如maxmin你的NumSet。最好避免这些。我们已经在标准头文件中定义了这种宏。尝试一些其他名称。否则会给你带来一些痛苦。

  • `min` 和 `max` 在 `windows.h` 中被定义为宏这一事实很糟糕,并且在尝试使用第 3 方代码时会导致很多问题。例如,请参阅 http://qt-project.org/forums/viewthread/22133。幸运的是,您可以使用 `NOMINMAX` 来防止 `windows.h` 定义它们。 (2认同)