同时使用托管数组和 std:array 不兼容 [英] Incompatibility using managed array and std:array at same time

查看:17
本文介绍了同时使用托管数组和 std:array 不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have my C++/CLI code using arrays like this (for example):

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"添加到项目之前,它编译得很好:

It's compiling fine until I add the library "array" to the project:

#include <array>

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

Then I don't know how to use the managed CLI array, because the compiler thinks that all the declared arrays are the 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++ 文件中删除它吗?

How to solve this? I tried removing using namespace std from that file, but it makes no difference. Should I remove that from every other C++ file on the project?

推荐答案

很明显,您在某个范围内有一个 using namespace std; .如果找不到它,请注意它在 .h 文件中使用.

Clearly you have a using namespace std; in scope somewhere. Watch out for it being used in .h file if you cannot find it.

您可以解决歧义,array 等 C++/CLI 扩展关键字位于 cli 命名空间中.这编译得很好:

You can resolve the ambiguity, the C++/CLI extension keywords like array are in the cli namespace. This compiles fine:

#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;
}

这篇关于同时使用托管数组和 std:array 不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆