在主函数之外使用命令行参数

发布于 2025-01-15 06:07:07 字数 1468 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2025-01-22 06:07:07

首先,ptr 初始化得太早,而 argv[1] 不可用。

其次,main局部变量run与用于设置ptr的全局run无关,该全局变量在中使用>玩具1

由于我们不知道toy是什么,我们无法回答更多。 8-DK 答案是一种可能性。

First, ptr is initialised too early, at a time where argv[1] is not available.

Second, main local variable run is not related to global run used to set ptr that is used in Toy1.

As we don't know what toy is we can not answer much more. 8-DK answer's is one possibility.

冷夜 2025-01-22 06:07:07

这是我测试过的代码。试试这个

#include <iostream>
#include <fstream>
#include <math.h>

struct run_t {
    std::string file;
};

run_t run;

class POINTER{
  public:
    POINTER(std::string file , int num){std::cout << "POINTER::POINTER(std::string file , int num)file " << file << std::endl;};
    double func(double a, double b, double c) {
        POINTER *toy(std::string file , int num);
        return (double)0;
    }
};


POINTER *toy(std::string file , int num)
{
    POINTER* ptr = new POINTER(file , num);
    std::cout << "POINTER *toy(std::string file , int num) file " << file << std::endl;
    return ptr;
}

const POINTER * ptr;

double Toy1(double a, double b, double c) {
    POINTER * ptrCst = const_cast<POINTER *>(ptr);
    return ptrCst->func(a, b, c);
};

double Toy2(double d) {
    double factor = pow(d, 2); //some dummy prefactor
    return factor * Toy1(4, 5, 6);
};

int main(int argc, char* argv[])
{
    run.file = argv[1];
    POINTER * ptrCst = const_cast<POINTER *>(ptr);
    ptrCst = toy(run.file, 0);
    std::cout << Toy2(1) << std::endl;
    while(1);
    return 0;
}

Here is the code I have tested. Try this

#include <iostream>
#include <fstream>
#include <math.h>

struct run_t {
    std::string file;
};

run_t run;

class POINTER{
  public:
    POINTER(std::string file , int num){std::cout << "POINTER::POINTER(std::string file , int num)file " << file << std::endl;};
    double func(double a, double b, double c) {
        POINTER *toy(std::string file , int num);
        return (double)0;
    }
};


POINTER *toy(std::string file , int num)
{
    POINTER* ptr = new POINTER(file , num);
    std::cout << "POINTER *toy(std::string file , int num) file " << file << std::endl;
    return ptr;
}

const POINTER * ptr;

double Toy1(double a, double b, double c) {
    POINTER * ptrCst = const_cast<POINTER *>(ptr);
    return ptrCst->func(a, b, c);
};

double Toy2(double d) {
    double factor = pow(d, 2); //some dummy prefactor
    return factor * Toy1(4, 5, 6);
};

int main(int argc, char* argv[])
{
    run.file = argv[1];
    POINTER * ptrCst = const_cast<POINTER *>(ptr);
    ptrCst = toy(run.file, 0);
    std::cout << Toy2(1) << std::endl;
    while(1);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文