Hi Tom Shelton,
Re: System.Collections.Stack stack
= new System.Collections.Stack();
Upon compiling that in a .CPP file, I got this error:
'System' : undeclared identifier
Re: using System.Collections;
That give me the following errors:
1. missing ';' before '.'
2. 'System' cannot be used in a using-declaration
As Tom mentionned already, that was a snippet of C# code. A "managed
C++" example (without much error checking) might look as follow:
#include <stdio.h>
#using <mscorlib.dll>
using namespace System;
#using "System.dll"
using namespace System::Collections;
public __gc class CVerySimple {
int m_nData;
public:
CVerySimple (int nData) : m_nData(nData) {}
const int get() { return m_nData; }
};
void main (void)
{
Stack* stack = new Stack();
CVerySimple *val = new CVerySimple (1234);
stack->Push(val);
CVerySimple *result = dynamic_cast<CVerySimple*>(stack->Pop());
if (val->get() == result->get()) {
printf ("It works!\n");
} else {
printf ("Error, result = %d\n", result->get());
}
}
Note that you'd need to use the /clr switch on the compiler command
line for the above snippet to compile. This tells the compiler that
the code might contains some .NET stuff and that you want to target
the common language runtime instead of the x86 processor.
Suppose that you saved the above snippet in a file called stack.cpp,
you'd run
cl /clr stack.cpp
to compile it.
If you don't have Visual Studio .NET, get a free version of the
Microsoft Optimizing C/C++ Compiler here:
http://msdn.microsoft.com/visualc/vctoolkit2003/
Just in case you'd like to check a C# example, it would look like
this:
using System;
using System.Collections;
namespace SimpleExample
{
class SimpleContainer {
// member(s)
public int data; // public for simplicity
// constructor
public SimpleContainer (int value)
{
this.data = value;
}
}
class SimpleClass
{
static void Main ()
{
try {
Stack stack = new Stack();
SimpleContainer val = new SimpleContainer(1234);
Console.WriteLine ("First test:");
stack.Push(val);
SimpleContainer result = (SimpleContainer)stack.Pop();
if (val.data == result.data) {
Console.WriteLine (" It works!");
} else {
Console.WriteLine (" Error, result = {0}",
val.data);
}
Console.WriteLine ("Second test:");
stack.Pop(); // stack was empty
} catch (InvalidOperationException e) {
Console.WriteLine (" Exception thrown: {0}",
e.Message);
}
}
}
}
use
csc stack.cs
to compile.
Or course you need the .NET framework to compile and run both samples.
You can get it from here:
http://tinyurl.com/95zt
http://www.microsoft.com/downloads/...e3-f589-4842-8157-034d1e7cf3a3&displaylang=en
If you want to begin playing with C#, be sure to try the Snippet
Compiler:
http://www.sliver.com/dotnet/SnippetCompiler/
The C# language specification is here:
http://tinyurl.com/n9d7
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/CSharpSpecStart.asp
Here is the .NET Framework class library reference:
http://tinyurl.com/1qzi
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp
Finaly, the .NET Framework SDK (lot's of samples and documentation)
http://tinyurl.com/97x7
http://www.microsoft.com/downloads/...a6-3647-4070-9f41-a333c6b9181d&displaylang=en
....everything to get you started if you want to...
--PV