1

そのため、エラーの発生元に 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

他に何ができるかわからないので、助けていただければ幸いです。

4

2 に答える 2

3

の実装に戻り値の型がありませんpop。戻り値の型がT宣言からのものであることを意図していたようです:

template<class T, int size>
T stack<T, size>::pop()
{
  // ...
}
于 2013-01-23T20:01:51.557 に答える
1

いくつかの問題があります:

  1. (sftrabbitが示したように):ポップのリターンタイプ仕様としてTがありません。
  2. string & i = c.top( );無効です:一時的なものへの参照
  3. for(i=0;i<size;i++) data[i]=0int-types以外では機能しませんT
于 2013-01-23T20:14:24.203 に答える