0

我在 Eclipse 中编写一个 C++ 应用程序,在编写一些代码时,我在 ch_type.cpp 的第 18、27、41 和 81 行发现了几个错误。这是我项目的当前代码:

ch_type.h

/********************************************************
 * char_type -- Character type class            *
 *                          *
 * Member functions:                    *
 *  type -- returns the type of a character     *
 *      (Limited to simple types)       *
 *  is(ch, char_type) -- check to see if ch is  *
 *      a member of the given type.     *
 *      (Works for derrived types as well.) *
 *******************************************************/
class char_type {
    public:
        enum CHAR_TYPE {
            C_EOF,      // End of file character
            C_WHITE,    // Whitespace or control character
            C_NEWLINE,  // A Newline character
            C_ALPHA,    // A Letter (includes _)
            C_DIGIT,    // A number
            C_OPERATOR, // Random operator
            C_SLASH,    // The character '/'
            C_L_PAREN,  // The character '('
            C_R_PAREN,  // The character ')'
            C_L_CURLY,  // The character '{'
            C_R_CURLY,  // The character '}'
            C_SINGLE,   // The character '\''
            C_DOUBLE,   // The character '"'
            // End of simple types, more complex, derrived types follow
            C_HEX_DIGIT,    // Hexidecimal digit
            C_ALPHA_NUMERIC // Alpha numeric
        };
    private:
        static enum CHAR_TYPE  type_info[256];      // information of each character

        // Fill in a range of type info stuff
        void fill_range(int start, int end, CHAR_TYPE type);
    public:
        char_type();    // Initialize the data
            //~char_type -- default destructor

            // Returns true if character is a given type
            int is(int ch, CHAR_TYPE kind);

            CHAR_TYPE type(int ch);
        }

ch_type.cpp

/********************************************************
 * ch-type package                  *
 *                          *
 * The class ch_type is used to tell the type of    *
 * various characters                   *
 *                          *
 * The main number functions are:           *
 *  is -- True if the character is the indicated    *
 *      type.                   *
 *  type -- Return type of character.       *
 *******************************************************/
#include <iostream>
#include <assert.h>

#include "ch_type.h"

// Define the type information array
char_type::CHAR_TYPE char_type::type_info[256];
/********************************************************
 * fill_range -- fill in a range of types for the   *
 *  character type class                *
 *                          *
 * Parameters                       *
 *  start, end -- range of items to fill in     *
 *  type -- type to use for filling         *
 *******************************************************/
void char_type::fill_range(int start, int end, CHAR_TYPE type)
{
    int cur_ch;

    for (cur_ch = start; cur_ch <= end; ++cur_ch) {
        assert(cur_ch >= 0);
        assert(cur_ch < sizeof(type_info)/sizeof(type_info[0]));
        type_info[cur_ch] = type;
    }
}

/*********************************************************
 * char_type::char_type -- initialize the char type table*
 ********************************************************/
char_type::char_type()
{
    fill_range(0, 255, C_WHITE);

    fill_range('A', 'Z', C_ALPHA);
    fill_range('a', 'z', C_ALPHA);
    type_info['_'] = C_ALPHA;

    type_info['!'] = C_OPERATOR;
    type_info['#'] = C_OPERATOR;
    type_info['$'] = C_OPERATOR;
    type_info['%'] = C_OPERATOR;
    type_info['^'] = C_OPERATOR;
    type_info['&'] = C_OPERATOR;
    type_info['*'] = C_OPERATOR;
    type_info['-'] = C_OPERATOR;
    type_info['+'] = C_OPERATOR;
    type_info['='] = C_OPERATOR;
    type_info['|'] = C_OPERATOR;
    type_info['~'] = C_OPERATOR;
    type_info[','] = C_OPERATOR;
    type_info[':'] = C_OPERATOR;
    type_info['?'] = C_OPERATOR;
    type_info['.'] = C_OPERATOR;
    type_info['<'] = C_OPERATOR;
    type_info['>'] = C_OPERATOR;

    type_info['/'] = C_SLASH;
    type_info['\n'] = C_NEWLINE;

    type_info['('] = C_L_PAREN;
    type_info[')'] = C_R_PAREN;

    type_info['{'] = C_L_CURLY;
    type_info['}'] = C_R_CURLY;

    type_info['"'] = C_DOUBLE;
    type_info['\''] = C_SINGLE;
}

int char_type::is(int ch, CHAR_TYPE kind)
{
    if (ch == EOF) return (kind == C_EOF);

    switch (kind) {
        case C_HEX_DIGIT:

            assert(ch >= 0);
            assert(ch < sizeof(type_info)/sizeof(type_info[0]));

            if (type_info[ch] == C_DIGIT)
                return (1);

            if ((ch >= 'A') && (ch <= 'F'))
                return (1);
            if ((ch >= 'a') && (ch <= 'f'))
                return (1);
            return (0);
        case C_ALPHA_NUMERIC:
            assert(ch >= 0);
            assert(ch < sizeof(type_info)/sizeof(type_info[0]));

            return ((type_info[ch] == C_ALPHA) ||
                (type_info[ch] == C_DIGIT));
        default:
            assert(ch >= 0);
            assert(ch < sizeof(type_info)/sizeof(type_info[0]));

            return (type_info[ch] == kind);
        }
};

我做错了什么?我该如何修复这些错误?

这是我的错误信息:

说明 资源路径 位置 类型

未找到成员声明 ch_type.cpp /stats 第 18 行语义错误

4

1 回答 1

4

首先,在类定义之后,您必须插入;

class char_type {
    ...
}; //HERE

其次,要使用宏EOF,您必须包括<stdio.h>

于 2013-02-06T01:20:24.757 回答