I'm running windows 7 64-bit and VS2012. When including windows.h I'm getting around 130 errors or so. and of course can't compile the project.
Did a short google research and I saw some people recommending installing the SDK again. I reinstalled but to no avail.
Then I tried to replace the windows.h file with the same file from my friend's pc where it works, but still I get the errors.
Then I tried to replace the whole include folder :D, still get the same 130 errors.
I also made sure that the "allow language extension" option is enabled in the project settings.
Any ideas?
So, I've done some more researching. If I start a new project and compile:
#include <windows.h>
void main()
{
}
It compiles well. Then I included my 2 .h files and then the errors appear (I'll mention again: while not including windows.h my project compiles flawlessly).
My two .h files contain 2 classes.
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()
{
}
And now for the interesting part:
In main.cpp. As long as I'm doing
#include "NumSet.h"
#include "Game.h"
#include <windows.h>
I'm getting a lot of errors in different h files such as wingdi.h and winuser.h.
If I'm not including my two h files NumSet and Game, and just including windows.h, it compiles with no errors.
If I'm including only my 2 h files, it compiles with errors.
So something in my 2 h files interrupts with windows.h. But what?
windows. h is an operating-system specific header. If you're compiling for Windows you need it, and every compiler that supports Windows will be okay with it.
h'". That problem happens because the file, which is needed to compile programs that make calls to the Windows operating system, is not installed. To fix this, download and install the Microsoft Windows SDK for your system (it is free). Once the SDK is installed, add the file paths to Visual Studio.
SIZE
is already defined in windef.h
. You don't want to redefine it. Change it to MY_SIZE
or something else in your NumSet.h
. Then It compiles on my machine (vs2012 on Win7).
And also, you have some function names like max
and min
in your NumSet
. Better avoid those. We already have that kind of macros defined in standard headers. Try some other names. Or it will give you some pains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With