包括windows.h在内的一百个错误

时间:2013-04-23 23:59:39

标签: c++ winapi visual-studio-2012

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

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

然后我尝试用我朋友的PC上的相同文件替换windows.h文件,但是我仍然得到错误。

然后我尝试替换整个include文件夹:D,仍然得到相同的130错误。

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

有什么想法吗?


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

#include <windows.h>

void main()
{

}

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

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

NumSet.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);
};

NumSet.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;
}

game.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);


};

game.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()
{

}

现在是有趣的部分:

在main.cpp中。只要我做

#include "NumSet.h"
#include "Game.h"
#include <windows.h>

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

如果我不包括我的两个文件NumSet和Game,并且只包含windows.h,它会编译没有错误。

如果我只包含我的2小时文件,则会编译错误。

因此,我的2小时文件中的某些内容会中断windows.h。但是什么?

2 个答案:

答案 0 :(得分:4)

SIZE已在windef.h中定义。你不想重新定义它。将其更改为MY_SIZENumSet.h中的其他内容。然后它在我的机器上编译(Win7上的vs2012)。

此外,您的max中还有一些功能名称,例如minNumSet。最好避免那些。我们已经在标准头文件中定义了这种宏。尝试其他一些名字。或者它会给你一些痛苦。

答案 1 :(得分:2)

首先,我建议您将windows.h作为首要内容。这样,您获得的任何错误都会被视为您的代码中的错误,而不是windows.h中的错误,这样您就可以修复错误。

其次,在添加#define NOMINNMAX之前,请尝试使用windows.h。这样可以防止windows.h生成宏minmax(其名称坦率地说非常糟糕,MS应该选择这些名称感觉不好)。这样,您仍然可以使用您的函数minmax(或者,正如gongzhitaao建议的那样,无论如何都可以重命名它们。)

(公主大使建议使用SIZE以外的东西可能也是正确的。)

编辑:有关NOMINMAX的更多信息:http://support.microsoft.com/kb/143208

相关问题