在编译时找到最大公约数
Find greatest common divisor at compile time
我正在尝试使用模板在编译时找到最大公约数。请考虑以下代码:
#include "stdafx.h"
#include <iostream>
template<int N, int M, int K>
class A{
public:
static const int a=A<M,K,M%K>::a;
};
template<int N, int M>
class A<N,M,0>{
public:
static const int a=M;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << A<11,13,11>::a;
return 0;
}
这段代码可以工作,但是如果我尝试编写
#include "stdafx.h"
#include <iostream>
template<int N, int M>
class GCD{
public:
static const int a=A<N,M,N%M>::a;
};
template<int N, int M, int K>
class A{
public:
static const int a=A<M,K,M%K>::a;
};
template<int N, int M>
class A<N,M,0>{
public:
static const int a=M;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << GCD<11,13>::a;
return 0;
}
我有崩溃错误 C2059"常量"。
为什么这段代码不起作用?
您需要
在顶部为A
进行前向声明:
template<int N, int M, int K>
class A;
然后它在符合标准的编译器上工作正常。或者你可以把GCD
移到所有A
的东西下面。
现场示例
相关文章:
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 最大化双分区的 GCD(最大公约数)之和?
- 最大公因数函数
- 我可以强制C++类使用最小的空间进行编译吗?
- GCC 编译 C/C++ 最强化的选项集是什么?
- 为什么最简单的模板模板不能编译
- "port" Visual Studio C++项目以在 Linux 上编译的最简单方法是什么?
- 可以动态分配和在C 的编译时分配的最大存储器
- 程序编译并执行,但输出的不合理大的数字作为最大值.如何显示长双倍?
- 为什么我的程序最多只能编译 16 条记录
- 多项式最大公约数C++
- C/C++:errno关联字符串的最大大小(编译时)
- 实现欧几里得除法,根据两个正整数的线性组合编写这两个正整数的最大公约数
- 使用数组的 2 个数字的最大公约数
- 在编译时找到最大公约数
- C++最大公约数循环
- 直到n的所有数的最大公约数与n的和
- c++程序计算最大公约数
- 使用欧几里得算法识别两个值的最大公约数 (GCD)
- 找出最大公约数的程序