c ++源文件中未声明的标识符?

时间:2014-06-10 00:29:42

标签: c++ eclipse

我似乎无法找到问题的答案,尽管我认为它的内容相当简单。我在Mac OSX和c ++上使用eclipse 3。我是否必须在我的来源中声明某些内容?页眉?有人请你快点帮我。

错误讯息: “使用未声明的标识符'place'source.cpp / Summative line 88 C / C ++ Problem”

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <fstream>
#include "header.h"

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ofstream;


void summ :: welcomeMessage()
{
    cout << "Wanna play some connect four? Lets Play!" << endl << endl;
}

void summ :: display()
{

    cout << " 1   2   3   4   5   6   7\n";
for(int a = 0; a <= 5; a++)
{
    for(int b = 0; b <= 6; b++)
        cout << char(218) << char(196) << char(191) << " ";
        cout << endl;

    for(int b = 0; b <= 6; b++)
        cout << char(179) << place[a][b] << char(179) << " ";
        cout << endl;

    for (int b = 0; b <= 6; b++)
        cout << char(192) << char(196) << char(217) << " ";
        cout << endl;
}
}


int summ :: drop(int b, char player)        //This function puts the piece     above the first full square or failing that at the bottom.
 {
if(b >= 0 && b <= 6)            //Checks if b given is a vaild number between 0 and 6.
{                               //For the 1 to 7 columns, 0 to 6 because arrays start at 0 not 1.
    if (place[0][b] == ' ')
         {
        int i;
        for (i = 0; place[i][b] == ' '; i++)
            if(i == 5)
            {
                place[i][b] = player;
                return i;
            }
            i--;
            place[i][b] = player;
            return i;
    }
    else
    {
        return -1;
    }
}

else
{
    return -1;
}
 }

 bool summ :: check (int a, int b)
 {
int vert = 1;
int hor = 1;
int diag1 = 1;
int diag2 = 1;
char player = place[a][b];
int i;
int ii;

for(i = a + 1; place[i][b] == player && i <= 5; i++, vert++);
for(i = a - 1; place[i][b] == player && i >= 0; i--, vert++);
if (vert >= 4)
    return true;

for(ii = b - 1; place[a][ii] == player && ii >= 0; ii--, hor++);
for(ii = b + 1; place[a][ii] == player && ii <= 6; ii++, hor++);
if (hor >= 4)
    return true;

for (i = a - 1, ii = b - 1; place[i][ii] == player && i >= 0 && ii >= 0; diag1++, i--, ii--);
for (i = a + 1, ii = b + 1; place[i][ii] == player && i <= 5 && ii <= 6; diag1++, i++, ii++);
if (diag1 >= 4)
    return true;

for (i = a - 1, ii = b + 1; place[i][ii] == player && i >= 0 && ii <= 6; diag2++, i--, ii++);
for (i = a + 1, ii = b - 1; place[i][ii] == player && i <= 5 && ii >= 0; diag2++, i++, ii--);
if (diag2 >= 4)
    return true;
return false;
}

 int summ :: PVP()
 {
string ax;
string bx;

cout << "Player 1. What's Your Name? ";
cin >> ax;
cout << "Player 1. Hello, " << ax << endl << endl;

cout << "Player 2. What's Your Name? ";
cin >> bx;
cout << "Player 2. Hello, " << bx << endl << endl;


for (int a = 0; a <= 5; a++)
{
    for (int b = 0; b <= 6; b++)
        place[a][b] = ' ';
}

display();
int hold;
int hold2 = 0;
int charsPlaced = 0;
bool gamewon = false;
char player = 15;
string r;

while (!gamewon)
{
    if (hold2 != -1)
    {
        if (player == 15)
        {
            cout << ax << " what column would you like to drop in?";
            player = 178;
        }

        else
        {
            cout << bx << " what column would you like to drop in?";
            player = 176;
        }
    }

    while (true)
    {
        if (charsPlaced == 42)
            break;
        cin >> hold;
        hold--;

        if (hold <= 6 && hold >= 0)
            break;
        else cout << "\nEnter a value between 1 and 7: ";

        if (cin.fail())
        {
            cin.clear();
            char c;
            cin >> c;
        }
    }

        if (charsPlaced == 42)
            break;
        hold2 = drop(hold, player);

        if (hold2 == -1)
            cout << "Column is full\nTry again: ";

        else
        {
            gamewon = check(hold2, hold);
            charsPlaced++;
            system("cls");
            display();
        }
}

    system("cls");
    if (charsPlaced == 42)
    {
        cout << "Draw!\n";
        system ("pause");
        return 0;
    }

    if (player == 15)
    {
        cout << bx << "has won!" << endl;
        cout << "Play another game? (Y/N): ";
        cin >> r;
        if (r = "Y", "Yes", "yes", "y")
        {
            !gamewon;
        }
    }
    else
    {
        cout << ax << "has won!" << endl;
        cout << "Play another game? (Y/N): ";
        cin >> r;
        if (r = "Y", "Yes", "yes", "y")
        {
            gamewon = false;
        }
    }

    system ("pause");
    return 0;
}

 int summ :: AI()
 {
string ax;
cout << "What's Your Name? ";
cin >> ax;

for (int a = 0; a <= 5; a++)
{
    for (int b = 0; b <= 6; b++)
        place[a][b] = ' ';
}

display();
int hold;
int hold2 = 0;
int charsPlaced = 0;
bool gamewon = false;
char player = 15;
string r;

while (!gamewon)
{
    if (hold2 != -1)
    {
        if (player == 15)
        {
            cout << ax << " what column woul you like to drop in?";
            player = 254;
        }

        else
        {
            player = 15;
        }
    }

    while (true)
    {
        if (charsPlaced == 42)
            break;
        if (player == 15)
        {
            cin >> hold;
            hold--;
        }

        else
        {
            hold = (1 + (rand() % 7));
            srand(time(NULL));
            hold--;
        }

        if (hold <= 6 && hold >= 0)
        break;
        else cout << "\nEnter a value between 1 and 7: ";

        if (cin.fail())
        {
            cin.clear();
            char c;
            cin >> c;
        }
    }

        if (charsPlaced == 42)
            break;
        hold2 = drop(hold, player);

        if (hold2 == -1)
            cout << "Column is full\nEnter another number between 1 and 7: ";

        else
        {
            gamewon = check(hold2, hold);
            charsPlaced++;
            system("CLS");
            display();
        }
}

    system("cls");
    if (charsPlaced == 42)
    {
        cout << "Draw!\n";
        system ("pause");
        return 0;
    }

    if (player == 15)
    {
        cout << ax << " has won!" << endl;
    }
    else
    {
        cout << "AI" << " has won!" << endl;
    }

    system ("pause");
    return 0;
 }

 void summ :: selection()
 {
int x = 0;
cout << "1. Player vs. Player" << endl << "2. Player vs. AI" << endl << endl;
cout << "Enter your selection: ";
cin >> x;

switch(x)
{
case 1: cout << endl;
        PVP();
        break;

case 2: cout << endl;
        AI();
        break;

default: cout << "Try again! Please enter 1 or 2.";
        getchar();
}

 }

2 个答案:

答案 0 :(得分:0)

char place[6][7] = 
{
    { ' ', ' ', ' ', ' ', ' '}, 
    { ' ', ' ', ' ', ' ', ' '}, 
    { ' ', ' ', ' ', ' ', ' '}, 
    { ' ', ' ', ' ', ' ', ' '}, 
    { ' ', ' ', ' ', ' ', ' '}, 
    { ' ', ' ', ' ', ' ', ' '}
};

看看是否将其置于全球范围内将解决问题。

它实际上只是2D阵列中的空间,可以让你在这个模型上玩一个板:

enter image description here

但是,严肃地说,这段代码没有place工作。除非你不小心删除它,否则它存在于某个地方。尝试搜索其他文件place,看看您是否找不到place最初定义的位置。

答案 1 :(得分:-1)

字面意思是编译器告诉你的。你使用&#39; place&#39;在该文件中的任何地方,就好像它是一个全局变量,但你不能在任何地方声明/初始化它。