是什么导致了这个 "invalid type argument of unary '*'"?

What's causing this "invalid type argument of unary '*'"?

好的,所以我已经尽我的小脑袋进行了研究。我还没有找到解决我的问题的答案。尝试编写一个计算器,将基数提高到不高于 214783647 的数字。截至目前,我正试图让程序在没有设置值的情况下运行。但这是调试器所说的:

main.cpp: In function 'int main()':
main.cpp:25:11: error: invalid type argument of unary '*' (have 'int')
 result = *x;

这是代码。

#include <iostream>
using namespace std;

int solve(int, int, char);

int main() 
{

    int x = 0;
    int y = 0;
    int result = 1;
        cout << "Enter your base:";
        cin >> x;
        cout << "Enter the number to raise base by:";
        cin >> y;

for (x = 0; result <= y; result++)
{
result = *x;
}
        cout << result;

return 0;

}

我正处于 C++ 的初级阶段,但我可以接受所有建设性的批评。

已修复!! 我不确定这是否允许,但感谢大家,我终于让程序完全运行了!

这里的问题是您正试图取消引用一个不是指针的变量。只需从 result = *x; 中删除 * 即可将 x 的值分配给 result.

但是我不相信你会得到想要的效果。因为您在循环中使用 x 并将其初始化回 0,所以您正在破坏用户输入的任何值。

我相信您正在尝试执行以下操作

int x = 1;
int y = 1;

std::cout << "Enter your base:";
std::cin >> x;
std::cout << "Enter the number to raise base by:";
std::cin >> y;

int result = x;

for(int i = 1; i < y; i++ ) { //loop y times 
  result = result * x;        //exponents just times itself y times
}

std::cout << result << endl;

还值得一提的是,在使用标准库中的内容时,使用 std:: 命名空间是一种很好的形式,即使您的编译器不需要它。以及使用结束行 endl 结束任何输出,否则您的终端提示将在输出后立即开始。希望对您有所帮助。