间接需要指针操作数和预期的表达式错误
indirection requires pointer operand and expected expression errors
我不断收到与这些类似的错误:
pitstop.cpp:36:23: error: indirection requires pointer operand
('double' invalid)
cost = UNLEADED * gallons;
^ ~~~~~~~
pitstop.cpp:40:14: error: expected expression
cost = SUPER * gallons; ^
#include <iostream>
#include <iomanip>
using namespace std;
#define UNLEADED 3.45;
#define SUPER {UNLEADED + 0.10};
#define PREMIUM {SUPER + 0.10};
/*
Author: Zach Stow
Date:
Homework
Objective:
*/
double cost, gallons;
string gasType, finish, stop;
int main()
{
for(;;)
{
cout <<"Hi, welcome to Pitstop.\n";
cout <<"Enter the type of gas you need:";
cin >> gasType;
cout << endl;
cout <<"Enter the amount of gallons you need:";
cin >> gallons;
cout << endl;
if(gasType == "finish" || gasType == "stop")break;
else if(gasType == "UNLEADED")
{
cost = UNLEADED * gallons;
}
else if(gasType == "SUPER")
{
cost = SUPER * gallons;
}
else if(gasType == "PREMIUM")
{
cost = PREMIUM * gallons;
}
}
cout <<"You need to pay:$" << cost << endl;
return(0);
}
不是 c++
专家,但我敢肯定,要定义常量,您只需要使用 #define
指令,后跟符号和要分配给它的值(即使这个值本身是一个表达式,即使这个表达式引用了另一个常量),花括号和结尾的分号是多余的:
// [...]
#define UNLEADED 3.45
#define SUPER (UNLEADED + 0.10)
#define PREMIUM (SUPER + 0.10)
//[...]
它在第一次尝试时通过此类更正编译。
错误原因是#define指令末尾的分号。
您也使用了不正确的括号类型,试试这个:
#define UNLEADED 3.45
#define SUPER (UNLEADED + 0.10)
#define PREMIUM (SUPER + 0.10)
请注意,当您使用#define 指令时,#define 后面的任何内容都会被替换到您的代码中。在这种情况下,在预处理器 运行 之后,您的代码如下所示:
else if(gasType == "UNLEADED")
{
成本 = 无铅 3.45; * 加仑;
}
否则如果(gasType == "SUPER")
{
成本 = {无铅 + 0.10}; * 加仑;
}<br>
否则如果(gasType == "PREMIUM")
{
成本 = PREMIUM {SUPER + 0.10}; * 加仑;
}
您收到 indirection requires pointer operand
错误的原因是编译器试图解释此语句:
* gallons;
因为 *
运算符只有一个参数,它被解释为指针解引用,幸运的是 gallons
变量不是指针类型。如果 gallons 被声明为指针类型,即 double cost, *gallons;
而 cin
不存在,代码将编译但不会执行您期望的操作,可能会引发段错误。
用#define 定义的宏可能非常强大也非常危险。在 C++ 中通常有更好的方法来实现。在这种情况下,UNLEADED
、SUPER_UNLEADED
和 PREMIUM
可以声明为 const double
类型,即
const double unleaded = 3.45;
const double super = unleaded + 0.10;
const double premium = super + 0.10;
我不断收到与这些类似的错误:
pitstop.cpp:36:23: error: indirection requires pointer operand
('double' invalid)
cost = UNLEADED * gallons;
^ ~~~~~~~
pitstop.cpp:40:14: error: expected expression
cost = SUPER * gallons; ^
#include <iostream>
#include <iomanip>
using namespace std;
#define UNLEADED 3.45;
#define SUPER {UNLEADED + 0.10};
#define PREMIUM {SUPER + 0.10};
/*
Author: Zach Stow
Date:
Homework
Objective:
*/
double cost, gallons;
string gasType, finish, stop;
int main()
{
for(;;)
{
cout <<"Hi, welcome to Pitstop.\n";
cout <<"Enter the type of gas you need:";
cin >> gasType;
cout << endl;
cout <<"Enter the amount of gallons you need:";
cin >> gallons;
cout << endl;
if(gasType == "finish" || gasType == "stop")break;
else if(gasType == "UNLEADED")
{
cost = UNLEADED * gallons;
}
else if(gasType == "SUPER")
{
cost = SUPER * gallons;
}
else if(gasType == "PREMIUM")
{
cost = PREMIUM * gallons;
}
}
cout <<"You need to pay:$" << cost << endl;
return(0);
}
不是 c++
专家,但我敢肯定,要定义常量,您只需要使用 #define
指令,后跟符号和要分配给它的值(即使这个值本身是一个表达式,即使这个表达式引用了另一个常量),花括号和结尾的分号是多余的:
// [...]
#define UNLEADED 3.45
#define SUPER (UNLEADED + 0.10)
#define PREMIUM (SUPER + 0.10)
//[...]
它在第一次尝试时通过此类更正编译。
错误原因是#define指令末尾的分号。
您也使用了不正确的括号类型,试试这个:
#define UNLEADED 3.45
#define SUPER (UNLEADED + 0.10)
#define PREMIUM (SUPER + 0.10)
请注意,当您使用#define 指令时,#define 后面的任何内容都会被替换到您的代码中。在这种情况下,在预处理器 运行 之后,您的代码如下所示:
else if(gasType == "UNLEADED")
{
成本 = 无铅 3.45; * 加仑;
}
否则如果(gasType == "SUPER")
{
成本 = {无铅 + 0.10}; * 加仑;
}<br>
否则如果(gasType == "PREMIUM")
{
成本 = PREMIUM {SUPER + 0.10}; * 加仑;
}
您收到 indirection requires pointer operand
错误的原因是编译器试图解释此语句:
* gallons;
因为 *
运算符只有一个参数,它被解释为指针解引用,幸运的是 gallons
变量不是指针类型。如果 gallons 被声明为指针类型,即 double cost, *gallons;
而 cin
不存在,代码将编译但不会执行您期望的操作,可能会引发段错误。
用#define 定义的宏可能非常强大也非常危险。在 C++ 中通常有更好的方法来实现。在这种情况下,UNLEADED
、SUPER_UNLEADED
和 PREMIUM
可以声明为 const double
类型,即
const double unleaded = 3.45;
const double super = unleaded + 0.10;
const double premium = super + 0.10;