C ++ NetBeans错误:'int'之前的预期unqualified-id

时间:2014-02-09 23:30:55

标签: c++ netbeans

我是c ++的新手,我收到错误,我不确定为什么有人可以帮我解决这个问题?提前谢谢。

这是我的头文件。

#ifndef SSTACK_H
#define SSTACK_H

#include <cstdlib>
#include <string>

class sstack {
    public:
            // Constructor
            sstack( int cap);
            // Copy Constructor
            sstack( const sstack& s );
           ~sstack( );
            void push ( const std::string&  s);
            std::string& pop ();
            std::string&  top () const;
            bool IsEmpty () const;
            int size() const;
    int getCapacity() const;

    // NONMEMBER FUNCTIONS for the bag class
    // Precondition:  s1.size( ) + s2.size( ) <= s1.Capacity.
    // Postcondition: The stack returned is the union of s1 and s2.
    sstack operator +(const sstack& s2);

    private:
            int Capacity; // Capacity is the maximum number of items that a stack can hold
            std::string* DynamicStack; 
            int used; // How many items are stored in the stack
};

#endif

这里是sstack的.cpp文件我的错误在这个类中 我的第一个错误是:

sstack.cpp:14:24:错误:预期在'int'之前的unqualified-id

sstack.cpp:14:24:错误:预期')'在'int'之前

我的第二个错误是:

sstack.cpp:19:24:错误:在'const'之前预期的unqualified-id

sstack.cpp:19:24:错误:预期')'在'const'之前 我在网上看了看,似乎无法弄清楚问题是什么?

顺便说一句,就像我之前说过的,我是c ++的新手,所以如果还有其他任何看起来不好或者错误的事情或者可以做得更好,请告诉我,这样我就可以学习谢谢了

#include "sstack.h"




            // Constructor

            //ERROR HERE 
            sstack(int cap){

                test = new std::string [cap];

                Capacity = cap;
            }

            // Copy Constructor

               //ERROR 2 HERE
              sstack(const sstack& s){

                test = new std::string[1000];

                  for(int i = s.size()-1; i > 0; i--){
                        test[i] = *s.pop();
                    }//end of for

                 Capacity = s.getCapacity();
                 used = s.size();      
            }

            ~sstack(){
                delete []test;
            }

            void push ( const std::string&  s){
                test[used] = *s;
                used++;
            }

            std::string& pop (){
                used-= 1;
                popped =  test[used];
                test[used] = "";
              return *popped;

            }

            std::string&  top () const{
                top = test[used--];
                return *top;
            }

            bool IsEmpty () const{
                if(used <= 0){
                    return true;
                }else{
                    return false;
                }
            }

            int size() const{
                return used;
            }

    int getCapacity() const{
                return Capacity;
            }

    // NONMEMBER FUNCTIONS for the bag class
    // Precondition:  s1.size( ) + s2.size( ) <= s1.Capacity.
    // Postcondition: The stack returned is the union of s1 and s2.
    sstack operator +(const sstack& s2){
                int amount = used;
                if(amount + s2.size() <= Capacity){
                    for(int i = used + s2.size()-1; i > used; i--){
                        test[i] = *s2.pop();
                        used++;
                    }//end of for
                }//end of if
            }


            int Capacity = 1000; // Capacity is the maximum number of items that a stack can hold
            std::string* DynamicStack; 
            int used = 0; // How many items are stored in the stack
            std::string test[1000];
            std::string popped;
            std::string top;

1 个答案:

答案 0 :(得分:2)

您在成员定义中缺少类范围:

sstack::sstack(int cap) { .... }
^^^^^^^^

void sstack::push ( const std::string&  s) { .... }
     ^^^^^^^^