| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2925 人关注过本帖
标题:请问结构体的this怎么用??
取消只看楼主 加入收藏
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
请问结构体的this怎么用??
struct token
{
    char type;
    char string[MAXTOKENLEN];
};
struct token stack[MAXTOKENS];
struct token this;
为什么我这样写会有错?我是照着书上写的,功能是编译器的声明解析(部分)(也就是声明void fun()它会解析出来"fun是一个返回值为缺省的函数")。后面好多地方用到this,但是编译时就在这儿卡壳了。DEV-C++:expected unqualified-id before "this" ,vc++6.0:syntax error : missing ';' before 'this' 两次。后面会用到this.string或者this.string[i]还有this.type,可this声明出错了,程序就没法执行。请各位帮我看看是不是定义出错了,分不多,如果找不出原因的话我可以把源代码贴出来(好辛苦抄下来的),谢谢了!
搜索更多相关主题的帖子: 声明 expected before 
2011-09-17 00:59
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
回复 楼主 stophin
书上就是这样的~~没有写错,果然尽信书不如无书啊
2011-09-17 22:50
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
回复 2楼 pauljames
是一本大师编程的书吧,this指明最后一次获得的字符
2011-09-17 22:52
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
回复 3楼 nextleave
我也在网上找了很多资料,都是在struct的花括号里面使用的,所以不懂,才会来这儿问
2011-09-17 22:53
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
回复 4楼 laoyang103
??只说一半啊?
2011-09-17 22:55
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
回复 5楼 Sliverwang
是一本叫什么大师编程的书(具体的我忘了),外国人写的。
——————————————————————
回复到5楼我才发现自己尽然点错回复了!新人发帖失误啊失误...大家将就着看吧,除5楼外回复都在楼上..
2011-09-17 23:01
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
谢谢楼上各位的回复,今天我把所有this去掉了s变成thi后编译成功并且运行成功。一个类似于自动机的程序,可以成为编译器的一部分,附上源代码,,额额额~~~另外还请各位高人看看这个this(也就是thi)究竟是做什么的?
程序代码:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string>
using namespace std;
#define MAXTOKENS 100
#define MAXTOKENLEN 64
enum type_tag { IDENTIFIER, QUALIFIER, TYPE };
struct token
{
    char type;
    char string[MAXTOKENLEN];
};
int top = -1;
struct token stack[MAXTOKENS];
struct token thi;
#define pop stack[top--]
#define push(s) stack[++top] = s
enum type_tag classify_string (void)
/*推断标识符类型*/
{
    char *s = thi.string;
    if (!strcmp (s, "const"))
    {
        strcpy (s, "read_only");
        return QUALIFIER;
    }
    if (!strcmp (s, "volatile")) return QUALIFIER;
    if (!strcmp (s, "void")) return TYPE;
    if (!strcmp (s, "char")) return TYPE;
    if (!strcmp (s, "signed")) return TYPE;
    if (!strcmp (s, "unsigned")) return TYPE;
    if (!strcmp (s, "short")) return TYPE;
    if (!strcmp (s, "int")) return TYPE;
    if (!strcmp (s, "long")) return TYPE;
    if (!strcmp (s, "float")) return TYPE;
    if (!strcmp (s, "double")) return TYPE;
    if (!strcmp (s, "struct")) return TYPE;
    if (!strcmp (s, "union")) return TYPE;
    if (!strcmp (s, "enum")) return TYPE;
    return IDENTIFIER;
}
void gettoken (void)
{
    char *p = thi.string;
    /*略过空白字符*/
    while ((*p = getchar()) == ' ');

    if (isalnum(*p))
    {
          /*读入的标识符以A-Z、0-9开头*/
          while (isalnum(*++p = getchar()));
          ungetc (*p, stdin);
          *p = '\0';
          thi.type = classify_string ();
          return;
    }
    if (*p == '*')
    {
        strcpy (thi.string, "pointer to ");
        thi.type = '*';
        return;
    }
    thi.string[1] = '\0';
    thi.type = *p;
    return;
}
/*理解所有分析过程的代码段*/
void read_to_first_identifer ()
{
    gettoken();
    while (thi.type != IDENTIFIER)
    {
        push(thi);
        gettoken();
    }
    printf("%s is ", thi.string);
    gettoken();
}
void deal_with_arrays ()
{
    while (thi.type == '[')
    {
        printf("array ");
        gettoken();/*数字或']'*/
        if (isdigit (thi.string[0]))
        {
            printf("0..%d ", atoi(thi.string)-1);
            gettoken();/*读取']'*/
        }
        gettoken();/*读取']'之后的再一个标记*/
        printf("of ");
    }
}
void deal_with_function_args ()
{
    while (thi.type != ')')
    {
        gettoken();
    }
    gettoken();
    printf("function returning ");
}
void deal_with_pointers ()
{
    while (stack[top].type == '*')
    {
        printf("%s ",pop.string);
    }
}
void deal_with_declarator()
{
    /*处理标识符之后可能存在的数组或函数*/
    switch(thi.type)
    {
        case '[':
            deal_with_arrays();
            break;
        case '(':
            deal_with_function_args();
            break;
    }
    deal_with_pointers();
    /*处理在读入到标识符之前压入到堆栈中的符号*/
    while(top >= 0)
    {
        if (stack[top].type == '(')
        {
            pop;
            gettoken();/*读取')'之后的符号*/
            deal_with_declarator();
        }
        else
        {
            printf("%s ",pop.string);
        }
    }
}
int main()
{
    /*将标记压入到堆栈中,直到遇见标识符*/
    read_to_first_identifer();
    deal_with_declarator();
    printf("\n");
    getch();
    return 0;
}

 

[ 本帖最后由 stophin 于 2011-9-17 23:17 编辑 ]
2011-09-17 23:12
快速回复:请问结构体的this怎么用??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020041 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved