gpt4 book ai didi

.net - 使用托管数组和 std :array at same time 不兼容

转载 作者:行者123 更新时间:2023-12-03 14:31:35 24 4
gpt4 key购买 nike

我的 C++/CLI 代码使用这样的数组(例如):

array<String^>^ GetColNames() { 
vector<string> vec = impl->getColNames();
array<String^>^ arr = gcnew array<String^>(vec.size());

for (int i = 0; i < vec.size(); i++) {
arr[i] = strConvert(vec[i]);
}
return arr;
}

在我将库“array”添加到项目之前,它编译得很好:
#include <array>

然后不知道怎么使用托管CLI数组,因为编译器认为所有声明的数组都是 std::array .

错误示例:
array<String^>^ arr
// ^ Error here: "too few arguments for class template "std::array""

gcnew array<String^>(vec.size())
// ^ Error: "Expected a type specifier"

如何解决这个问题?我尝试删除 using namespace std从那个文件,但它没有区别。我应该从项目中的所有其他 C++ 文件中删除它吗?

最佳答案

显然你有一个 using namespace std;在某处的范围内。如果找不到它,请注意它正在 .h 文件中使用。

您可以解决歧义,像数组这样的 C++/CLI 扩展关键字在 cli 中。命名空间。这编译得很好:

#include "stdafx.h"
#include <array>

using namespace std; // <=== Uh-oh
using namespace System;

int main(cli::array<System::String ^> ^args)
{
auto arr = gcnew cli::array<String^>(42);
return 0;
}

关于.net - 使用托管数组和 std :array at same time 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23062179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com