c++ - 头文件中声明的变量不能在主文件中使用?
问题描述
我有以下头文件:
#include <iostream>
const int x = 15;
const int y = 15;
const double lx = 2 / (x - 1);
const double ly = 2 / (y - 1);
以及以下代码:
#include "t.h"
int main()
{
std::cout << "x: " << x << std::endl;
std::cout << "y: " << y << std::endl;
std::cout << "lx: " << lx << std::endl;
std::cout << "ly: " << ly << std::endl;
std::cout << "lx / 2: " << lx / 2 << std::endl;
std::cout << "ly / 2: " << ly / 2 << std::endl;
}
输出:
x: 15
y: 15
lx: 0
ly: 0
lx / 2: 0
ly / 2: 0
并且由于某种原因,我无法弄清楚为什么lx
和ly
输出0
,显然当我与它们一起操作时它也是0
。我究竟做错了什么?
谢谢
解决方案
推荐阅读
- javascript - 将对象传递给函数
- c# - DataValueField 中有两个值?
- algorithm - 洪水建模(修改 Dijkstra 以使用路径权重是其边的最大权重的图。)
- javascript - 函数返回未定义,但变量在函数范围内未定义
- python - Jupyter notebook 在屏幕锁定时停止运行(不是睡眠模式)
- java - 如何对无限通量进行排序?
- jsx - 如何通过 Jsx 在 Photoshop 中选择“透明形状图层”
- qt - QML:如何隐藏子菜单?
- javascript - 在以 HTML 运行的 Javascipt 中循环减慢超时并最终崩溃
- python - Pandas 中的方法链:str.replace 不起作用