473,473 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Feb 9 '06 #1
9 2685
You haven't defined the type of _top. The compiler doesn't know what it
is.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ne****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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;
}
}
{}
}
}

Feb 9 '06 #2
A variable must have a type, as far as I know :'-(

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

<Ne****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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;
}
}
{}
}
}

Feb 9 '06 #3
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;"

Feb 9 '06 #4
Additionally, your const statement should come before the type.
Finally, your array declaration should look like this:

private int[] _arr;
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ne****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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;
}
}
{}
}
}

Feb 9 '06 #5
You have

private _top;

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

private int _top;


"Ne****@gmail.com" wrote:
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;
}
}
{}
}
}

Feb 9 '06 #6
Thanks, I new it was something simple. I got this example of the web
(http://www.c-sharpcorner.com/Languag...tionInCSDS.asp)
and thought I fool around with it. Too bad it had errors in it.

Feb 9 '06 #7
In fact...
"Csharper" <Ne****@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Thanks, I new it was something simple. I got this example of the web
(http://www.c-sharpcorner.com/Languag...tionInCSDS.asp)
and thought I fool around with it. Too bad it had errors in it.

Feb 9 '06 #8
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;
}
}
}
}

Feb 9 '06 #9
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.

Feb 9 '06 #10

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

Similar topics

10
by: Ian Lazarus | last post by:
Hello. How do "unresolved token" link errors occur. How do I fix them? Linking... LINK : error LNK2020: unresolved token (0A000015) ??_7type_info@@6B@ LINK : error LNK2020: unresolved token...
4
by: Eric Lilja | last post by:
Is this an invalid program? Doesn't compile on my system: #include <cstdio> class Why { enum TArch {LITTLE_ENDIAN, BIG_ENDIAN, NON_IEEE}; TArch Architecture; }; int
9
by: Sebouh | last post by:
I'm very close of shooting myself in the head right now. Why in hell does free(xx) give me an error: *** glibc detected *** free(): invalid pointer: 0x08ce6158 *** // The function returns...
4
by: Mamluk Caliph | last post by:
For quite some time I've used code as the following to be able to "cut&paste" parts from different headerfiles of the same name. It has worked with GCC, MS, Borland, Keil to name a few. In...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.