0

I have a working VS2010 solution that has a C# program passing an array to a C++ DLL using this syntax:

In the C++ file:

void MyClass::MyFunction( array< Byte >^% byteArray, unsigned int size )

Where the first parameter is a pointer to the array and the second is the number of elements in it.

In the C# file I call this function like so:

MyClass myClass = new MyClass();
byte[] bArray = new byte[8];
myClass.MyFunction(ref bArray, 8)

This all works. But I am now trying to port the code to a VS2015 solution and the C++ code is failing to compile.

In the C++ project VS puts a red squiggle under the trailing > in the function definition, and the message seems to be too few arguments for class template "std::array"

My guess is that either the std::array template definition changed between VS2010 and VS2015, or the compiler itself changed (and is now more strict)

This question seems to back up my guess and points that the std::array needs an additional size parameter. However I am trying to pass in an array of an arbitrary size.

So my question is: can I achieve the same functionality in VS2015? If so, how?

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91

1 Answers1

4

In C++, std::array<T,n> absolutely is a fixed-size container. However, as you might have noted, you don't actually say std::array. This points at the actual problem: you used another array in VS2010, a definition that is now hidden by std::array. That is almost certainly caused by using namespace std;, something that is widely discouraged.

Fix: remove using namespace std; and use std:: where you mean it.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Except I am not `using namespace std`. But I do accept that I am probably not using `std::array` in my VS2010 solution – Peter M Aug 31 '16 at 14:35
  • @PeterM: Well, the error message is rather clear that you're using `std::array` when you write `array`, so that means you either have `using namespace std` or `using std::array` _somewhere_ prior to that error. Could be in a header, though. – MSalters Aug 31 '16 at 14:36
  • Ok I had to go back 3 levels of includes before I found it, but yeah I see the `using namespace std` now. The mystery to me is why I didn't have this issue in VS201 – Peter M Aug 31 '16 at 14:41
  • @PeterM: You might not have an explicit `#include ` but implementations are allowed to include one header from another. VS2015 probably has different header dependencies. – MSalters Aug 31 '16 at 14:55
  • 2
    I did eventually find https://msdn.microsoft.com/en-us/library/bb531344.aspx#BK_STL so I suspect that that is the case. – Peter M Aug 31 '16 at 14:58
  • @PeterM `using namespace std;` in a header is a recipe for huge problems. By the way, `cli::array` has a `Length` member, you don't need to pass the size separately. – Lucas Trzesniewski Aug 31 '16 at 18:51