error: identifier "byte" is undefined.

This is a discussion on error: identifier "byte" is undefined. within the C++ Programming forums, part of the General Programming Boards category; A friend gave me some code to add to my project but I fail to compile it, probably because of ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    error: identifier "byte" is undefined.

    A friend gave me some code to add to my project but I fail to compile it, probably because of the definition of byte(or BYTE, I don't know) as a variable. Here is the code that won't compile, and below are the errors from the compiler.

    parser.cpp
    Code:
    //-----------------------------------------------------------------------------
    // Parser
    //-----------------------------------------------------------------------------
    
    #include "parser.h"
    
    char		Parser::token[MAXTOKEN];			// buffer for tokens
    int			Parser::scriptline;
    const byte *Parser::buffer;
    const byte *Parser::buf_start;
    const byte *Parser::buf_end;
    
    
    void Parser::StartParseBuffer(const byte *buff, const int size)
    {
    	buf_start = buffer = buff;
    	buf_end = buff + size;
    	scriptline = 1;
    }
    
    void Parser::StartParseString(const char *string)
    {
    	buf_start = buffer = (byte *) string;
    	buf_end = (byte *) string + strlen(string);
    	scriptline = 1;
    }
    
    // if crossline is false, GetToken returns false when a line break is found
    // GetToken(true) returns 0 only if end of file
    // GetToken(false) returns 0 if line break or end of file
    bool Parser::GetToken(bool crossline)
    {
    	char *token_p;
    
    	if (!buffer) return 0;
    	if (buffer == buf_end) return 0;
    
    	*token = 0;	// init to zero
    
    	// skip space
    skipspace:
    	while (*buffer <= 32 || *buffer == '\n')
    	{
    		// no se si \n, \t y \r son menores que 32, pero supongo que sí
    		if (*buffer == '\n') ++scriptline;
    		if (buffer >= buf_end)
    		{
    			if (!crossline)
    			{
    				//gConsole->InsertLine("Line %i is incomplete", scriptline);
    			}
    			return 0;
    		}
    
    		if (!crossline)
    		{
    			if (*buffer == '\n')
    			{
    				--scriptline;
    				return 0;
    			}
    		}
    		++buffer;
    	}
    
    	if (buffer >= buf_end) return 0;
    
    	// # // comments
    	if (*buffer == '#' ||
    		(buffer[0] == '/' && buffer[1] == '/'))
    	{
    		while (*buffer++ != '\n')
    		{
    			if (buffer >= buf_end) return 0;
    		}
    		++scriptline;
    		if (!crossline) return 0;
    		goto skipspace;
    	}
    
    	// /* */ comments
    	if (buffer[0] == '/' && buffer[1] == '*')
    	{
    		buffer += 2;
    		while (buffer[0] != '*' &&
    			buffer[1] != '/')
    		{
    			++buffer;
    			if (buffer >= buf_end)
    			{
    				//print error here
    				return 0;
    			}
    			if (*buffer == '\n') ++scriptline;
    		}
    		buffer += 2;
    		goto skipspace;
    	}
    
    	// copy token
    	token_p = token;
    
    	if (*buffer == '"')
    	{
    		// quoted token
    		++buffer;
    		while (*buffer != '"')
    		{
    			*token_p++ = *buffer++;
    			if (buffer == buf_end) break;
    			if (token_p == &token[MAXTOKEN])
    			{
    				//Print Error here.
    			}
    		}
    		++buffer;
    	}
    	else
    	{
    		// regular token
    		if (*buffer == '{' ||
    			*buffer == '}')
    		{
    			*token_p++ = *buffer++;
    			*token_p = 0;
    			return 1;
    		}
    		while (*buffer > 32 &&
    			*buffer != '\n' &&
    			*buffer !='{' &&
    			*buffer !='}')
    		{
    			*token_p++ = *buffer++;
    			if (buffer == buf_end) break;
    			if (token_p == &token[MAXTOKEN])
    			{
    				//Print error here.
    			}
    		}
    	}
    
    	*token_p = 0;
    
    	return 1;
    }
    
    int Parser::GetOffset(void)
    {
    	return (int) (buffer - buf_start);
    }
    
    void Parser::GoTo(const int offset)
    {
    	buffer = buf_start + offset;
    
    }
    parser.h
    Code:
    //-----------------------------------------------------------------------------
    // Parser
    //-----------------------------------------------------------------------------
    
    #ifndef __PARSER_H__
    #define __PARSER_H__
    
    //#include "files.h"
    
    #define MAXTOKEN			1024
    
    /**
    * Parser class.
    * The parser class provides methods to parse files. This is specially used
    * for shaders parsing.
    */
    class Parser
    {
    public:
    	static void StartParseBuffer(const byte *buffer, const int size);
    	static void StartParseString(const char *string);
    
    	static bool GetToken(bool crossline);
    	static int GetOffset(void);
    	static void GoTo(const int offset);
    
    	static char token[MAXTOKEN];				/**< buffer for tokens */
    	static int scriptline;
    
    protected:
    
    	static const byte *buffer;
    	static const byte *buf_start;
    	static const byte *buf_end;
    };
    
    #endif	/* __PARSER_H__ */
    ERRORS
    Code:
    Compiling...
    parser.cpp
    Parser\parser.h(20): error: identifier "byte" is undefined
       static void StartParseBuffer(const byte *buffer, const int size);
                                          ^
    Parser\parser.h(32): error: identifier "byte" is undefined
       static const byte *buffer;
                    ^
    Parser\parser.h(33): error: identifier "byte" is undefined
       static const byte *buf_start;
                    ^
    Parser\parser.h(34): error: identifier "byte" is undefined
       static const byte *buf_end;
                    ^
    Parser\parser.cpp(9): error: identifier "byte" is undefined
      const byte *Parser::buffer;
            ^
    Parser\parser.cpp(10): error: identifier "byte" is undefined
      const byte *Parser::buf_start;
            ^
    Parser\parser.cpp(11): error: identifier "byte" is undefined
      const byte *Parser::buf_end;
            ^
    Parser\parser.cpp(14): error: identifier "byte" is undefined
      void Parser::StartParseBuffer(const byte *buff, const int size)
                                          ^
    Parser\parser.cpp(23): error: identifier "byte" is undefined
       buf_start = buffer = (byte *) string;
                             ^
    Parser\parser.cpp(23): error: expected an expression
       buf_start = buffer = (byte *) string;
                                   ^
    Parser\parser.cpp(23): error: expected a ";"
       buf_start = buffer = (byte *) string;
                                     ^
    Parser\parser.cpp(24): error: expected an expression
       buf_end = (byte *) string + strlen(string);
                        ^
    Parser\parser.cpp(24): error: expected a ";"
       buf_end = (byte *) string + strlen(string);
                          ^
    compilation aborted for Parser\parser.cpp (code 2)
    PS: If you find any other mistake or anything that needs to be changed(including a way to get rid of the goto) please let me know.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Your friend gave you some pretty crappy code. It sounds like you already understand that goto should not have been used. Likewise, using a #define when a const definition could've been used is also wrong. I looked over the code, and I couldn't find a definition of byte. So, no surprise, byte is undefined. Your friend might have forgotten to give you another library that contains the definition of byte. If I were you, though, I'd write the code myself.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    change byte to unsigned char and you'll be fine.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Yeah, I think unsigned char should fix it, what puzzle me is the fact that byte is shown on Visual Studio as a keyword just like int, char or any other kind of variable.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Hulag
    Yeah, I think unsigned char should fix it, what puzzle me is the fact that byte is shown on Visual Studio as a keyword just like int, char or any other kind of variable.
    On VS .NET? byte will show like other keywords because the IDE is apparently not smart enough to only highlight keywords in the language you're using. byte is a keyword in C#, but not in C++. You're probably missing a typedef (or, given the nature of that code, a #define).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21