저는 C++을 처음 사용하고 있으며 오류가 발생하며이 문제에 대해 도움을 줄 수있는 이유가 무엇인지 알 수 없습니까? 미리 감사드립니다.C++ NetBeans 오류 : 'int'앞에 부적합 ID가 필요합니다.
여기 내 헤더 파일입니다.
sstack.cpp : 14 : 24 : 오류 : 'INT'전에 예상 비정규-ID 여기
#ifndef SSTACK_H
#define SSTACK_H
#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
내 첫 번째 오류는 MY 오류가이 클래스에있는 sstack의 .cpp 파일입니다
sstack.cpp : 14 : 24 : 오류 : 'INT'
내 두 번째 오류 전에 ')'예상은 다음과 같습니다
sstack.cpp : 19 : 24 : 오류 : 예상 비정규-ID'const'이전에
sstack.cpp : 19 : 24 : 'const'before Ive가 온라인에서 둘러보고 문제가 어떤 아이디어인지 파악하는 것 같습니다. BTW
당신이 누락
#include "sstack.h"
// Constructor
//ERROR HERE
sstack(int cap){
test = new std::string [cap];
Capacity = cap;
}
// Copy Constructor
//ERROR 2 HERE
sstack(const sstack& s){
test = new std::string[1000];
for(int i = s.size()-1; i > 0; i--){
test[i] = *s.pop();
}//end of for
Capacity = s.getCapacity();
used = s.size();
}
~sstack(){
delete []test;
}
void push (const std::string& s){
test[used] = *s;
used++;
}
std::string& pop(){
used-= 1;
popped = test[used];
test[used] = "";
return *popped;
}
std::string& top() const{
top = test[used--];
return *top;
}
bool IsEmpty() const{
if(used <= 0){
return true;
}else{
return false;
}
}
int size() const{
return used;
}
int getCapacity() const{
return Capacity;
}
// 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){
int amount = used;
if(amount + s2.size() <= Capacity){
for(int i = used + s2.size()-1; i > used; i--){
test[i] = *s2.pop();
used++;
}//end of for
}//end of if
}
int Capacity = 1000; // Capacity is the maximum number of items that a stack can hold
std::string* DynamicStack;
int used = 0; // How many items are stored in the stack
std::string test[1000];
std::string popped;
std::string top;
여기서 'test = new std :: string [1000]'배열의 크기를 하드 코딩해서는 안됩니다. 그것은 원본의 진정한 사본이 아닙니다. 또한 생성자 initializer-list에서 초기화를하는 것이 더 좋습니다. 그리고 정말로, '원시'배열에서 멀리 떨어져있어 라. 표준 콜렉션을 보라. – codah