Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

.net - Incompatibility using managed array and std:array at same time

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

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

#include <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.

Errors examples:

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...