简体   繁体   English

C ++中':'标记之前的预期主表达式

[英]expected primary-expression before ‘:’ token in c++

I keep getting these errors when trying to make this class and im not really sure what it means. 尝试上此类时,我一直收到这些错误,但我不确定这是什么意思。 I thought this was the correct way to do this but im not sure because im still new to c++. 我认为这是执行此操作的正确方法,但我不确定,因为即时消息对c ++来说仍然很新。

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

Here is the header file: 这是头文件:

#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

And here is the .cpp file: 这是.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;

}

any help would be appreciated thanks. 任何帮助,将不胜感激谢谢。

The initializer list is part of the constructor definition, and written like this: 初始化程序列表是构造函数定义的一部分,其编写方式如下:

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

Here is the problem 这是问题所在

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

}

it should be like this: 应该是这样的:

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

This is not a direct answer to the question but to the general compiler error "expected primary-expression before ':' token" 这不是问题的直接答案,而是一般编译器错误“':'标记之前的预期主表达式”

In my case, I had typed std:string instead of std::string . 就我而言,我输入的是std:string而不是std::string

I share this here because it is the first occurrence of this specific error I found on StackOverflow and other people might have the same problem. 我在这里分享此信息是因为这是我在StackOverflow上发现的此特定错误的第一次出现,其他人可能也有相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM