2014-02-21 54 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上发现的第一个此特定错误的发生,而其他人可能会遇到同样的问题。

相关问题