Hi I am making a simple stack class in C++ and am new to C++. I am having a few errors that I cannot figure out what they mean. Some help would be greatly appreciated! Here is my code:
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
Stack.cpp
#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
};
And here are the errors:
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
Your include guard has the same name as your class. Remember that the preprocessor is a very simplistic search/replace feature.
#define SStack
class SStack
{
public:
// Constructor
SStack( int cap);
becomes:
#define SStack
class
{
public:
// Constructor
( int cap);
One pattern is to name it the same as your header's filename, such as STACK_H.
First of all, as was already noted, the header guard is broken.
Now, onto the actual problems:
You have misunderstood how class definition works. Your header is largely correct, what is wrong is your .cpp file. You are redefining class that you already have defined inside the header file. The proper way to provide implementation of member function is this
void SStack::SStack(const SStack& s){
DyanmicStack = new string[cap];
} or in for clearer example:
void SStack::push(const string& s){
DynamicStack[used] = s;
used++;
}.
Basically, you have to prepend the classes's name before the function name.
Also, just by copy pasting this I've noticed typo in your code (see if you can spot it ;-) ), and I would recommend rethinking the design, even if it is just an exercise.
I realized the class Pila(stack) some times ago, this is my solution:
(sorry but I'm new.. so I don't know how to indent the code here)
file: 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();
};
file: 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;
}
}
Related
The compiler is telling me that these functions do not belong to my class or vector. I'm a bit confused as to where I could have gone wrong. Any help would be appreciated as i scramble to figure this out. I put the header for vector but that does not resolve the issue. It seems as though my compiler is failing to detect my .h file and my private variable sorted_vector.
//Compiler
In file included from SortedVector.cpp:1:0:
SortedVector.h:22:3: error: ‘vector’ does not name a type
vector<int> sorted_vector;
^~~~~~
SortedVector.cpp: In constructor ‘SortedVector::SortedVector(int)’:
SortedVector.cpp:5:2: error: ‘sorted_vector’ was not declared in this scope
sorted_vector.reserve(cap);
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘int SortedVector::getCapacity() const’:
SortedVector.cpp:9:9: error: ‘sorted_vector’ was not declared in this scope
return sorted_vector.capacity();
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘int SortedVector::getSize() const’:
SortedVector.cpp:13:9: error: ‘sorted_vector’ was not declared in this scope
return sorted_vector.size();
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘bool SortedVector::isEmpty() const’:
SortedVector.cpp:17:6: error: ‘sorted_vector’ was not declared in this scope
if (sorted_vector.empty())
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘void SortedVector::insertVal(int)’:
SortedVector.cpp:25:8: error: ‘sorted_vector’ was not declared in this scope
if(sorted_vector.isEmpty())
^~~~~~~~~~~~~
SortedVector.cpp:28:8: error: ‘sorted_vector’ was not declared in this scope
if(sorted_vector.isEmpty()){
^~~~~~~~~~~~~
SortedVector.cpp: At global scope:
SortedVector.cpp:39:5: error: prototype for ‘int SortedVector::find(int)’ does not match any in class ‘SortedVector’
int SortedVector::find(int val) {
^~~~~~~~~~~~
In file included from SortedVector.cpp:1:0:
SortedVector.h:14:7: error: candidate is: int SortedVector::find(int) const
int find(int val) const;
^~~~
SortedVector.cpp: In member function ‘bool SortedVector::deleteVal(int)’:
SortedVector.cpp:50:2: error: ‘vector’ was not declared in this scope
vector<int> temp;
^~~~~~
SortedVector.cpp:50:2: note: suggested alternative:
In file included from /usr/include/c++/6/vector:64:0,
from SortedVector.h:6,
from SortedVector.cpp:1:
/usr/include/c++/6/bits/stl_vector.h:214:11: note: ‘std::vector’
class vector : protected _Vector_base<_Tp, _Alloc>
^~~~~~
SortedVector.cpp:50:9: error: expected primary-expression before ‘int’
vector<int> temp;
^~~
SortedVector.cpp:51:2: error: ‘temp’ was not declared in this scope
temp.reserve(sorted_vector.capacity());
^~~~
SortedVector.cpp:51:15: error: ‘sorted_vector’ was not declared in this scope
temp.reserve(sorted_vector.capacity());
^~~~~~~~~~~~~
SortedVector.cpp:75:1: error: expected ‘}’ at end of input
}
//SortedVector.h
#ifndef SORTEDVECTOR_H_
#define SORTEDVECTOR_H_
#include <iostream>
using std::ostream;
#include <vector>
class SortedVector {
public:
SortedVector();
SortedVector(int cap = 10);
void insertVal(int val);
int find(int val) const;
bool deleteVal(int val);
bool isEmpty() const;
friend ostream & operator<<(ostream & out, const SortedVector & sV);
int getCapacity() const;
int getSize() const;
private:
vector<int> sorted_vector;
};
#endif
//SortedVector.cpp
#include "SortedVector.h"
#include <vector>
SortedVector::SortedVector(int cap) {
sorted_vector.reserve(cap);
}
int SortedVector::getCapacity() const{
return sorted_vector.capacity();
}
int SortedVector::getSize() const{
return sorted_vector.size();
}
bool SortedVector::isEmpty() const{
if (sorted_vector.empty())
return true;
else
return false;
}
void SortedVector::insertVal(int val) {
if(sorted_vector.isEmpty())
sorted_vector.push_back(val);
if(sorted_vector.isEmpty()){
for(int i=0;i!=sorted_vector.size();i++){
if(sorted_vector[i]<=val)
sorted_vector.insertVal(val);
}
}
}
int SortedVector::find(int val) {
for(int i=0; i<sorted_vector.size(); i++){
if(sorted_vector[i]==val)
return i;
else
return -1;
}
}
bool SortedVector::deleteVal(int val) {
vector<int> temp;
temp.reserve(sorted_vector.capacity());
temp = sorted_vector;
int idx = sorted_vector.find(val)
if (idx!= -1 ){
for(int i =0; i<sorted_vector.size(); i++){
if(i==idx)
continue;
temp.push_back(sorted_vector[i]);
}
sorted_vector=temp;
return true
}
else
return false;
}
ostream & operator<<(ostream & out, const SortedVector & sV) {
for(int i = 0; i < sV.getSize(); i++) {
out << sV.sorted_vector[i] << " ";
}
return out;
}
I'm writing a c++ stack and queue implementation program, I finished the stack part, but when compiling I'm getting these errors
arrayListImp.cpp:18:19: error: expected unqualified-id
arrayList[++top]= x;
^
arrayListImp.cpp:28:13: error: 'arrayList' does not refer to a value
itemPoped=arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:35:9: error: 'arrayList' does not refer to a value
return arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:46:9: error: 'arrayList' does not refer to a value
cout<<arrayList[i]<<endl;
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
4 errors generated.
Here is the header file
#ifndef ARRAYLIST_H
class arrayList{
public:
arrayList();
static const int maxSize = 10;
int array[10];
};
class stack : public arrayList{
public:
stack();
void push(int x);
void pop();
int Top();
int isEmpty();
void print();
int x;
int top;
int itemPoped;
int i;
};
#define ARRAYLIST_H
#endif
arrayListImp.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
//Stack implementation
stack::stack(){
top = -1;
}
void stack::push(int x){
if (top == maxSize -1){
cout<<"Stack overflow"<<endl;
}
else{
arrayList[++top]= x;
cout<<x<<", is pushed on to the stack"<<endl;
}
}
void stack::pop(){
if (top == -1){
cout<<"Stack underflow"<<endl;
}
else{
itemPoped=arrayList[top];
top--;
cout<<itemPoped<<", is poped from the stack"<<endl;
}
}
int stack::Top(){
return arrayList[top];
}
int stack::isEmpty(){
if (top == -1) return 1;
return 0;
}
void stack::print(){
cout<<"Stack: "<<endl;
for (i = 0; i<=top; i++){
cout<<arrayList[i]<<endl;
}
}
arrayListUse.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
int main()
{
//Stack testing
stack S;
S.push(1);S.print();
S.push(2);S.print();
S.push(3);S.print();
S.pop();S.print();
S.push(4);S.print();
//Queue testing
return 0;
}
Can you please point out to what I'm doing wrong here?
You should just read your error messages.
You should use array instead of arrayList, which is the name of the class. So just refer to the variable instead.
The error message you got is something like
test.cpp: In member function ‘void stack::push(int)’:
test.cpp:44:18: error: expected unqualified-id before ‘[’ token
arrayList[++top]= x;
^
When you check the line, you immediately see what is wrong there.
You declare a constructor arrayList::arrayList(), but you do not define it. Either you can drop the declaration, or you should implement it in the cpp-file.
arrayList::arrayList() {
// do some initialization
}
The error message you got is something like
/tmp/cc4y06YN.o:test.cpp:function stack::stack(): error: undefined reference to 'arrayList::arrayList()'
The code could compile, but it did not link. So all declarations may be correct, but a symbol was missing. This is usually the case when you declared something you referred to, but you never defined it.
You always have written
arrayList[...]
what is the name of your class but reading the code it seems like you wanted to write
array[...]
which would access the data.
I am trying to learn how to use the Lemon parser generator, and I built a learning project. However, when I try to compile the sources, I get some strange errors from GCC. It looks like there could be some text transcribing problem or typo, but I can't find it.
I am compiling using the C++11 standard. The file "log" contains the compiler output.
I compile with
flex lexicon.l
lemon grammar.y
g++ -std=c++11 -o parser lex.yy.c grammar.c main.cpp 2> log
AST.h
#ifndef AST_H
#define AST_H
#include <string>
#include <sstream>
#include <iostream>
#include <unordered_map>
using namespace std;
string itos(int i) {
stringstream ss;
ss << i;
return ss.str();
}
class Identifier {
string name;
public:
Identifier()
: name("") {
cerr << "This initializer should never be called!" << endl;
}
Identifier(string _name)
: name(_name) {
}
string getName() {
return name;
}
};
unordered_map<string, Identifier> identifiers;
class ASTNode {
public:
virtual string toString() = 0;
};
class StatementNode: public ASTNode {
public:
virtual string toString() = 0;
};
class AssignmentNode: public StatementNode {
Identifier *id;
int newValue;
public:
AssignmentNode(Identifier *_id, int _newValue)
: id(_id),
newValue(_newValue) {
}
string toString() {
return id->getName() + " is now " + itos(newValue);
}
};
class ExpressionNode: public StatementNode {
public:
virtual string toString() = 0;
};
class ValueExpressionNode: public ExpressionNode {
int value;
public:
ValueExpressionNode(int _value)
: value(_value) {
}
string toString() {
return string("value: ") + itos(value);
}
};
class IdentifierExpressionNode: public ExpressionNode {
Identifier *id;
public:
IdentifierExpressionNode(Identifier *_id)
: id(_id) {
}
string toString() {
return string("id : ") + id->getName();
};
class PlusExpressionNode: public ExpressionNode {
ExpressionNode *lexp;
ExpressionNode *rexp;
public:
PlusExpressionNode(ExpressionNode *_lexp, ExpressionNode *_rexp)
: lexp(_lexp),
rexp(_rexp) {
}
string toString() {
return string("(") + lexp->toString() + " + " + rexp->toString() + ")";
}
};
class MinusExpressionNode: public ExpressionNode {
ExpressionNode *lexp;
ExpressionNode *rexp;
public:
MinusExpressionNode(ExpressionNode *_lexp, ExpressionNode *_rexp)
: lexp(_lexp),
rexp(_rexp) {
}
string toString() {
return string("(") + lexp->toString() + " - " + rexp->toString() + ")";
}
};
class TimesExpressionNode: public ExpressionNode {
ExpressionNode *lexp;
ExpressionNode *rexp;
public:
TimesExpressionNode(ExpressionNode *_lexp, ExpressionNode *_rexp)
: lexp(_lexp),
rexp(_rexp) {
}
string toString() {
return string("(") + lexp->toString() + " * " + rexp->toString() + ")";
}
};
class DividedByExpressionNode: public ExpressionNode {
ExpressionNode *lexp;
ExpressionNode *rexp;
public:
DividedByExpressionNode(ExpressionNode *_lexp, ExpressionNode *_rexp)
: lexp(_lexp),
rexp(_rexp) {
}
string toString() {
return string("(") + lexp->toString() + " / " + rexp->toString() + ")";
}
};
#endif
grammar.y:
%include {
#include <vector>
#include <iostream>
#include <cassert>
#include <sstream>
#include "AST.h"
int atoi (char * a) {
std::stringstream ss;
ss << a;
int i;
ss >> i;
return i;
}
Identifier* idForName(string name) {
if (identifiers.find(name) == identifiers.end()) {
identifiers[name] = Identifier(name);
}
return &(identifiers.at(name));
}
}
%token_type {char *}
%extra_argument {std::vector<StatementNode*>* statements}
start ::= statements.
statements ::= statement(stmt).
{
statements->push_back(stmt);
}
statements ::= statements NEWLINE statement(stmt).
{
statements->push_back(stmt);
}
%type statement {StatementNode*}
statement(stmt) ::= assignment(asgn).
{
stmt = asgn;
}
%type assignment {AssignmentNode*}
assignment(asgn) ::= IDENTIFIER(id) EQUALS NUMBER(num).
{
asgn = new AssignmentNode(idForName(id)), atoi(num));
}
statement(stmt) ::= expression(expr).
{
stmt = expr;
}
%type expression {ExpressionNode*}
expression(expr) ::= NUMBER(num).
{
expr = new ValueExpressionNode(atoi(num));
}
expression(expr) ::= IDENTIFIER(id).
{
expr = IdentifierExpression(idForName(id));
}
expression(expr) ::= LROUNDPAREN expression(pexpr) RROUNDPAREN.
{
expr = pexpr;
}
expression(expr) ::= expression(lexp) PLUS expression(rexp).
{
expr = new PlusExpressionNode(lexp, rexp);
}
expression(expr) ::= expression(lexp) MINUS expression(rexp).
{
expr = new MinusExpressionNode(lexp, rexp);
}
expression(expr) ::= expression(lexp) TIMES expression(rexp).
{
expr = new TimesExpressionNode(lexp, rexp);
}
expression(expr) ::= expression(lexp) DIVIDEDBY expression(rexp).
{
expr = new DividedByExpressionNode(lexp, rexp);
}
%left PLUS MINUS.
%left TIMES DIVIDEDBY.
%nonassoc LROUNDPAREN RROUNDPAREN.
lexicon.l:
%{
#include "grammar.h"
%}
%option noyywrap
%%
[A-Za-z_][A-Za-z0-9]* return IDENTIFIER;
[0-9]+ return NUMBER;
"=" return EQUALS;
"+" return PLUS;
"-" return MINUS;
"*" return TIMES;
"/" return DIVIDEDBY;
"(" return LROUNDPAREN;
")" return RROUNDPAREN;
\n return NEWLINE;
%%
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include "AST.h"
using namespace std;
void* ParseAlloc(void* (*allocProc)(size_t));
void Parse(void*, int, char *, vector<StatementNode*>*);
void ParseFree(void*, void(*freeProc)(void*));
int yylex();
extern char * yytext;
int main() {
vector<StatementNode*> statements;
vector<char*> strpointers;
void* parser = ParseAlloc(malloc);
while (int lexcode = yylex()) {
char *tmp = (char*)malloc((strlen(yytext)+1)*sizeof(char));
strcpy(tmp, yytext);
Parse(parser, lexcode, tmp, &statements);
strpointers.push_back(tmp);
}
for (vector<StatementNode*>::iterator i = statements.begin(); i != statements.end(); i++) {
cout << (*i)->toString() << endl;
}
Parse(parser, 0, NULL, &identifiers);
ParseFree(parser, free);
for (vector<char*>::iterator i=strpointers.begin(); i != strpointers.end(); i++) {
free(*i);
}
return 0;
}
log:
grammar.c:105:44: error: ‘constexpr’ needed for in-class initialization of static data member ‘const IdentifierExpressionNode::YYMINORTYPE IdentifierExpressionNode::yyzerominor’ of non-integral type [-fpermissive]
grammar.c:173:1: error: in-class initialization of static data member ‘const unsigned char IdentifierExpressionNode::yy_action []’ of incomplete type
grammar.c:179:1: error: in-class initialization of static data member ‘const unsigned char IdentifierExpressionNode::yy_lookahead []’ of incomplete type
grammar.c:187:1: error: in-class initialization of static data member ‘const signed char IdentifierExpressionNode::yy_shift_ofst []’ of incomplete type
grammar.c:194:1: error: in-class initialization of static data member ‘const signed char IdentifierExpressionNode::yy_reduce_ofst []’ of incomplete type
grammar.c:199:1: error: in-class initialization of static data member ‘const unsigned char IdentifierExpressionNode::yy_default []’ of incomplete type
grammar.c:257:28: error: ‘constexpr’ needed for in-class initialization of static data member ‘FILE* IdentifierExpressionNode::yyTraceFILE’ of non-integral type [-fpermissive]
grammar.c:258:30: error: ‘constexpr’ needed for in-class initialization of static data member ‘char* IdentifierExpressionNode::yyTracePrompt’ of non-integral type [-fpermissive]
grammar.c:296:1: error: in-class initialization of static data member ‘const char* const IdentifierExpressionNode::yyTokenName []’ of incomplete type
grammar.c:316:1: error: in-class initialization of static data member ‘const char* const IdentifierExpressionNode::yyRuleName []’ of incomplete type
grammar.c:642:1: error: in-class initialization of static data member ‘const IdentifierExpressionNode::<anonymous struct> IdentifierExpressionNode::yyRuleInfo []’ of incomplete type
grammar.c:842:13: error: ‘static void IdentifierExpressionNode::yy_accept(IdentifierExpressionNode::yyParser*)’ cannot be overloaded
grammar.c:644:13: error: with ‘static void IdentifierExpressionNode::yy_accept(IdentifierExpressionNode::yyParser*)’
grammar.c:1025:1: error: expected ‘}’ at end of input
grammar.y: In static member function ‘static void IdentifierExpressionNode::yy_reduce(IdentifierExpressionNode::yyParser*, int)’:
grammar.y:51:69: error: cannot call member function ‘Identifier* IdentifierExpressionNode::idForName(std::string)’ without object
grammar.y:51:96: error: cannot call member function ‘int IdentifierExpressionNode::atoi(char*)’ without object
grammar.y:51:97: error: expected ‘;’ before ‘)’ token
grammar.y:63:68: error: cannot call member function ‘int IdentifierExpressionNode::atoi(char*)’ without object
grammar.y:68:70: error: cannot call member function ‘Identifier* IdentifierExpressionNode::idForName(std::string)’ without object
grammar.y:68:71: error: ‘IdentifierExpression’ was not declared in this scope
grammar.c: At global scope:
grammar.c:1025:1: error: expected unqualified-id at end of input
main.cpp:9:7: error: expected nested-name-specifier before ‘namespace’
main.cpp:9:7: error: expected unqualified-id before ‘namespace’
main.cpp:9:7: error: expected ‘;’ before ‘namespace’
main.cpp:9:7: error: expected unqualified-id before ‘namespace’
main.cpp:16:15: error: storage class specified for ‘yytext’
main.cpp:40:1: error: expected ‘}’ at end of input
main.cpp: In member function ‘int IdentifierExpressionNode::main()’:
main.cpp:33:37: error: no matching function for call to ‘IdentifierExpressionNode::Parse(void*&, int, NULL, std::unordered_map<std::basic_string<char>, Identifier>*)’
main.cpp:33:37: note: candidate is:
main.cpp:12:6: note: void IdentifierExpressionNode::Parse(void*, int, char*, std::vector<StatementNode*>*)
main.cpp:12:6: note: no known conversion for argument 4 from ‘std::unordered_map<std::basic_string<char>, Identifier>*’ to ‘std::vector<StatementNode*>*’
main.cpp: At global scope:
main.cpp:40:1: error: expected unqualified-id at end of input
You have a missing curly brace in AST.h.
The first error message you received indicated that the given declaration wasn't allowed "in-class". This is the clue that you need. It looks like it's not supposed to be in a class, so why does the compiler think that it is? This should make you strongly suspect that somewhere earlier there is a missing closing brace. Indeed, if you look in your AST.h file, you have this:
class IdentifierExpressionNode: public ExpressionNode {
Identifier *id;
public:
IdentifierExpressionNode(Identifier *_id)
: id(_id) {
}
string toString() {
return string("id : ") + id->getName();
};
You need to add a missing closing brace to toString.
In general, when you get a huge long list of error messages like this, you should look most closely at just the first error or the first few, and ignore the rest. They are all likely from the same cause, and the further you read past the point that things started going wrong, the less useful further error messages become.
I have this code:
hpp:
#include <list>
using namespace std;
class funcionario
{
public:
struct Dia {
int d;
int h;
int id;
int tipo;
};
funcionario ();
void eliminar(int dia, int hora);
private:
list<Dia> agenda;
};
cpp:
#include "myClass.hpp"
funcionario::funcionario(){
agenda = list<Dia> ();
}
void funcionario::eliminar(int dia, int hora) {
list<funcionario::Dia>::iterator it;
it = agenda.begin();
while(it != agenda.end() && (*it).d <= dia) {
if((*it).d == dia && (*it).h == hora) {
agenda.erase(it);
return;
}
++it;
}
}
I get this compiling error:
Funcionario.cpp: In constructor ‘funcionario::funcionario()’:
Funcionario.cpp:5: error: cannot convert ‘std::list<funcionario::Dia, std::allocator<funcionario::Dia> >’ to ‘int’ in assignment
Funcionario.cpp: In member function ‘void funcionario::eliminar(int, int)’:
Funcionario.cpp:9: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
Funcionario.cpp:10: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
Funcionario.cpp:11: error: request for member ‘end’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
I don't know what I'm doing wrong.
Not sure what you're trying to achieve, but the code just needs to be fleshed out a bit with complete function definitions. I got this to compile:
#include <list>
class myClass
{
public:
myClass();
struct myStruct {
int myInfo;
};
void something();
void doSomething(myStruct & ms);
private:
std::list<myStruct> myList;
};
myClass::myClass(){
myList = list<myStruct> ();
}
void myClass::something() {
std::list<myStruct>::iterator it;
it = myList.begin();
while(it != myList.end()) {
doSomething(*it);
++it;
}
}
Incidentally (or maybe directly relevant, not sure) - the copy-initialization of myList in myClass() is unnecessary, as others have stated. The list<> default constructor will do the right thing, and more efficiently.
This seems to be working on my computer, so may it be a compiler problem? Try with another compiler and tell us if it worked
The initialization you're looking for is analogous to Initializing map and set class member variables to empty in C++? - but actually you'll get an empty list automatically (i.e. by the std::list default constructor).
--Edited to reflect your posting of the original code--
H is not declared anywhere.
and is not a valid C++ keyword or token. Use &&.
Use the local header include form of : #include "myClass.hpp"
(Beginner programmer..) I'm following the style of a header file that worked fine, but I'm trying to figure out how I keep getting all of these errors when I compile. I am compiling with g++ in Cygwin.
Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token
Ingredient.h:9:25: error: expected ‘)’ before ‘n’
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’
Ingredient.h: In member function ‘std::string<anonymous class>::name()’:
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested
Ingredient.h: In member function ‘int<anonymous class>::quantity()’:
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not match ‘int’
Ingredient.h: At global scope:
Ingredient.h:4:18: error: an anonymous struct cannot have function members
Ingredient.h:21:2: error: abstract declarator ‘<anonymous class>’ used as declaration
And here is my class header file...
#ifndef Ingredient
#define Ingredient
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string name() { return name; }
int quantity() {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I am confused by these errors and don't really know what I am doing wrong concerning the implementation of the class..
That's a funny one. You are essentially killing your class name by #define Ingredient - all occurrences of Ingredient will be erased. This is why include guards generally take the form of #define INGREDIENT_H.
You are also using name both for the member and the getter function (probably an attempt to translate C#?). This is not allowed in C++.
How about look on errors? variables and functions can't have same names. And include guard should never names such as class.
#ifndef INGREDIENT_H
#define INGREDIENT_H
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string get_name() const { return name; }
int get_quantity() const {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif