一个c++程序,不知道错在哪里,希望大家帮忙指出
这是我写的一个程序:#include<iostream.h>
const int MaxSize=10;
template <class Type>
class Stack {
public:
Stack()
{
cout<<"请输入10个整数."<<endl;
for(int i=0;i<10;i++)
cin>>stack[i];
cout<<endl;
}
void printstack(int n);
Boolean IsFull( );
void Add(const Type& item);
Boolean IsEmpty( );
Type* Delete( Type& );
private:
int top;
Type* stack;
int MaxSize;
template <class Type>
Stack<Type>::Stack(int MaxStackSize):MaxSize(MaxStackSize) {
stack = new Type[MaxSize];
top = -1;
}
template <class Type>
inline Boolean Stack<Type>::IsFull( ) {
if (top == MaxSize-1) return TRUE;
else return FALSE;
}
void Stack::printstack(int n){
for(int j=0;j<n;j++)
cout<<stack[j]<<"\t";
cout<<endl;
};
template <class Type>
inline Boolean Stack<Type>:: IsEmpty( ) {
if (top == -1) return TRUE;
else return FALSE;
}
template <class Type>
void Stack<Type>::Add(const Type& x) {
if (IsFull( )) StackFull( );
else stack[++top] = x;
}
template <class Type>
Type* Stack<Type>::Delete( Type& x) {
if (IsEmpty( )) { StackEmpty( ); return 0;};
x = stack[top--];
return &x;
}
int main()
{
Stack p;
p.printstack(10);
cout<<"请输入你要插入的数:"<<endl;
cin >> x;
p.Add(10,x);
p.printstack(11);
cout <<"删除栈顶元素"<<endl;
cin >> x;
p.Delete(11,x);
p.printstack(10);
return 0;
}
目的是表示一个栈和它的插入和删除操作,运行后提示错误:C:\Documents and Settings\pc\Cpp2.cpp(68) : fatal error C1075: end of file found before the left brace '{' at 'C:\Documents and Settings\pc\Cpp2.cpp(4)' was matched
Error executing cl.exe.
我自己真的无法找出错误,望各位指导一下,谢了
问题点数:100、回复次数:14Top
1 楼chenhu_doc(^0^纯一狼^0^ 看书看到大笑,直到不能自已)回复于 2006-06-04 21:25:28 得分 0
难道学了template,程序代码就可以写得怎么紧?Top
2 楼DogFriend(犬友)回复于 2006-06-04 21:46:32 得分 0
错误的地方比较多。
Stack的定义体内怎么那样钻进了3个格式不全的函数?
Stack既然是个模板,那么Stack p;这个定义就是错误的,应该Stack<类型> p;
Add()和Delete()函数的参数,定义时是1个参数,怎么使用时就是两个参数?
StackFull()和StackEmpty()的定义在哪里?
cin >> x; x没有定义。
还有,写个析构函数吧。Top
3 楼cnhgj(戏子) (没时间练太极)回复于 2006-06-05 00:49:37 得分 100
#include <iostream>
using namespace std;
const int MaxSize=10;
template <class Type>
class Stack
{
public:
Stack(int MaxStackSize);
~Stack();
void printstack(int n);
inline bool IsFull();
void Add(const Type &x);
inline bool IsEmpty();
Type* Delete( Type &x);
private:
int top;
Type* stack;
int MaxSize;
};
template<class Type>
Stack<Type>::Stack(int MaxStackSize) : MaxSize(MaxStackSize)
{
stack = new Type[MaxSize];
cout<<"请输入10个整数."<<endl;
for(int i=0;i<MaxSize;i++)
cin>>stack[i];
cout<<endl;
top = -1;
}
template<class Type>
void Stack<Type>::printstack(int n)
{
for(int j=0;j<n;j++)
cout<<stack[j]<<"\t";
cout<<endl;
}
template<class Type>
bool Stack<Type>::IsFull()
{
if (top == MaxSize-1)
return true;
else
return false;
}
template<class Type>
void Stack<Type>::Add(const Type &x)
{
if (IsFull())
cout<<"full"<<endl;
else
stack[++top] = x;
}
template<class Type>
bool Stack<Type>::IsEmpty()
{
if (top == -1)
return true;
else
return false;
}
template<class Type>
Type *Stack<Type>::Delete(Type &x)
{
if (IsEmpty())
{
cout<<"empty"<<endl;
return 0;
};
x = stack[top--];
return &x;
}
template<class Type>
Stack<Type>::~Stack()
{
delete[] stack;
}
int main()
{
Stack<int> p(10);
p.printstack(10);
cout<<"请输入你要插入的数:"<<endl;
int x;
cin >> x;
p.Add(x);
p.printstack(11);
cout <<"删除栈顶元素"<<endl;
cin >> x;
p.Delete(x);
p.printstack(10);
return 0;
}Top
4 楼cnhgj(戏子) (没时间练太极)回复于 2006-06-05 00:50:55 得分 0
楼主这个程序好多错误。。以上是改正的。。Top
5 楼MagicCarmack(MagiC++)回复于 2006-06-05 11:08:24 得分 0
楼主的程序连类在哪里结束自己都没搞清楚?
函数后面居然来分号这些错误不知是怎么回事
全都是基础错误Top
6 楼flyswift(溺水鱼)回复于 2006-06-05 11:09:48 得分 0
晕,大括号后面接分号.Top
7 楼vivawands(守护蓉蓉)回复于 2006-06-05 12:35:25 得分 0
谢谢Top
8 楼vivawands(守护蓉蓉)回复于 2006-06-05 12:48:27 得分 0
我是新手,水平确实很差,很多基础都还没掌握,虽然上面的程序还有很多不懂,不过还是很很很感谢楼上的各位的指导。Top
9 楼templarzq(原谅我这一生不羁放纵爱自由,也会怕有一天会跌倒)回复于 2006-06-05 17:03:44 得分 0
从编译器信息看是大括号不匹配。。。好像是类的定义那里?Top
10 楼pottichu(拉拉是头猪)回复于 2006-06-05 21:14:30 得分 0
楼猪的代码可以拿去当面试题,让人改错。嘿嘿。Top
11 楼leebuilder(建筑者)回复于 2006-06-05 21:29:32 得分 0
不是都给你错误号了么,查msdn啊
Visual C++ 概念:生成 C/C++ 程序
致命错误 C1075
与左侧的 token(位于“filename(linenumber)”)匹配之前遇到文件结束
编译器在到达文件尾之前需要匹配的 token。
可能的原因
含有不匹配的括号、大括号或其他成对的字符。Top
12 楼rabbit729(无名胜有名)回复于 2006-06-05 22:01:05 得分 0
没看你的代码,我刚学C++时,也经常遇到这个错误,这个错误经常是在定义类时的大括号后少分号,或者是在.cPP文件前面缺少#include "stdafx.h"文件,楼主试一下吧。Top
13 楼bristy(一踏糊涂)回复于 2006-06-06 11:20:37 得分 0
类的定义后面要加分号;
内联函数里最好不要有循环;
如果是刚开始学C++,还有数据结构,用c语言的数据结构不要用C++的,一大片的TEMPLATE烦都烦死人;
养成良好的编程习惯Top
14 楼langzi8818(┤天道酬勤┝爱老婆┦┷我是来学习滴┷)回复于 2006-06-06 14:47:51 得分 0
类的定义后面要加分号;
内联函数里最好不要有循环;
如果是刚开始学C++,还有数据结构,用c语言的数据结构不要用C++的,一大片的TEMPLATE烦都烦死人;
养成良好的编程习惯
Top




