gpt4 book ai didi

c++ - 如何使用作为参数传递的信息来构建在其他 CPP 文件中定义的字符串

转载 作者:行者123 更新时间:2023-11-28 04:38:03 25 4
gpt4 key购买 nike

我有一个 C++ 程序,它有几个带有变量声明的 .hpp 文件(其中大部分是 NFS 文件系统的路径)和带有这些变量定义的 .cpp 文件。

在其中一些类型为 std::string 的变量中,我需要通过在主程序中附加一些作为参数传递的内容来构建其内容。例如:

文件constants.hpp:

namespace constants {
extern std::string cudnn_version;
extern const std::string path_caffe_cuda;
extern const std::string path_caffe_cuda_cudnn;
}

文件constants.cpp:

const std::string constants::path_caffe_cuda = "/nfs/apps/caffe/cuda";
const std::string constants::path_caffe_cuda_cudnn = constants::path_caffe_cuda + "/cudnn" + constants::cudnn_version;

constants::cudnn_version 的内容在主程序中作为参数询问用户并在那里更新。问题是,当 constants::path_caffe_cuda_cudnn 变量必须用 constants::cudnn_version 变量的内容构建时,它的内容仍然是空的,所以在某种程度上变量 < em>path_caffe_cuda_cudnn 在 constants::cudnn_version 具有用户传递的内容之前进行评估。

你认为我该如何解决这个问题?

非常感谢大家。

最佳答案

它可以通过使用一个函数来轻松完成:

namespace constants {
extern std::string cudnn_version;
extern const std::string path_caffe_cuda;

inline std::string path_caffe_cuda_cudnn()
{
return constants::path_caffe_cuda + "/cudnn" + constants::cudnn_version;
}
}

只要在constants::cudnn_version初始化之前不调用path_caffe_cuda_cudnn,就可以了。

关于c++ - 如何使用作为参数传递的信息来构建在其他 CPP 文件中定义的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50870077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com