将' args '声明为引用数组错误

declaration of ‘args’ as array of references Error

本文关键字:数组 错误 引用 args 声明      更新时间:2023-10-16

我是c++ boost的新手,我有一个程序试图编译它



#include "Program.h"
#include <boost/asio/io_service.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/udp.hpp>
namespace ConsoleApp
{
    void Main(std::wstring& args[])
    {
            .
            .
    }
}

出现的错误是

Program.cpp:11:31: error: declaration of ‘args’ as array of references
  void Main(std::wstring& args[])
这里有人可以帮助我,这个代码是错误的吗?由于

这个错误几乎说明了一切。std::wstring& args[]是wstring (std::wstring)引用(&)的数组([])。不能有引用数组——参见为什么引用数组是非法的?

注意:如果你用c++编程,main函数应该如下:

int main(int argc, char *argv[])
{
    // Your code
    return 0;
}
编辑:

和AFAIK main函数不能在任何命名空间

另外,你的代码还有一个问题——即使我们可以创建引用数组,也没有存储关于数组长度的信息。除了第一个元素,你不能使用它!

无论如何,你可以这样做(替换wstringstring,因为我很懒):

#include <vector>
#include <string>
namespace ConsoleApp
{
    void Main(std::vector<std::string> &args)
    {
    }
}
int main(int argc, char *argv[])
{
    std::vector<std::string> args;
    args.resize(argc);
    for(int i = 0; i < argc; ++i)
    {
        args[i] = argv[i];
    }
    ConsoleApp::Main(args);
    return 0;
}