小编Spr*_*fox的帖子

每个C++,从矢量元素拉

我试图在一个攻击向量上做一个foreach,每个攻击有一个唯一的ID,1-3.

类方法采用1-3的键盘输入.

我试图使用foreach在m_attack中运行我的元素以查看数字是否匹配,如果它确实...做某事.

我看到的问题是:

a'for each' statement cannot operate on an expression of type "std::vector<Attack
Run Code Online (Sandbox Code Playgroud)

我是否完全错了,我有C#的经验,并且是我的基础,任何帮助都将受到赞赏.

我的代码如下:

在标题中

vector<Attack> m_attack;
Run Code Online (Sandbox Code Playgroud)

在班上

int Player::useAttack (int input)
{

    for each (Attack* attack in m_attack) // Problem part
    {  
        //Psuedo for following action
        if (attack->m_num == input)
        {
            //For the found attack, do it's damage
            attack->makeDamage();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ each for-loop vector

55
推荐指数
4
解决办法
22万
查看次数

C++ fstream覆盖而不是追加

我正在尝试为我正在进行的项目创建一个基本的高分系统.

我遇到的问题是,虽然我将名字写入我的主要内容,但它们只是覆盖了之前的名称.

目前我有这个:

void ManagePoint::saveScore(string Name, int Score)
{

    ofstream newFile("scorefile.txt");

    if(newFile.is_open())   
    {
        newFile << Name << " " << Score;            
    }
    else 
    {
        //You're in trouble now Mr!
    }


    newFile.close();

}
Run Code Online (Sandbox Code Playgroud)

为了测试我正在添加它们:

runner->saveScore("Robert", 34322);

runner->saveScore("Paul", 526);

runner->saveScore("Maxim", 34322);
Run Code Online (Sandbox Code Playgroud)

在负载显示上,所有出现的是Maxim的分数,我如何循环并保存所有内容,或者附加全部或者其他内容?

c++ file overwrite

25
推荐指数
1
解决办法
6万
查看次数

C++从类方法返回一个对象数组

我试图返回一个二维的'房间'对象数组作为一种打印/显示数组中的内容.

Room* m_map[10][10];
Run Code Online (Sandbox Code Playgroud)

如此生成:

//Initialise array to NULL
for(int x=0;x<10;x++)
{
    for(int y=0;y<10;y++)
        m_map[x][y] = NULL;
}

for(int n=0; n<10; n++)
{       
    for(int ran=0; ran<3; ran++)
    {
        int r_n = rand() % 10 ;

        Room* r = new Room(n, "Normal", true, false, false, true);
        m_map[r_n][n] = r;
    }       

}
Run Code Online (Sandbox Code Playgroud)

所以这给出了阵列中Rooms的分散.

然后,我想为这些房间所在的用户显示/打印,参考NULL.

所以我可以举例来说,如果NULL显示'#',如果它是一个房间留下''.

我不确定我应该在方法中返回的位.

任何帮助或指向正确的方向将真的很感激

c++ arrays oop

1
推荐指数
1
解决办法
1816
查看次数

标签 统计

c++ ×3

arrays ×1

each ×1

file ×1

for-loop ×1

oop ×1

overwrite ×1

vector ×1