【问题标题】:How to use reference const object passed to a function as a parameter如何使用作为参数传递给函数的引用常量对象
【发布时间】:2017-07-19 02:20:30
【问题描述】:

Pos 是一个带有 x 和 y 的结构,我想访问 currentPos 的这些组件。但我得到了错误。我无法直接访问 currentPos.x 和 .y,因为编译器无法处理它,因为分配尚未完成。所以我选择了一个全局变量。但这也不起作用。 错误在代码段的底部

 bwi_gridworld::Pos current_pose; //global variable

 void setCurrentPose(const bwi_gridworld::Pos& currentPos) {
        current_pose = *currentPos;
}



  char myAgent::nextAction(const bwi_gridworld::Pos& currentPos) {
    //Grid grid;
    int ht =  10;
    int width = 10;;
    setCurrentPose(currentPos);
    switch(id){
        case 0: {
                    if(current_pose.x - 0 < 5)
                        return 'e';
                    else if(current_pose.y - ht  < 5)
                        return 's';
                    else if(fabs(current_pose.x - (width/2)) < 5) 
                        return 'w';
                    else if(fabs(current_pose.y - (ht/2)) < 5)
                        return  'n';
                    else {

                        return defaultGetMove();
                        }


            }break;
        case 1:{
                if(fabs(current_pose.y - ht) < 5)
                    return 'e';
                else if(fabs(current_pose.x -x) < 5) 
                    return 'w';
                else if(fabs(current_pose.x - x/2) < 5)
                    return 's';
                else if(fabs(current_pose.y - ht/2) < 5)
                    return 'n';
                else return defaultGetMove();

            }break;
        case 2:{
                if(current_pose.x - 0 < 5)
                        return 'e';
                else if(fabs(current_pose.x - ht/2) < 5) 
                        return 'w';
                else if( fabs(current_pose.y - 0) < 5) 
                    return 'n';
                else if( fabs(current_pose.y - (width/2)) < 5)
                    return 's';
                else
                    return defaultGetMove();
            }break;
        case 3:{

                if(fabs(current_pose.x - width) < 5) 
                    return 'w';
                else if(fabs(current_pose.x - (width/2)) < 5)
                    return 'e';
                else if(fabs(current_pose.y - ht/2) < 5)
                    return 's';
                else if(fabs(current_pose.y - 0) < 5)
                    return 'n';
                else
                    return defaultGetMove();
            }
        }
}





   /home/bonny/bwi_gridworld/agent/src/myAgent.cpp:7:20: error: aggregate ‘bwi_gridworld::Pos current_pose’ has incomplete type and cannot be defined
     bwi_gridworld::Pos current_pose;
                        ^
    /home/bonny/bwi_gridworld/agent/src/myAgent.cpp: In function ‘void setCurrentPose(const bwi_gridworld::Pos&)’:
    /home/bonny/bwi_gridworld/agent/src/myAgent.cpp:23:19: error: no match for ‘operator*’ (operand type is ‘const bwi_gridworld::Pos’)
        current_pose = *currentPos;

【问题讨论】:

  • 只定义bwi_gridworld::Pos的成员类型!
  • 如果您遇到错误,您应该将它们包含在问题中。 “也不工作”有点太模糊了
  • 并且引用不会被 * 取消引用。
  • @DannyuNDos 你能告诉我如何定义成员的类型
  • 该错误表明它们尚未定义。弄清楚它们是什么并添加它们的定义。

标签: c++ reference constants


【解决方案1】:

好的,这就是我所做的 - 一条评论让我意识到您不能也不需要使用 * 运算符取消引用引用。你会用指针而不是引用来做类似的事情。

我的 struct Pos 有一个形式为 Pos(intx,int y, int time) 的构造函数: 所以我只是使用语句使 current_pose 成为结构的对象

bwi_gridworld::Pos current_pose((currentPos).x, (currentPos).y,0); 

它奏效了。长话短说,您不能也不能使用 * 取消引用引用(与指针不同)

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 2021-02-06
    • 2018-08-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多