#2
雪影辰风2020-02-23 19:18
|
程序代码:
怎么让这三个文件运行?
constants.cpp:
namespace constants
{
// actual global variables
extern const double pi { 3.14159 };
extern const double avogadro { 6.0221413e23 };
extern const double my_gravity { 9.2 }; // m/s^2 -- gravity is light on this planet
}
constants.h:
#ifndef CONSTANTS_H
#define CONSTANTS_H
namespace constants
{//由于实际变量位于名称空间内,因此前向声明也必须位于名称空间内
// since the actual variables are inside a namespace, the forward declarations need to be inside a namespace as well
extern const double pi;
extern const double avogadro;
extern const double my_gravity;
}
#endif
main.cpp:
#include "constants.h" // include all the forward declarations
int main()
{
std::cout << "Enter a radius: ";
int radius{};
std::cin >> radius;
std::cout << "The circumference is: " << 2 * radius * constants::pi;
}
//已经解决//
constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
// define your own namespace to hold constants
namespace constants
{
inline constexpr double pi { 3.14159 }; // note: now inline constexpr
inline constexpr double avogadro { 6.0221413e23 };
inline constexpr double my_gravity { 9.2 }; // m/s^2 -- gravity is light on this planet
// ... other related constants
}
#endif
main.cpp:
#include "constants.h"
int main()
{
std::cout << "Enter a radius: ";
int radius{};
std::cin >> radius;
std::cout << "The circumference is: " << 2 * radius * constants::pi;
}
[此贴子已经被作者于2020-2-23 20:27编辑过]