gpt4 book ai didi

c++ - 试图制作一堆字符串。缺少类型说明符错误

转载 作者:行者123 更新时间:2023-11-30 02:01:06 26 4
gpt4 key购买 nike

所以我尝试在错误来源处添加一个 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<<"Pushed\t" << 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<<"popped\t"<< 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:\pf3\a-2\a-2\a-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()
{
// ...
}

关于c++ - 试图制作一堆字符串。缺少类型说明符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com