游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3924|回复: 5

关于模板的一个问题,高手帮忙啊,呵呵

[复制链接]

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2009-3-27 13:45:00 | 显示全部楼层 |阅读模式
这是算法与数据结构中的一道题,比如输入(2+5)*5+5-7求它的值
错误如下:
1>------ Build started: Project: operator, Configuration: Debug Win32 ------
1>Compiling...
1>stack.cpp
1>e:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(25) : error C2632: 'short' followed by 'wchar_t' is illegal
1>e:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(25) : warning C4091: 'typedef ' : ignored on left of 'unsigned short' when no variable is declared
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(132) : error C2146: syntax error : missing ';' before identifier 'FILE'
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(132) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : error C2144: syntax error : 'int' should be preceded by ';'
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : error C2470: '_CRcharIMP' : looks like a function definition, but there is no parameter list; skipping apparent body
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : error C3094: 'vc_attributes:ost': anonymous usage not allowed
1>        e:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(102) : see declaration of 'vc_attributes::Post'
1>        attribute can only be applied to: 'formal parameter', 'return value'
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : fatal error C1075: end of file found before the left brace '{' at 'e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(32)' was matched
1>useoperator.cpp
1>e:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(25) : error C2632: 'short' followed by 'wchar_t' is illegal
1>e:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(25) : warning C4091: 'typedef ' : ignored on left of 'unsigned short' when no variable is declared
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(132) : error C2146: syntax error : missing ';' before identifier 'FILE'
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(132) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : error C2144: syntax error : 'int' should be preceded by ';'
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : error C2470: '_CRcharIMP' : looks like a function definition, but there is no parameter list; skipping apparent body
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : error C3094: 'vc_attributes::Post': anonymous usage not allowed
1>        e:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(102) : see declaration of 'vc_attributes::Post'
1>        attribute can only be applied to: 'formal parameter', 'return value'
1>e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(189) : fatal error C1075: end of file found before the left brace '{' at 'e:\program files\microsoft visual studio 9.0\vc\include\stdio.h(32)' was matched
1>Generating Code...
1>Build log was saved at "file://f:\operator\operator\operator\Debug\BuildLog.htm"
1>operator - 14 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2009-3-27 13:47:00 | 显示全部楼层

Re:关于模板的一个问题,高手帮忙啊,呵呵

// stack.h
#ifndef STACK_H_
#define STACK_H_
template <typename T>
class Stack
{
private:
         static const int MAXSIZE = 1024;
        T data[MAXSIZE];
        int top;
public:
        Stack ( ) {top = -1;}
        ~Stack ( ) {}
        void Push (const T & t);        //入栈       
        T Pop (void);                //出栈
        inline T & GetTop ()
        {
                if (top != -1)
                        return data[top];
                return T (0);
        }
};
#endif        // STACK_H_
// stack.cpp
//stack.cpp
#include "stack.h"
#include <iostream>
using namespace std;
template <typename T>
void Stack<T>:ush (const T & t)
{
        if (top == MAXSIZE - 1)
        {
          cout<<"Stack already full!"<<endl;
          exit (0);
        }
        data[++top] = t;
}
template <typename T>
T Stack<T>::Pop (void)
{
        if (top == -1)
        {
          cout<<"Stack already empty."<<endl;
          exit (0);
        }
        return data[top--];
}


// useoperator.cpp
// useoperator.cpp
#include "stack.h"
#include <iostream>
using namespace std;
template <typename T>
int  operate (int a, T  o, int b);
template <typename T>
T Precede (T  t, T  ch);
int main ( )
{
        Stack<char> optr ;
        Stack<int> opnd ;
        char ch;
        cin>>ch;
        while (!((ch == '#' && optr.GetTop ( ) == '#')))
        {
                if (ch >= '0' && ch <= '9')
                {
                        opnd.Push (ch-48);
                        cin>>ch;
                }
                else
                {
                        switch (Precede<char> (optr.GetTop ( ), ch))
                        {
                        case '<':
                                optr.Push (ch);
                                cin>>ch;
                                break;
                        case '=':
                                optr.Pop ( );
                                cin>>ch;
                                break;
                        case '>':
                                char o = optr.Pop ( );
                                int b = opnd.Pop ( );
                                int a = opnd.Pop ( );
                                opnd.Push (operate<char> (a, o, b));
                        }
                }
        }
        cout<<opnd.Pop ( );
        return 0;
}
template <typename T>
int operate (int a , T  o, int b)
{
        switch (o)
        {
        case '+':
                return a + b;
        case '-':
                return a - b;
        case '*':
                return a * b;
        case '/':
                return a / b;
        default:
                cout<<"operator error"<<endl;
                exit (0);
        }
}
template <typename T>
T Precede (T  t, T  ch)
{
        switch (t)
        {
        case '-':
        case '+':
                if (ch == '*' || ch == '/' || ch == '(')
                        return '<';
                else
                        return '>';
        case '*':
        case '/':
                if (ch == '(')
                        return '<';
                else
                        return '>';
        case '#':
        case '(':
                if (t == '(' && ch == ')')
                        return '=';
                else
                        return '<';
        case ')':
                return '>';
        }
        return (0);
}

       

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2009-3-27 14:25:00 | 显示全部楼层

Re:关于模板的一个问题,高手帮忙啊,呵呵

自己顶啊,呵呵

5

主题

68

帖子

90

积分

注册会员

Rank: 2

积分
90
QQ
发表于 2009-3-28 02:42:00 | 显示全部楼层

Re:关于模板的一个问题,高手帮忙啊,呵呵

你是程序不对,还是编译错误。。。问人说明白哦。呵呵

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2009-3-28 20:58:00 | 显示全部楼层

Re: Re:关于模板的一个问题,高手帮忙啊,呵呵

Timeshift: Re:关于模板的一个问题,高手帮忙啊,呵呵

你是程序不对,还是编译错误。。。问人说明白哦。呵呵

编译错误啊,呵呵,帮忙看一下吧

3

主题

14

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2009-4-2 15:06:00 | 显示全部楼层

Re:关于模板的一个问题,高手帮忙啊,呵呵

自己解决了,呵呵!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 15:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表