gpt4 book ai didi

c++ - 简单的C++程序错误c2064 : term does not evaluate to a function taking 0 arguments

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:30 25 4
gpt4 key购买 nike

新用户,编程新手,x 秒的搜索没有找到我的答案。完整代码(不完整 - 以及基本内容的在线测验):

#include "stdafx.h"
#include <iostream>
#include "constant.h"

double towerheight()
{
std::cout << "Input tower height" << std::endl;
double height;
std::cin >> height;

return height;
}

double ballheight(double towerheight)
{
//valid stuff goes here in program, but not applicable to question
}

int main()
{
double towerheight;
towerheight = towerheight(); //Error occurs here
ballheight(towerheight);

return 0;

}

从底部开始的 3 行忽略空格和右括号是发生错误的地方。 (term 不计算为采用 0 个参数的函数。)

我在研究结束时有点迷茫,需要一些人工帮助。

对于可能格式不佳的问题,我们深表歉意。请告知,我可能不知道导致错误的某些术语,我的目标是了解发生了什么。

编辑:

我的解决方案是更改错误上方的 double 变量的名称。为什么这会成为一个问题?

最佳答案

main 中,名为 towerheight 的变量隐藏了函数 towerheight。因此,

towerheight = towerheight(); 

无效。它类似于对变量调用函数。

double towerheight;
double dummy;
towerheight = dummy();

这就是为什么它不正确。

解决方案一

您可以使用:

double towerheight;
towerheight = ::towerheight();

使用 ::towerheight 是可行的,因为它指的是封闭命名空间中的名称,而不是本地定义的同名变量。

方案二

使用不同的名称。

您可以通过使用两个不同的名称来避免一些混淆。

double t_height = towerheight();

关于c++ - 简单的C++程序错误c2064 : term does not evaluate to a function taking 0 arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34080179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com