Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2004

Tip: Looking for answers? Try searching our database.

Ecma Doc Stack Example

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Don Kim - 20 Jul 2004 08:26 GMT
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
Brandon Bray [MSFT] - 20 Jul 2004 17:22 GMT
> 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.

Signature

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;
}

Don Kim - 20 Jul 2004 22:22 GMT
> 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
Brandon Bray [MSFT] - 21 Jul 2004 21:45 GMT
> 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.

Signature

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});
}

Tomas Restrepo \(MVP\) - 20 Jul 2004 23:38 GMT
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? ;)

Signature

Tomas Restrepo
tomasr@mvps.org

Brandon Bray [MSFT] - 21 Jul 2004 21:46 GMT
Tomas Restrepo (MVP) wrote:
> Need any help with that? ;)

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

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.