In VC++ 2008: "generic type argument for 'T'" problem

Dec 11, 2010 at 5:48 AM

Hi,

I am new to Pcap.Net as well as VC++.NET. When I try to deploy the C# example code "ObtainingTheDeviceList" to VC++, there are build errors which indicate:

error C3225: generic type argument for 'T' cannot be 'PcapDotNet::Core::LivePacketDevice', it must be a value type or a handle to a reference type

I have successfully created a C# project and built and run. It seems that the problem only happens in VC++.

Can anyone tell me how to solve this error? Thanks.

Here is the code:

-----------------------------------------------------------------------

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace PcapDotNet::Core;

int main(array<System::String ^> ^args)
{
	IList <LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
	
	if (allDevices.Count == 0)
	{
		Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
		return;
	}

	// Print the list
	for (int i = 0; i != allDevices.Count; ++i)
	{
		LivePacketDevice device = allDevices[i];
		Console.Write((i + 1) + ". " + device.Name);
		if (device.Description != null)
			Console.WriteLine(" (" + device.Description + ")");
		else
			Console.WriteLine(" (No description available)");
	}
    return 0;
}

Coordinator
Dec 13, 2010 at 5:21 AM

Hi gallon,

 

I think you need to use IList<LivePacketDevice^>^ as the type to indicate that it's a reference type list of a reference type objects.

 

Boaz.

Dec 13, 2010 at 5:29 AM

Hi, brickner

 

Thank you for your suggestion. Actually, I have changed it to IList<LivePacketDevice^>^ , but there were other errors coming so that I changed it back.

 

Here is the changed code:

IList <LivePacketDevice^>^ allDevices = LivePacketDevice.AllLocalMachine;

Then I get the following error:

error C2275: 'PcapDotNet::Core::LivePacketDevice' : illegal use of this type as an expression

and

error C2039: 'get' : is not a member of 'PcapDotNet::Core::LivePacketDevice::AllLocalMachine'

I am really confused. Would you or anyone can help me with this?

Thanks.


Coordinator
Dec 13, 2010 at 5:34 AM

One thing is that in C++ you need to use double colons (::) instead of a dot (.) for accessing static methods so this means you need to do LivePacketDevice::AllLocalMachine

Dec 13, 2010 at 6:02 AM

Thank you and sorry for my dummy questions... but there is one more after I changed all the C# grammar to C++

if (device->Description != NULL) {}

This causes a build error which says:

error C2446: '!=' : no conversion from 'int' to 'System::String ^' 

I think this is because device->Description is not a common type but a reference type so NULL cannot be used to judge whether the information is available. What should I use here to make the expression legal?

Thanks.

Coordinator
Dec 13, 2010 at 6:08 AM

In C++/CLI, the keyword for a reference null pointer is nullptr (equivalent to C#'s null) so I believe you should write

if (device->Description != nullptr) {}

Dec 13, 2010 at 6:42 AM

Thank you, brickner. Now the program can be built.

I have done a lot programming in C and would like to learn .Net and CLI part of VC++. Could you please recommend some material/books to read?

Coordinator
Dec 13, 2010 at 7:08 AM

I have learned it a few years ago and I can't remember what material I have used to learn it.

C++/CLI is quite a complex language for various reasons. It includes basically all the features of C++ (which is a complex language on its own) and all the features of .NET. Another big problem is that unlike C++ and .NET, there is no good support by the IDE for this language.

I suggest learning C++ and .NET before trying to learn C++/CLI. For both these languages there are a lot of great books and tutorials and I'm sure you can find something that will fit the way you like to learn (different people learn in different ways).

Once you know both these languages well, C++/CLI will be easier to handle since you just need to know the diff between it and C++ and .NET. Every time I had a problem with that language, I simply Googled my issue and found a solution... As I said, unlike C++ and .NET, the IDE will help you solve the problems much.

I also would suggest thinking a lot before deciding to write something in C++/CLI. Because of the lack of support for it, I don't see why would you want to use it unless you're wrapping C or C++ code in .NET. If you do that, try to limit your C++/CLI project to do that alone and do the higher level .NET programming or lower level C and C++ programming in different projects.

 

Boaz.