我的程序有啥问题?我希望保存坐标值,但每次循环后它都会重置为 0,0

Posted

技术标签:

【中文标题】我的程序有啥问题?我希望保存坐标值,但每次循环后它都会重置为 0,0【英文标题】:What's wrong with my program? I want the coordinate values to be saved but it keeps resetting to 0,0 after every loop我的程序有什么问题?我希望保存坐标值,但每次循环后它都会重置为 0,0 【发布时间】:2015-01-02 04:41:45 【问题描述】:

我的程序有什么问题?我希望保存坐标值,但每次循环后它都会重置为 0,0。如果您在同一文件夹中运行带有 .h 的主文件,您会看到循环完成后坐标值会重置。非常令人沮丧。

我的 .h 文件 不保存的部分很可能在 move() 函数中 非常感谢 :) 我什么都试过了,是不是我做错了什么?

using namespace std;
    #include <string>
    #define FORWARD 0
    #define BACKWARD -1
    #define RIGHT 2
    #define LEFT -2
    #define t true
    class Player
    
        public:
        //x pos
        float x;
        //y pos
        float y;
        //id of player
        string id;
    ;
    string d;
    string mv;
    // Making a new player
    Player p;

    int direction;


    void turnForward()
    
        direction = FORWARD;
    

    void turnBackward()
    
       direction = BACKWARD;
    

    void turnRight()
    
        direction = RIGHT;
    

    void turnLeft()
    
        direction = LEFT;
    

    int spaces;

    void move(int spaces, int direction, float x, float y, Player p)
    
        x = p.x;
        y = p.y;
        if(direction == LEFT)
        
            x = x - spaces;

            cout << x << ","<<   y << endl;
        
          p.x = x;

        if(direction == RIGHT)
        
            x = x + spaces;

            cout << x << ","<<   y << endl;
        
        p.x = x;

        if(direction == FORWARD)
        
            y = y + spaces;

            cout << x << ","<<   y << endl;
        
        p.y = y;
        if(direction == BACKWARD)
        
            y = y - spaces;

            cout << x << ","<<    y << endl;
        
         p.y = y;

    

我的主文件

#include <iostream>
#import <string>
#include "assets.h"
using namespace std;

void run()


        while(t)
    
    cout << "What direction would you like to turn? Left (L), Right (R), Forward (F), Backward (B) ";
        cin >> d;
        if(d == "L" || d == "l")
        
            turnLeft();
            cout << "Would you like to move " << d << "? Y/N ";
            cin >> mv;
            if(mv == "Y" || mv == "y")
            
                cout << "How many spaces would you like to go " <<d << "? ";
                cin >> spaces;
                move(spaces, direction, p.x, p.y, p);
            
            else
            
                run();
            
        
        if(d == "R" || d == "r")
        
            turnRight();
            cout << "Would you like to move " << d << "? Y/N ";
            cin >> mv;
            if(mv == "Y" || mv == "y")
            
                cout << "How many spaces would you like to go " <<d << "? ";
                cin >> spaces;
                move(spaces, direction, p.x, p.y, p);
            
            else
            
                run();
            
        

        if(d == "F" || d == "f")
        
            turnForward();
            cout << "Would you like to move " << d << "? Y/N ";
            cin >> mv;
            if(mv == "Y" || mv == "y")
            
                cout << "How many spaces would you like to go " <<d << "? ";
                cin >> spaces;
                move(spaces, direction, p.x, p.y, p);
            
            else
            
                run();
            
        

        if(d == "B" || d == "b")
        
                turnBackward();
                cout << "Would you like to move " << d << "? Y/N ";
                cin >> mv;
                if(mv == "Y" || mv == "y")
                
                    cout << "How many spaces would you like to go " <<d << "? ";
                    cin >> spaces;
                    move(spaces, direction, p.x, p.y, p);
                
                else
                
                    run();  
                
            

    


int main(int argc, char *argv[]) 
   
        cout << "Enter in your name (This will be your username): ";
        cin >> p.id;

        run();


主文件调用run()

【问题讨论】:

为什么.h 文件中有函数定义?函数应该在.cpp 中。头文件应该只包含宏和声明。 【参考方案1】:

您通过值传递p,而不是通过引用。所以move() 正在修改播放器的副本。将move() 更改为:

void move(int spaces, int direction, float x, float y, Player &p)

【讨论】:

move() 的前两行会立即丢弃类中传递的值,因此也需要解决。 @Olipro 他总是称它为move(spaces, direction, p.x, p.y, p);。然后move() 执行x = p.x; y=p.y。所以他将参数设置为之前设置的值。他显然不需要一开始就传递那些参数。【参考方案2】:

在 move() 方法中,您将 x 和 y 的参数值重置为 p.x 和 p.y。你在哪里初始化了 p 的值? 每次执行循环时,它都会传递对象 Person 的副本,因此如果您在 move() 方法中更改该对象的任何值,它将不会在 run() 方法中生效。正如 Barmar 提到的,您必须通过引用传递值或从 move() 方法返回更新的 Person 对象,然后在 run() 方法中将其分配回对象 p。

【讨论】:

以上是关于我的程序有啥问题?我希望保存坐标值,但每次循环后它都会重置为 0,0的主要内容,如果未能解决你的问题,请参考以下文章

易语言, 取两点水平坐标值的问题!

html5视频元素有啥bug吗?几个小时后它不会继续播放内容

使用位置坐标值在谷歌地图中绘制路线图

在Django中覆盖保存方法时的无限循环

r中怎么删除图形坐标值

delphi中如何获得当前屏幕photoshop选择区域右上角的坐标值