我試圖編寫類似於康威生命遊戲的代碼。一切似乎都很好,但我有一個致命的錯誤#include「colour.h」。代碼是在C + +中,我知道這個API屬於C.我不知道,如果它會有所作爲,或者我必須使用另一個API的C + +。原因是在我的臺式電腦上,我沒有改變任何東西,程序運行完美。但是當我在其他計算機上運行該程序時,它會在代碼的第一行(#include「colour.h」)中出現致命錯誤。我不知道它爲什麼會給出錯誤。#include「colour.h」C++中的致命錯誤
見http://en.wikipedia.org/wiki/Conway's_Game_of_Life
#include "colours.h"
#include <sstream> //
#include <iomanip> // setw()
#include <windows.h> // setConsoleTitle(), Sleep()
#include <cstdlib> // rand(), seed()
#include <iostream> // cout
using namespace std;
// OUR DATA and CONSTANTS
const char LIFE = 'L';
const char BLANK = '.';
const int N = 20;
const int M = 20;
const int NGRID = N*M;
char world[NGRID]; // will simulate 2-dimenisonal array.
int loc(int i, int j)
{
return i*M+j*sizeof(char);
}
void gotoxy(int x, int y, char c)
{
world[ loc(x,y)] = c;
}
// OUR FUNCTIONS (ie, TOOLS) that work on our DATA
void fillWorldwith(char w[], const int N, const int M, const char stuff){
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
w[loc(i,j)]=stuff;
}
void displayWorld(char *w, const int N, const int M){
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
cout << setw(2) << w[loc(i,j)];
cout << endl;
}
}
// Used in applyRules as a buffer to update world.
void copyWorld(char dest[], char src[], const int N, const int M){
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
dest[loc(i,j)]= src[loc(i,j)];
}
int main()
{
SMALL_RECT windowSize; // windows
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // windows
windowSize = {0, 0, 200, 200};
SetConsoleWindowInfo(hConsole, TRUE, &windowSize); // windows
SetConsoleTitle("Game of Life"); // windows
system("cls"); // clear the console
stringstream ss; // trick to combine string and numbers
ss << "color " << Black << Yellow;
// objects are fun to use.
system(ss.str().c_str()); // change console colour
fillWorldwith(world, N, M, BLANK); // fill whole world with BLANKS
displayWorld(world, N, M);
int x=0, y =0;
for(int time=0; time<100; time++){ // each loop, considered time.
system("cls");
displayWorld(world, N, M); // display world
gotoxy(x, y, BLANK);
x= (x+1)%N;
y= (y+1)%M;
gotoxy(x, y, LIFE);
Sleep(50);
}
return 0;
}
什麼是錯誤信息? – Beta
顯示「colours.h」文件。這可能是有用的 –
正如我寫的第一行代碼給出了一個致命的錯誤。它另外說沒有這樣的文件或目錄。 –