473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Ecma Doc Stack Example

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::object' : 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
Nov 17 '05 #1
5 1610
Don Kim wrote:
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;
}
Nov 17 '05 #2
> 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[i]);
}

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
Nov 17 '05 #3
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? ;)

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4
Don Kim wrote:
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[i]);
}

int main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(gcnew array<int> {1, 2, 3, 4});
}
Nov 17 '05 #5
Tomas Restrepo (MVP) wrote:
Need any help with that? ;)


Sure. I'll follow up offline. :-)

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
14
by: John Bentley | last post by:
Note this is crossposted to comp.lang.javacript and microsoft.public.dotnet.scripting. After some Googling and FAQing my understanding of these terms is, crudely: Javascript (3 different...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
19
by: anonymouse | last post by:
Is it possible to have C# run in an unmanaged environemnt if somebody should decide to implemnent it this way? Its possible to code C# projects without any dependancy on the libraries at all...
8
by: VK | last post by:
I'm a bit confused about ECMAScript docs. Can I legally change it format (say from .pdf to .html or .doc) or publish it on my web-site? ECMA provides a free of charge pdf version here:...
9
by: Edward Diener | last post by:
Are there any differences between the version of C++/CLI as implemented in Visual C++ 2005 and the ECMA-372 C++/CLI Language Specification of December 2005 freely downloaded from ECMA ? Asking...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.