首页 > 解决方案 > 头文件中声明的变量不能在主文件中使用?

问题描述

我有以下头文件:

#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

并且由于某种原因,我无法弄清楚为什么lxly输出0,显然当我与它们一起操作时它也是0。我究竟做错了什么?

谢谢

标签: c++

解决方案


推荐阅读