Invalid token .... Very Very simply program, why do I get this?

N

Nelmr1

I'm using 2002.Net, 1.0 framwork.

I am basically learning C# so I wrote this and get invalid token
errors. Does anyone see why?

using System;


namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.maxSize; ++i)
{
Console.Write("Pushing [" + stack.Push(i+10)+ "]") ;
Console.WriteLine(" now Top becomes: " + stack._top) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.maxSize; ++j)
{
Console.Write("Popping [" + j + "] of " + stack.maxSize) ;
Console.WriteLine("Content: " + stack.Pop()) ;
}
}
}


}





using System;

namespace ConsoleApplication2
{

class stack
{

//ERRORS BEGIN HERE SUCH AS "Invalid
token ";" in the private _top; line.
private _top;
public int const maxsize = 10;
private _arr[maxSize];

public stack()
{
_top = 0;

}

void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
break;
}
}

int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
break;
}
}
{}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

You haven't defined the type of _top. The compiler doesn't know what it
is.

Hope this helps.
 
B

Bruce Wood

You get the error because in the cases of _top and _arr you don't say
what type they are.

You can't just say: "private _top;" you have to say something like
"private int _top;"
 
N

Nicholas Paldino [.NET/C# MVP]

Additionally, your const statement should come before the type.
Finally, your array declaration should look like this:

private int[] _arr;
 
G

Guest

You have

private _top;

You seem to have forgotten to declare what type of field _top is. Try

private int _top;
 
R

Ravi Ambros Wallau

A variable must have a type, as far as I know :'-(

Instead of:
private _top;
Use:
private int _top;
 
C

Csharper

Apparently there were other errors in the example code I used. The
follow code works now:

using System;

namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
Stack stack = new Stack() ;
int i = 0 ;
for (; i<stack.getSize(); ++i)
{
stack.push(i+10);
Console.Write("Pushing [" + (i+10) + "]") ;
Console.WriteLine(" now Top becomes: " + stack.getTop()) ;
Console.WriteLine() ;
}

int j = 0 ;
for (; j<stack.getSize(); ++j)
{
Console.Write("Popping [" + j + "] of " + stack.getSize()) ;
Console.WriteLine("Content: " + stack.pop()) ;
}
}
}

}


using System;

namespace ConsoleApplication2
{
public class Stack
{
private int _top;
public const int maxSize = 10;
private int[] _arr = new int[maxSize];

public Stack()
{
_top = 0;

}

public int getTop()
{
return _top;
}

public int getSize()
{
return maxSize;
}

public void push (int val)
{
if (_top < maxSize)
{
_arr[_top] = val;
++_top;
}
else
{
}
}

public int pop()
{
if (_top > 0)
{
--_top;
return _arr[_top];
}
else
{
return 0;
}
}
}
}
 
B

Bruce Wood

Ouch. They really should test their code before they post it in an
article. That's embarrassing, especially when they build the program up
line by line, with the same error persisting throughout the article.
 

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