简体   繁体   English

错误预期为声明C ++

[英]error expected a declaration c ++

UPDATE 2: well I fixed it, turns out it was a simple matter of a semi column getting in where it didn't belong, was looking in completely the wrong place. 更新2:好吧,我修复了它,原来这是半列插入不属于它的地方的简单问题,它在完全错误的位置查找。

UPDATE:removing some comments fixed the cin cout errors, now the only errors are expected a declaration on the opening and closing brackets and one has appeared on the else statement. 更新:删除一些注释可修复cin cout错误,现在唯一的错误应该在左括号和右括号中声明,并且else语句中已经出现了。

the Error "expected a declaration" appears in the function playeroneturn on the opening and closing {} aswell as the closing } of the if statement in the function, also in the if statement the cin and cout both give the error "this declaraton has no storage class or type specifier" 错误“预期的声明”出现在函数playeroneturn中,函数的if语句的开始和结束{}以及结束},并且在if语句中cin和cout都给出错误“此声明没有存储类或类型说明符”

#include "stdafx.h"
#include "iostream"
#include "ctime"
#include "cstdlib"
#include "string"

//prototype function declaration from stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c
int help(int menu);
int start(int menu);
int oneplaymode();
int playeroneturn();
int playertwoturn();

//function start is called to display the games menu screen
int start(int menu)
{
    using std::cout;
    using std::cin;
    using std::endl;
    cout << "#########################################################################" << endl;
    cout << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t    PIGS\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t1. 1 Player\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t2. 2 Player\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t3. help\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl;
    cout << "#########################################################################" << endl;
    cout << "enter number for your selection: ";
    cin >> menu;
    if(menu == 1)
    {
        cout << "single-player not yet implemented" << endl;

    }
    else if(menu == 2)
    {
    int twoplayermode();
    }
    else if(menu == 3)
    {
    help(menu);
    }
    else
    {
    cout << "Error: Please choose a valid option" << endl;

    start(menu); 

    }
return(menu);
}

int help(int menu)
{
    using std::cout;
    using std::endl;    
    cout << "#########################################################################" << endl;
    cout << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t HELP\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#  The objective of pigs is to be the first to score 100.\t\t#" << endl << "#  Each turn you must roll a die then pass or roll again.\t\t#" << endl << "#  You must then choose to ROLL again or END your turn.\t\t\t#" << endl << "#  At the end of your turn your total is added to your score.\t\t#" << endl << "#  However if you roll a 1 your turn ends and you score 0.\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl;
    cout << "#########################################################################" << endl;
    system("pause");
    start(menu);
    return 0;
}

int playeroneturn(int p1score);
{
    using namespace std;
    using std::cout;
    using std::cin;
    using std::endl;
    using std::srand;
    using std::rand;
    using std::time;
    using std::string;
    srand((unsigned int)time(0)); 
    int roll = 0;
    int p1score = 0;
    string reroll = "roll";
    while(reroll == "roll") 
    {
        roll = 1 + (rand() % 6);
            if(roll > 1)
            {
                p1score = p1score+roll;
                // using " in a string msdn.microsoft.com/en-us/library/267k4fw5.aspx
                cout << "You rolled a " << roll << endl << "Type roll to roll again or end to end your turn."; // error on cout this declaraton has no storage class or type specifier and error on first << expected a ;
                cin >>  reroll; 
            }
            else 
            { 
                cout >> "Bad luck! you rolled a 1, your turn is over and you score nothing!"
                p1score = 0; 
                reroll = end;
            }
    }
    return p1score; 
}

I see several issues (from my comment): 我看到了几个问题(根据我的评论):

  • you cannot have statements like 1 + (rand() % 6); = roll 您不能有类似1 + (rand() % 6); = roll语句1 + (rand() % 6); = roll 1 + (rand() % 6); = roll . 1 + (rand() % 6); = roll What is this supposed to do? 这应该怎么办? If you want roll to be 1 + (rand() % 6); 如果您希望roll1 + (rand() % 6); , write it roll = 1 + (rand() % 6); ,将其写为roll = 1 + (rand() % 6);
  • you cannot use continue as variable, it's reserved keyword 您不能将continue用作变量,它是保留关键字
  • you cannot mix >> and << when using cout . 使用cout时不能混合>><< Use only << . 仅使用<<
  • p1score is not initialized (it's not compiler's error, but still - you use it (in the body of the if-statement , where the first condition ( if(roll > 1) ) is true) without being initialized, so you'll get garbage value p1score没有初始化(这不是编译器的错误,但是仍然)-您无需初始化就可以使用它(在if-statement的主体中,其中第一个条件( if(roll > 1) )为true),因此您会得到垃圾价值
  • have you included #include <string> ? 您是否包含#include <string>
  • you don't have using std::string and if you don't have using namespace std; 您没有using std::string ,如果您没有using namespace std; , you'll have a compiler error for this, too. ,您也会为此遇到编译器错误。

  • waiting for an answer - what is before the first { . 等待答案-第一个{之前是什么。


EDIT: Looking at you edit, you need to do some other changes: 编辑:看着您进行编辑,您需要做一些其他更改:

  • first, the definition of playeroneturn is wrong: int playeroneturn(int p1score); { ... } 首先, playeroneturn的定义是错误的: int playeroneturn(int p1score); { ... } int playeroneturn(int p1score); { ... } <- remove ; int playeroneturn(int p1score); { ... } <-删除; before the function's body 在功能主体之前
  • note, that in playeroneturn , int p1score = 0; 注意,在playeroneturnint p1score = 0; shadows the parameter of the function, it has the same name 遮蔽函数的参数,它具有相同的名称
  • again in playeroneturn - cout must be used with << , not with >> 再次playeroneturn - cout 必须与使用<< ,不与>>
  • a ';' 一种 ';' must be added after the wrong cout (from the previous point) 必须在错误之后增加cout (从上一个点)
  • in playeroneturn (again), reroll = end; playeroneturn (再次)中, reroll = end; is bad - what is this end , it's not defined nowhere. 不好-这是什么end ,它无处不在。

continue是一个关键字,不能用作变量名。

while(continue == "roll")
{
    1 + (rand() % 6); = roll //<--- what is this? no ; and no l value
        if(roll > 1)

where is semicolon and lvalue? 分号和左值在哪里?

continue is a keyword / reserved word . 继续是关键字/保留词 and hence it should not be used as a variable name or other literal specification.For feeling the difference just rename the variable 'continue' to 'continue1' and see the error go. 因此,不应将其用作变量名或其他文字规范。要感觉到差异,只需将变量“ continue”重命名为“ continue1”,然后查看错误信息。 Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM