尝试制作一堆字符串.出现缺少类型说明符错误
Trying to make a stack of strings. Getting missing type specifier error
所以我尝试在错误来源中添加一个int,但这只会增加大量新错误。我已经用谷歌搜索了一下,但我没有找到任何对我有帮助的东西,或者我只是不明白所说的很多内容。
#include <iostream>
#include <string>
using namespace std;
//Define the stack class, set default stack size to 7
//use a template to define type at later point
template<class T,int size=7>
class stack
{
private: T data[size];
int stack_ptr;
public:
stack(void);
void push(T x);
T pop();
T top();
};
//constructor function to initialize stack and data
template<class T, int size>
stack<T, size>::stack(void)
{
int i;
for(i=0;i<size;i++) data[i]=0;
stack_ptr=0;
}
//Push data onto stack
template<class T,int size>
void stack<T, size>::push(T x)
{
if(stack_ptr>=size)
{
cout<<"cannot push data: stack full"<<endl;
return;
}
data[stack_ptr++]=x;
cout<<"Pushedt" << x << "tonto the stack"<<endl;
return;
}
//Pop data from stack
template<class T, int size>
stack<T, size>::pop()
{
using namespace std;
if(stack_ptr<=0)
{
cout<<"cannot pop data: stack empty"<<endl;
return data[0];
}
cout<<"poppedt"<< data[--stack_ptr]<< "tfrom stack"<<endl;
return data[stack_ptr];
}
int main()
{
//declaring stack of strings and using default size
stack<string> c;
string w;
string name1 = "Rich";
string name2 = "Debbie";
string name3 = "Robin";
string name4 = "Dustin";
string name5 = "Philip";
string name6 = "Jane";
string name7 = "Joseph";
c.push(name1);
c.push(name2);
c.push(name3);
c.push(name4);
c.push(name5);
c.push(name6);
c.push(name7);
//pick up the stack value
w=c.pop();
string & i = c.top( );
string next_top = c.top();
cout << "The top person the stack is " << next_top << "."<< endl;
return 0;
}
这是我的错误
1>------ Build started: Project: A-2, Configuration: Debug Win32 ------
1> A-2.cpp
1>h:pf3a-2a-2a-2.cpp(55): error C4430: missing type specifier-int assumed.
Note: C++ does not support default-int
我不确定我还能做什么,任何帮助都值得赞赏。
您在pop
的实现中缺少返回类型。看起来您打算从声明中T
返回类型:
template<class T, int size>
T stack<T, size>::pop()
{
// ...
}
存在一些问题:
- (如 sftrabbit 所示):缺少 T 作为流行音乐的返回类型规范。
-
string & i = c.top( );
无效:引用临时 -
for(i=0;i<size;i++) data[i]=0
不适用于除 int 类型以外的T
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误