将' args '声明为引用数组错误
declaration of ‘args’ as array of references Error
我是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函数不能在任何命名空间
另外,你的代码还有一个问题——即使我们可以创建引用数组,也没有存储关于数组长度的信息。除了第一个元素,你不能使用它!无论如何,你可以这样做(替换wstring
与string
,因为我很懒):
#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;
}
相关文章:
- 类动态数组错误
- 如何返回多维数组?错误C2440
- 数组错误(不允许类型不完整)
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 从文本文件读取 2D 数组错误
- 动态结构数组错误
- 旋转数组 - 错误
- 按字母顺序排序名称,数组错误
- 因为每个循环都读取数组错误
- 二维数组错误
- C 获取数组错误
- 编译时出现字符串数组错误
- c++2维指针类数组错误(使用openframework)
- C++数组错误
- C++上数组错误的大小
- C++数组错误:访问冲突读取位置0xC0000005
- c++2D数组错误
- C++:为“char [4]”数组错误获取太多初始值设定项
- 分段错误(核心转储)排序字符串数组错误