2014-02-21 10 views
-1

Я продолжаю получать эти ошибки, пытаясь сделать этот класс, и im не совсем уверен, что это значит. Я думал, что это правильный способ сделать это, но я не уверен, потому что я все еще новичок в 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, и другие люди могут иметь одинаковую проблему.

Смежные вопросы