简体   繁体   English

为什么我的C ++ main()不使用我的参数

[英]why my C++ main() doesn't take my parameters

I have a C++ program running in Fedora 20. It can take no parameter or one parameter when I start it via the main(). 我有一个在Fedora 20中运行的C ++程序。通过main()启动它时,它可以不带任何参数或只有一个参数。 Now I need more parameters but somehow, it refuse to take the new parameters. 现在,我需要更多参数,但是不知何故,它拒绝采用新参数。

This is how I start it: 这是我的开始方式:

$ ./runtest.sh ire 22 33 $ ./runtest.sh ire 22 33

This is the first line cout inside the main(): 这是main()中的第一行cout:

UICommTest is starting ... argc=2 UICommTest正在启动... argc = 2

I expect the argc=4. 我期望argc = 4。

This is my main(): 这是我的main():

int main(int argc, char*argv[])
{

    std::cout << "UICommTest is starting ... argc="<< argc << std::endl;

    if(argc == 1)
    {
        UICommTest::startTest(false);
    }
    else if(argc == 2)
    {
        if(string(argv[1]) == "true")
        {
            UICommTest::startTest(true);
        }
        else if(string(argv[1]) == "ire")
        {
            UICommTest::startTestIRE("-1", "-1");
        }
        else
        {
            std::cout << "Usage:: ..." << std::endl;
        }
    }
    else if(argc == 4)
    {
        if(string(argv[1]) == "ire")
        {
            UICommTest::startTestIRE(argv[2], argv[3]);
        }
        else
        {
            std::cout << "Usage:: ..." << std::endl;
        }
    }
    else
    {
        std::cout << "Usage:: ..." << std::endl;
    }

    return EXIT_SUCCESS;
}

I do clean and make several times. 我做几次清洁。 And I am sure the new changes are in. Anybody can help about this? 而且我敢肯定,新变化已经到来。有人可以提供帮助吗?

So you have a shell script runtest.sh , it seems. 因此,您似乎拥有一个shell脚本runtest.sh In it you probably have a line like this (path may be something longer than just ./ ): 在其中,您可能会有这样的一行(path可能比./更长):

./UICommTest "$1" "$2"

It should be apparent that this will give your program only 2 arguments. 显然,这只会为您的程序提供2个参数。 Change that line to this: 将该行更改为此:

./UICommTest "$@"

Note: Using the "$@" is important detail, it has the special feature, that it expands to equivalent of "$1" "$2" "$3" ... . 注意:使用"$@"是重要的细节,它具有特殊的功能,它可以扩展为等效于"$1" "$2" "$3" ... The quotes are important, so that if you pass a parameter with spaces, it still remains as one parameter. 引号很重要,因此,如果您传递带空格的参数,则该参数仍将保留为一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM