为什么我在 ' ' c++ 之前得到 [Error] expected primary-expression?

Why am I getting [Error] expected primary-expression before ' ' c++?

我不确定为什么我的代码中会出现这些错误。我正在尝试为机器人制作一个基本的骨架构想,它需要通过提供给我的 main() 中的命令。我试图查找与此类似的错误,但没有运气。我认为这与我如何使用 main 中的方法有关,但我不确定如何更改它。非常感谢帮助。

#include <iostream>
using std::cout;
using std::endl;
enum direction {North, South, East, West};
class ur_Robot
{
    private:
        int ave;
        int str;
        direction dir;
        int num_beeper;
        bool on;
    public:
        ur_Robot(int a, int str, direction d, int nBeeper);
        print(const ur_Robot & u) const;
        void pickBeeper(int nBeeper);
        void turnLeft(enum direction);
        void move(enum direction, int b);
        void putBeeper(int nBeeper);
        void turnOff(bool o);
};

ur_Robot::ur_Robot(int a, int str, direction d, int nBeeper)
{
    ave = a;
    str = str;
    dir = d;
    num_beeper = nBeeper;
    on = 1;


}

ur_Robot::print(const ur_Robot & u) const
{
    if(on == 1)
    {
        cout << ave << " avenue, " << str; 
        cout << " street, " << dir << ", "; 
        cout << num_beeper << " number of beepers " << endl;
    };
    if(on == 0)
        cout << "ur_Robot is off and unable to take commands.";
}
void ur_Robot::turnLeft(enum direction)
{
    if(on == 1)
    {
        if (dir == North)
            dir = West; 
        if (dir == South)
            dir = East;
        if (dir ==  West)
            dir = South;
        if (dir == East)
            dir = North;
    }
    if(on == 0)
        cout << "ur_Robot is off and unable to take commands.";
}

void ur_Robot::putBeeper(int nBeeper)
{
    if(on == 1)
        num_beeper =- num_beeper;
    if(on == 0)
        cout << "ur_Robot is off and unable to take commands.";
}

void ur_Robot::pickBeeper(int nBeeper)
{
    if(on == 1)
        num_beeper =+ num_beeper;
    if(on == 0)
        cout << "ur_Robot is off and unable to take commands.";
}

void ur_Robot::turnOff(bool o)
{
    on = 0; 
}

int main()
{
  ur_Robot karel(1,1,East,10);

  karel.print (const ur_Robot & u) const;

  karel.move(enum direction, int b);
  karel.pickBeeper(int nBeeper);
  karel.move(enum direction, int b);
  karel.print(const ur_Robot & u) const;

  karel.turnLeft(enum direction);
  karel.move(enum direction, int b);
  karel.putBeeper(int nBeeper);
  karel.putBeeper(int nBeeper);
  karel.turnOff(bool o);

  karel.print(const ur_Robot & u);

  return 0;
}

In function 'int main()':
88  [Error] expected primary-expression before 'const'
90  [Error] expected primary-expression before 'enum'
90  [Error] expected primary-expression before 'int'
91  [Error] expected primary-expression before 'int'
92  [Error] expected primary-expression before 'enum'
92  [Error] expected primary-expression before 'int'
93  [Error] expected primary-expression before 'const'
95  [Error] expected primary-expression before 'enum'
96  [Error] expected primary-expression before 'enum'
96  [Error] expected primary-expression before 'int'
97  [Error] expected primary-expression before 'int'
98  [Error] expected primary-expression before 'int'
99  [Error] expected primary-expression before 'bool'
101 [Error] expected primary-expression before 'const'

您没有在成员函数的声明中指定 return 类型:

print(const ur_Robot & u) const;

查看实现,需要使用:

void print(const ur_Robot & u) const;

修复声明后,需要修改实现以匹配声明。

小东西:

如上所述,首先清理你的

声明
print(const ur_Robot & u) const;

void print(const ur_Robot & u) const;

同样,您需要更改

的定义
ur_Robot::print(const ur_Robot & u) const
{
    //...
}

void ur_Robot::print(const ur_Robot & u) const
{
    //...
}

此外,您对 using std::coutusing std::cin 的使用有些不标准,并且涵盖范围更广

using namespace std;

真正的问题:

您的代码中的大部分问题是由于您在调用 ur_Robot 的成员函数时没有传递任何内容,无论如何,您不需要.

例如,您的 print(const ur_Robot & u) const 实现仅使用成员变量,因此您可以完全删除对另一个 ur_Robot 对象的引用:

void print() const;

同样,它的定义也应该缩短:

void ur_Robot::print() const
{
    //...
}

这几乎适用于 ur_Robot 的所有成员函数。

此外,void ur_Robot::move(enum direction, int b) 从未被正确定义,因此尝试在 main() 中使用它会产生不太理想的结果。