Ecma Doc Stack Example

D

Don Kim

I copied this example off ECMA C++/CLI draft document, and it doesn't
compile:

#using <mscorlib.dll>
using namespace System;

public ref class Stack {
public:
Stack() {
first = nullptr;
}

property bool Empty {
bool get() {
return (first == nullptr);
}
}

Object^ pop() {
if (first == nullptr)
throw gcnew Exception("Can't Pop from an empty Stack!");
else {
Object^ temp = first->Value;
first = first->Next;
return temp;
}
}

void Push(Object^ o) {
first = gcnew Node(o, first);
}

ref struct Node {
Node^ Next;
object^ Value;
Node(object^ value) : Node(value, nullptr) {}
Node(object^ value, Node^ next) {
Next = next;
Value = value;
}
};
private:
Node^ first;
};

int main()
{
Stack^ s = gcnew Stack();
for (int i = 0; i < 10; i++)
s->Push(i);
s = nullptr;
}

I get the following errors:

8_4.cpp
8_4.cpp(32) : error C2143: syntax error : missing ';' before '^'
8_4.cpp(32) : error C2501: 'Stack::Node::blush:bject' : missing storage-class or
type specifiers
8_4.cpp(32) : error C2501: 'Stack::Node::Value' : missing storage-class or
type specifiers
8_4.cpp(33) : error C2143: syntax error : missing ')' before '^'
8_4.cpp(33) : error C2143: syntax error : missing ';' before '^'
8_4.cpp(33) : error C3149: 'Stack::Node' : cannot use this type here without
a top-level '^'
8_4.cpp(33) : error C2059: syntax error : ')'
8_4.cpp(33) : error C2065: 'value' : undeclared identifier
8_4.cpp(33) : fatal error C1903: unable to recover from previous error(s);
stopping compilation

I'm using .Net 2.0 Beta. Any clues?

-Don Kim
 
B

Brandon Bray [MSFT]

Don said:
I copied this example off ECMA C++/CLI draft document, and it doesn't
compile:

The project editor adapted this from a C# example before a compiler was
available and thus missed the capitalization of a few "object" identifiers.
He was also using the delegating constructor feature which we removed from
the standard due to the ISO C++ committee's interest in changing the design.

I've included a corrected sample below. Now that a compiler is available,
one of my tasks (somewhere around #50 on my to do list) is to extract all
the examples from our language specification and compile them.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.


using namespace System;

public ref class Stack {
public:
Stack() {
first = nullptr;
}

property bool Empty {
bool get() {
return (first == nullptr);
}
}

Object^ pop() {
if (first == nullptr)
throw gcnew Exception("Can't Pop from an empty Stack!");
else {
Object^ temp = first->Value;
first = first->Next;
return temp;
}
}

void Push(Object^ o) {
first = gcnew Node(o, first);
}

ref struct Node {
Node^ Next;
Object^ Value;
Node(Object^ value) {
Next = nullptr;
Value = value;
}
Node(Object^ value, Node^ next) {
Next = next;
Value = value;
}
};
private:
Node^ first;
};

int main()
{
Stack^ s = gcnew Stack();
for (int i = 0; i < 10; i++)
s->Push(i);
s = nullptr;
}
 
D

Don Kim

I've included a corrected sample below. Now that a compiler is available,
one of my tasks (somewhere around #50 on my to do list) is to extract all
the examples from our language specification and compile them.

Thanks for the corrections. Since there is no real tutorial yet on C++/CLI,
except things like your webcast on the VC site which I enjoyed and other
small snippets, I figure I'm going to learn about it as much as I can by
just reading the specs and running the examples. Here's another example
that did not compile:

#using <mscorlib.dll>
using namespace System;
using namespace stdcli::language;

void F(... array<int>^ args) {
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine("\targs[{0}] = {1}", i, args);
}

int main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(gcnew array<int> {1, 2, 3, 4});
}

This one is for the parametized array. I get these errors:

8_3.cpp
8_3.cpp(5) : error C2144: syntax error : 'stdcli::language::array<Type>'
should be preceded by ')'
with
[
Type=int
]
8_3.cpp(5) : warning C4518: '?$array@H$00 ' : storage-class or type
specifier(s) unexpected here; ignored
8_3.cpp(5) : error C2143: syntax error : missing ';' before '^'
8_3.cpp(5) : error C2059: syntax error : ')'
8_3.cpp(5) : error C2470: 'args' : looks like a function definition, but
there is no formal parameter list; skipping apparent body

- Don Kim
 
T

Tomas Restrepo \(MVP\)

Hi Brandon,
I've included a corrected sample below. Now that a compiler is available,
one of my tasks (somewhere around #50 on my to do list) is to extract all
the examples from our language specification and compile them.

Need any help with that? ;)
 
B

Brandon Bray [MSFT]

Don said:
Thanks for the corrections. Since there is no real tutorial yet on
C++/CLI, except things like your webcast on the VC site which I enjoyed
and other small snippets, I figure I'm going to learn about it as much as
I can by just reading the specs and running the examples.

I'm working hard towards getting more content, as are a few other people. In
any case, I'm happy to assist in forums however I can.
Here's another example that did not compile:

This example regarding param arrays also demonstrates some unfinished work
in the compiler. Instead of using the ... for the param array, substitute
the actual attribute that goes into metadata:

System::Runtime::InteropServices::paramArray

That will make it work now. Later you will be able to use the ... instead.
I've copied a corrected sample that compiles.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.


using namespace System;
using namespace stdcli::language;
using namespace System::Runtime::InteropServices;


void F([ParamArray] array<int>^ args) {
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine("\targs[{0}] = {1}", i, args);
}

int main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(gcnew array<int> {1, 2, 3, 4});
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top