我目前正在编写一个使用c ++编写汇编程序的任务。当输入一个数字时,我已经得到代码区分,并且我试图让程序将16位数组中的相关位更改为所需的二进制数。
问题在于,在计算功率之后,输入数字的剩余部分和超过它的第一个功率被分配给一个int,然后将其传递回dec_to_bin()我收到以下错误。
assembler.cpp:在函数'void dec_to_bin(int,int *)'中: assembler.cpp:135:32:错误:从'double(*)(double,double)throw()'到'int'的无效转换[-fpermissive] dec_to_bin(余数,指针);
//*pointer points to an array[16]
void dec_to_bin(int num, int *pointer)
{
//Base cases
if(num == 2)
{
pointer[14] = 1;
return;
}
else if(num == 1)
{
pointer[15] = 1;
return;
}
else
{
int power = 0;
//power function outlined below. The inbuilt pow(a, b) returns a
//double and gave the same problem, so I tried this instead.
while(PowerFunc(2, power) < num)
{
power++;
}
//Problem arises here. remainder is assigned as an int
//Using typeinfo shows it as an int, yet passing it a few lines
//down gives the quoted error.
int remainder = PowerFunc(2, power)-num;
pointer[16-power] = 1;
}
if(remainder == 0)
{
return;
}
else
{
//Line that throws the error. Saying remainder is a double.
dec_to_bin(remainder, pointer);
}
}
//Power function to replace pow(a, b). Not ideal but it works.
int PowerFunc(int number, int powernum)
{
if(powernum == 0)
{
return 1;
}
else if(powernum == 1)
{
return number;
}
else
{
return number * PowerFunc(number, powernum-1);
}
}
这让我老老实实地难过。起初我曾经想过使用pow(a,b)并将它分配给一个int只会将小数部分拼接掉,但即使自己生成幂函数仍然告诉我我的int是双倍的。
答案 0 :(得分:3)
该行
int remainder = PowerFunc(2, power)-num;
出现在else
下的花括号分隔块之间,因此remainder
变量的范围只向下延伸一行。
remainder
if(remainder == 0)
并在
dec_to_bin(remainder, pointer);
是一些其他实体,巧合的是同名。您可以通过将remainder
重命名为其他内容来验证,例如myremainder
...
附加说明:
考虑用if(powernum == 0)
替换if(powernum <= 0)
- 这样可以在负指数的情况下为您节省大量的计算。
我还建议替换递归
return number * PowerFunc(number, powernum-1);
迭代:
int result = 1;
while(powernum-- >= 0)
result *= number;
return result;
答案 1 :(得分:1)
很简单,@Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyApplication.get(this).applicationComponent().inject(this);
getIntentValues(intent);
buildNotification();
return START_REDELIVER_INTENT;
}
private void getIntentValues(Intent intent) {
// Here I get some strings from the intent ...
}
private void buildNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_action_search);
builder.setContentTitle("Upload");
builder.setContentText("Thank you for uploading!");
builder.setPriority(Notification.PRIORITY_MIN);
Notification notification = builder.build();
// Start the Service in the Foreground
startForeground(NOTIFICATION_ID, notification);
Thread taskThread = new Thread(new UploadVideoTask());
taskThread.start();
}
也是在其他地方声明的函数,并且编译器抱怨remainder
将dec_to_bin
作为其第一个参数而不是指向类型为{double
的函数的指针。 1}}。
您在double (*)(double, double)
范围内声明的dec_to_bin(remainder, pointer);
变量remainder
无法显示。
您的else
也有问题,因为它始终会评估为false。