您好我在C ++中创建一个简单的堆栈类,并且是C ++的新手。我有一些错误,我无法弄清楚它们的含义。一些帮助将不胜感激!这是我的代码:
Stack.h
#ifndef SStack
#define SStack
#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
#include "SStack.h"
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
class SStack
{
public:
void SStack(int cap){
DyanmicStack = new string[cap];
Capacity = cap;
used = -1;
}
void SStack(const SStack& s){
DyanmicStack = new string[cap];
}
~SStack( ){
delete(DynamicStack);
}
void push(const string& s){
DynamicStack[used] = s;
used++;
}
string& pop(){
if(used==-1){
cout << "Error stack is empty";
return " ";
}
else{
used--;
return DynamicStack[used+1];
}
}
string& top () const{
if(used==-1){
cout << "Error stack is empty";
return " ";
}
else{
return DynamicStack[used];
}
}
bool isEmpty(){
return (used==-1);
}
int size(){
return (used+1);
}
int getCapacity(){
return Capacity;
}
private:
int Capacity; //How much the stack can hold
string* DynamicStack;
int used; //objects in the stack
};
SStack.h:11: error: expected unqualified-id before ‘int’
SStack.h:11: error: expected `)' before ‘int’
SStack.h:13: error: expected unqualified-id before ‘const’
SStack.h:13: error: expected `)' before ‘const’
SStack.h:14: error: expected class-name before ‘(’ token
SStack.h:25: error: ISO C++ forbids declaration of ‘operator+’ with no type
SStack.h:25: error: ISO C++ forbids declaration of ‘s2’ with no type
SStack.h:8: error: an anonymous union cannot have function members
SStack.h:31: error: abstract declarator ‘<anonymous class>’ used as declaration
SStack.cpp:11: error: expected unqualified-id before ‘int’
SStack.cpp:11: error: expected `)' before ‘int’
SStack.cpp:17: error: expected unqualified-id before ‘const’
SStack.cpp:17: error: expected `)' before ‘const’
SStack.cpp:21: error: expected class-name before ‘(’ token
SStack.cpp: In member function ‘std::string&<anonymous class>::pop()’:
SStack.cpp:33: error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘const char*’
SStack.cpp: In member function ‘std::string&<anonymous class>::top() const’:
SStack.cpp:44: error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘const char*’
SStack.cpp: At global scope:
SStack.cpp:8: error: an anonymous union cannot have function members
SStack.cpp:70: error: abstract declarator ‘<anonymous class>’ used as declaration
答案 0 :(得分:6)
您的包含护卫与您的班级同名。请记住,预处理器是一种非常简单的搜索/替换功能。
#define SStack
class SStack
{
public:
// Constructor
SStack( int cap);
变为:
#define SStack
class
{
public:
// Constructor
( int cap);
一种模式是将其命名为与标题文件名相同,例如STACK_H
。
答案 1 :(得分:1)
首先,正如已经指出的那样,头部防守被打破了。
现在,关于实际问题:
您误解了类定义的工作原理。你的标题大致正确,你的.cpp文件有什么问题。您正在重新定义已在头文件中定义的类。提供成员功能的正确方法是这个
void SStack::SStack(const SStack& s){
DyanmicStack = new string[cap];
}
或更明确的例子:
void SStack::push(const string& s){
DynamicStack[used] = s;
used++;
}
。
基本上,您必须在函数名之前添加类的名称。
另外,只是通过复制粘贴这个我已经注意到代码中的拼写错误(看看你是否可以发现它;-)),我建议重新考虑设计,即使它只是一个练习。
答案 2 :(得分:0)
我很久以前就意识到阶级Pila(堆栈),这是我的解决方案:
(对不起,但我是新的...所以我不知道如何在这里缩进代码)
档案: pila.h
//nodo is the type of elements that class pila contains.
struct nodo
{
int dato;
nodo* precedente;
};
class pila
{
private:
nodo* ultimo;
public:
pila();
void push(int numero);
int pop();
};
文件: pila.cpp
#include <iostream>
#include"pila.h"
using namespace std;
pila::pila()
{
ultimo=NULL; // punta all'ultimo nodo inserito
}
void pila::push(int numero)
{
nodo* nuovo;
nuovo=new struct nodo;
nuovo->dato=numero;
if(ultimo==NULL)
{
nuovo->precedente=NULL;
ultimo=nuovo;
}
else
{
nuovo->precedente=ultimo;
ultimo=nuovo;
}
}
int pila::pop()
{
if (ultimo==NULL)
{
return 0;
}
else
{
int prelevato=ultimo->dato;
ultimo=ultimo->precedente;
return prelevato;
}
}