【问题标题】:method isn't taking long long int value shows error [duplicate]方法不需要 long long int 值显示错误 [重复]
【发布时间】:2015-07-01 11:50:28
【问题描述】:

我正在创建一个使用long long int 的简单程序,但出现编译器错误。请帮我解决这个错误。

错误] countTrees 的类型冲突

我在这行出错了

long long int countTrees(long long int numKeys)

这是我的代码:

#include <stdio.h>
#include <math.h>

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        long long int n;
        scanf("%lld", &n);
        long long int result = countTrees(n);
        printf("%lld\n",result);        
    }

return 0;
}  
long long int countTrees(long long int numKeys) {

  if (numKeys <=1) {
    return(1);
  }
  else {
    long long int sum = 0;
    long long int left,right,root;

    for (root=1; root<=numKeys; root++) {
      left = countTrees(root - 1);
      right = countTrees(numKeys - root);

      sum += left*right;
    }

    return(sum);
  }
}

【问题讨论】:

  • Error] 'countTrees' 的类型冲突

标签: c++ c tree binary-search-tree


【解决方案1】:

您收到错误,因为 countTrees() 的隐式声明与实际定义不匹配。

为了澄清调用函数时的“隐式声明”,但编译器尚未看到函数定义,它假设该函数返回int 并接受任何数字(和类型)参数。

C99 和未来的标准中,隐式声明 是无效的,因此如果一个尚未定义(或至少。原型)的函数通过前向声明编译器应该抱怨),被使用(调用)。

要解决此问题,您可以这样做

  • 添加函数的前向声明。
  • main()之前定义函数。

【讨论】:

  • result is so big long long int 不起作用。还有其他选择吗??
  • @SaketMittal 试试long long unsigned
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多