2014-02-21 50 views
-1

當我嘗試創建這個類時,我總是收到這些錯誤,我不確定它的含義。我認爲這是做到這一點的正確方法,但我不知道,因爲即時通訊仍然是新的C++。在C++之前預期的主表達式':'令牌

expected primary-expression before ‘:’ token 
expected ‘;’ before ‘:’ token  

這裏是頭文件:

#ifndef LEAKY_STACK_A_H 
#define LEAKY_STACK_A_H 
#include <string> 
#include "LeakyStack.h" 
using std::string; 

class LeakyStackA : public LeakyStack { 

public: 
    /** 
    * Constructor with specified max capacity 
    * \param the maximum capacity (default: 10) 
    */ 
    LeakyStackA(int cap=DEF_CAPACITY); 

    /** 
    * Return the number of objects in the stack. 
    * \return number of elements 
    */ 
    int size() const; 

    /** 
    * Determine if the stack is currently empty. 
    * \return true if empty, false otherwise. 
    */ 
    bool empty() const; 

    /** 
    * Return a const reference to the top object in the stack. 
    * \return const reference to top element 
    * \throw runtime_error if the stack is empty 
    */ 
    const std::string& top() const; 

    /** 
    * Insert an object at the top of the stack. If the stack 
    * is already at capacity, the oldest element will be lost. 
    * \param the new element 
    */ 
    void push(const std::string& e); 

    /** 
    * Remove the top object from the stack. 
    * \throw runtime_error if the stack is empty. 
    */ 
    void pop(); 

private: 
    enum { DEF_CAPACITY = 10 };      // default stack capacity 


    string* S; 
    int capacity; 
    int t; 
    int n; 
    int k; 

}; 

#endif 

這裏是.cpp文件:

#include <stdexcept> 
#include <iostream> 
#include "LeakyStack.h" 
#include "LeakyStackA.h" 
using namespace std; 

/** 
* Constructor with specified max capacity 
* \param the maximum capacity (default: 10) 
*/ 
LeakyStackA::LeakyStackA (int cap) { 
    : S(new string[cap]), capacity(cap), t(-1); 

} 
/** 
* Return the number of objects in the stack. 
* \return number of elements 
*/ 
int LeakyStackA::size() const { 
    return (t+1);  
} 


/** 
* Determine if the stack is currently empty. 
* \return true if empty, false otherwise. 
*/ 
bool LeakyStackA::empty() const { 
    return (t < 0); 
} 


/** 
* Return a const reference to the top object in the stack. 
* \return const reference to top element 
* \throw runtime_error if the stack is empty 
*/ 
const string& LeakyStackA::top() const { 
    if (empty()) throw runtime_error("Stack is Empty"); 
    return S[t]; 
} 


/** 
* Insert an object at the top of the stack. If the stack 
* is already at capacity, the oldest element will be lost. 
* \param the new element 
*/ 
void LeakyStackA::push(const string& e) { 
    if (size() == capacity) { 
    S[t--]; 
    S[t++] = e; 
    } 
    else { 
     S[t++] = e; 
    } 
    //if (size() == capacity) throw runtime_error("Stack is Full"); 
    //S[++t] = e; 
} 

/** 
* Remove the top object from the stack. 
* \throw runtime_error if the stack is empty. 
*/ 
void LeakyStackA::pop() { 
    if(empty()) throw runtime_error("Stack is Empty"); 
    --t; 

} 

任何幫助,將不勝感激感謝。

+0

請指出發生編譯錯誤的行 – Brian

+3

這應該是每個基本教科書的一部分。如果它不在你的身上,請把它扔掉,然後換一個更好的。 –

+2

請注意,這是*方式*更多的代碼比必要證明你的問題。 – chris

回答

5

的初始化列表構造函數定義的一部分,並且這樣寫的:

LeakyStackA::LeakyStackA (int cap) 
: S(new string[cap]), capacity(cap), t(-1) 
{ } 
+0

非常感謝您的幫助! – MikesBoys1

1

這裏的問題是

LeakyStackA::LeakyStackA (int cap) { 
    : S(new string[cap]), capacity(cap), t(-1); 

} 

它應該是這樣的:

LeakyStackA::LeakyStackA (int cap) 
    : S(new string[cap]), capacity(cap), t(-1) {} 
+1

一分鐘來不及... –

+0

謝謝你的幫助! – MikesBoys1

0

這不是對問題的直接回答,而是一般編譯器錯誤「預期的初級表達式之前」:'代幣「

在我的情況下,我輸入了std:string而不是std::string

我在這裏分享這是因爲這是我在StackOverflow上發現的第一個此特定錯誤的發生,而其他人可能會遇到同樣的問題。

相關問題