c++ - 方法不占用 long long int 值显示错误

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

<分区>

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

Error] conflicting types for 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);
  }
}

最佳答案

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

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

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

要解决此问题,您可以执行以下任一操作

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

关于c++ - 方法不占用 long long int 值显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31160478/

相关文章:

c++ - 如何将 CV_64F 类型的 Matrix 转换为具有 Double 元素的一维数组

c - 在 C 中声明字符串而不给出大小

c - 使用OpenSSL加密解密报错0x0407106B

c - 是否可以将结构转换为另一个结构?

c - 从 C 二叉树中删除节点而不弄乱它

c++ - 为什么链接器找不到我在类中声明的模板函数?

c++ - std::chrono & Boost.Units

c++ - C++将对象保存在列表中以供以后重用

c++ - n-ary 树 C++ 的错误

java - 是否可以使用计时器扩展Flex树中的所有节点?