tags:

views:

66

answers:

5

I am attempting to simply add a FilterInfo class to my FilterInfo collection. I'm having a terrible time trying to understand why the following code keeps throwing the error:

System::Collections::Generic::List::Add' : cannot convert parameter 1 from 'Ziz::FilterInfo *' to 'Ziz::FilterInfo'

I'm only learning C++/CLI, as I'm a C# developer, and I'm sure it's something simple, but I sure could use some pointers. My stripped code is as follows:

     public value class FilterInfo
    {
        public:
            char* Address;
    };

    public ref class Ziz
    {
    private:
        List<FilterInfo>^ _blockList;

    public:
        // Constructor
        Ziz(){
            _blockList = gcnew List<FilterInfo>();
        }

        List<FilterInfo>^ GetBlockList()
        {

            for each(_IPFILTERINFO ip in _packetFilter->GetBlockList())
            {
                // _IPFILTERINFO is the native C++ struct.
                FilterInfo* f = new FilterInfo();
                _blockList->Add(f);
            }
            return _blockList;
        }
A: 

You have to construct your FilterInfo with gcnew as well. You can't really mix and mash these together without marshaling.

Daniel A. White
A: 

FilterInfo is not FilterInfo*. If you want a List of pointers to FilterInfo, you need to say that List<FilterInfo*>. Since FilterInfo is a value class here though you'll likely just want to skip the new.

     FilterInfo fi;
     _blockList->Add(fi);
Logan Capaldo
+1  A: 

You declared _blockList as

List<FilterInfo>^ _blockList;

but you are trying to add

FilterInfo* f

to it. It cannot work since one is a pointer and the other one is a reference.

I'm not sure how "value" fits in but in

public value class FilterInfo
{
    public:
        char* Address;
};

You are derefore declaring an unmanaged class to make it managed, you should use

public ref class FiterInfo

This will allow you to use FilterInfo* without having to manage memory explicitely.

Finally, char* is not so great in C++/CLI, I would recommend using System::String

Eric
A: 
_blockList->Add(*f);
Windows programmer
A: 

public ref class A { };

int main(array ^args)
{
Console::WriteLine(L"Hello World");
ICollection^ oCollection = gcnew List();
oCollection->Add(gcnew A());
return 0;
}

roland