我正在运行 Windows 7 64 位和 VS2012。包含 windows.h 时,我遇到大约 130 个错误。当然不能编译项目。
做了一个简短的谷歌研究,我看到一些人建议再次安装 SDK。我重新安装但无济于事。
然后我尝试用我朋友的电脑上的相同文件替换 windows.h 文件,但我仍然得到错误。
然后我尝试替换整个包含文件夹:D,仍然得到相同的 130 错误。
我还确保在项目设置中启用了“允许语言扩展”选项。
有任何想法吗?
所以,我做了更多的研究。如果我开始一个新项目并编译:
#include <windows.h>
void main()
{
}
它编译得很好。然后我包含了我的 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);
};
数字集.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;
}
游戏.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);
};
游戏.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>
我在诸如wingdi.h 和winuser.h 等不同的h 文件中遇到了很多错误。
如果我不包括我的两个 h 文件 NumSet 和 Game,而只包括 windows.h,它编译时没有错误。
如果我只包含我的 2 h 文件,它会编译错误。
所以我的 2 h 文件中的某些内容会被 windows.h 中断。但是什么?