- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个函数,其签名如下:
GetCustomers( wchar_t** Name,int *count);
在主要方法中:调用客户如下所示:
GetCustomers( Name,&count);
函数体如下:(由于客户数量未知,我尝试动态分配内存)
GetCustomers( wchar_t** Name,int *count)
{
//Logic to get customer count : Stored in int myCustomersCount
Names = new wchar_t*[myCustomersCount];
for (int i=0; i < myCustomersCount; i++ )
{
Names[i] = new wchar_t;
}
//Logic to get customer names in wchar_t* strName = "Name1";
Names[0] = strName;
*count = myCustomersCount;
}
我认为此实现将允许数组名称正确传递回 Main() 函数,并在堆上分配内存,但它似乎不起作用。这里有什么问题? myCustomersCount 在来电者中似乎是正确的。
PS:代码编译并执行,但 Main 中接收到的数组是垃圾。
最佳答案
您似乎在考虑 C,而不是真正的 C++。我会使用类似的东西:
std::vector<std::string> GetCustomers();
或(可能首选):
template <class outIt>
void GetCustomers(outIt output_iterator);
后者你会使用类似的东西:
std::vector<std::wstring> customers;
GetCustomers(std::back_inserter(customers));
第三个明显的可能性是为您的customers
类配备一个返回迭代器的begin()
和end()
成员函数到客户数据。
Edit2:这是一些经过测试的演示代码:
#include <stdio.h>
#include <string.h>
#include <wchar.h>
void GetCustomers(wchar_t ***names, int *count) {
static wchar_t *myCustomers[] = {
L"You",
L"Him",
L"Her"
};
int myCustomersCount = 3;
wchar_t **temp = new wchar_t *[myCustomersCount];
*count = myCustomersCount;
for (int i=0; i<myCustomersCount; i++) {
temp[i] = new wchar_t[wcslen(myCustomers[i])+1];
wcscpy(temp[i], myCustomers[i]);
}
*names = temp;
}
int main() {
wchar_t **customers;
int count;
GetCustomers(&customers, &count);
for (int i=0; i<count; i++)
printf("%S\n", customers[i]);
return 0;
}
关于c++ - 从函数返回动态分配的 wchar_t* 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5347954/
error C2440: 'initializing': cannot convert from 'TCHAR [260]' to 'std::basic_string,std::allocator>
首先。我想说,我不是 C++ 程序员。outlook 字符串在 visual studio 2015 中生成错误,并显示标题中的消息。 HWND windowHandle = (HWND)FindPr
我正在尝试检查一个 wchar_t * 是否等于另一个 wchar_t * 不区分大小写。有人可以把我放在正确的位置吗? 这是我尝试过的: wchar_t *vectored[80] = { ...
这是我的代码: wchar_t wbuffer[512]; wchar_t* wc = (wchar_t*) malloc(buffer_size); int buflen = 0;
如何正确转换这种方式? VARIANT varIndex; CString csIndex; //Index BSTR csIndex = (LPCSTR)(_bstr
我将以下代码作为我正在编写的字符串类中的私有(private)函数(它是从 wstring_convert “借来的”,并由我进行了一些修改): template std::string my_st
我有这样的东西: vector elements; 其中包含: 083938F8 083938FA 083938FC 083938FE 08393900 08393902 08393904 我想将它们
字符的签名不规范。因此,有signed char和unsigned char类型。因此,使用单个字符的函数必须使用既可以包含有符号字符又可以包含无符号字符的参数类型(此 类型选择为int),因为如果参
我在 Visual Studio C++ 2013 Community Edition 中遇到了一些奇怪的事情,这可能是编译器错误,也可能是我正在编写在没有警告的情况下进行编译的无效代码。 考虑以下片
我无法理解为什么某个隐式转换没有按我预期的那样工作。我有以下类(class) ref class ManagedWStringArrayWrapper { wchar_t** m_pointe
我有一个名为 DirToEmpty 的变量,它保存临时目录的路径。 我有另一个名为 TempBufDir 的变量,它将保持与 DirToEmpty 相同的值,但反斜杠被转义。 预期行为示例: wcha
我需要在附加 '\n' 后将多字节字符发送到套接字,但我拥有的是一个 const wchar_t*。我如何转换它? 最佳答案 如果您的问题是如何实际操作常量的内容,请考虑 const_cast . 关
我正在尝试使用语言环境方面将 wstring 转换为字符串,但我遇到了以下错误: test_facet.cpp: In function ‘int main()’: test_facet.cpp:14
我正在尝试使用 C++ 更改文件的属性我究竟做错了什么 ?代码完全拿下 https://learn.microsoft.com/en-us/windows/win32/stg/writeread-sa
我尝试将以下字符串数组传递给函数: const wchar_t *testTokens[] = { L"function", L"(",
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我尝试将阿拉伯字符存储为 wchar_t,然后将其打印到控制台,但无法使其工作。 我已将控制台设置为 UTF-8,如果我只是将阿拉伯字母输入控制台,它们就会正确显示(尽管未加入)。 我根据( How
我想存储一个包含扩展 ascii 表中字符的字符串,并打印它们。我尝试过: wchar_t wp[] = L"Росси́йская Акаде́мия Нау́к "; printf("%S", w
在C++ Visual Studio 2017中使用main函数是否错误如下: int main(int argc, wchar_t* argv[]) 因为我的程序可以接收特殊字符。 最佳答案 请阅读
我有这个运算符重载器。我的程序在创建新的 wchar_t 数组时崩溃。 myObject &operator += (const myObject &s) { wchar_t *cat = w
我是一名优秀的程序员,十分优秀!