C++ simple class - c++

I started learning C++, classes, objects, structures and more, but I'm having some problems with this.
#include <iostream>
using namespace std;
class Owner
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
};
};
class Pet
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
}
};
int main()
{
// Creating object ...
cout << "qq" << endl;
return 0;
}
But I get these errors when I try to compile it:
In member function 'std::string Owner::GetName()':|
main.cpp|9|error: expected primary-expression before '.' token|
In member function 'int Owner::GetAge()':|
main.cpp|10|error: expected primary-expression before '.' token|
In member function 'short int Owner::GetGender()':|
main.cpp|11|error: expected primary-expression before '.' token|
In member function 'void Owner::SetName(std::string)':|
main.cpp|14|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetAge(int)':|
main.cpp|15|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetGender(short int)':|
main.cpp|16|error: expected unqualified-id before '.' token|
main.cpp|45|error: expected unqualified-id before '}' token|
In member function 'std::string Pet::GetName()':|
main.cpp|30|error: expected primary-expression before '.' token|
In member function 'int Pet::GetAge()':|
main.cpp|31|error: expected primary-expression before '.' token|
In member function 'short int Pet::GetGender()':|
main.cpp|32|error: expected primary-expression before '.' token|
In member function 'void Pet::SetName(std::string)':|
main.cpp|35|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetAge(int)':|
main.cpp|36|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetGender(short int)':|
main.cpp|37|error: expected unqualified-id before '.' token|
||=== Build finished: 13 errors, 0 warnings ===|
Why does it give me so many errors?
I don't know why, because it is obvious that, for example,
string GetName()
{
return info.name;
}
returns a string, from the structure info.name
I'm using CodeBlocks.

You're declaring the struct as a type (Owner.info) instead of as a member (this->info). You probably want this:
struct OwnerInfo
{
string name;
int age;
short int gender;
};
class Owner {
// stuff..
private:
OwnerInfo info;
};
Or, the more reasonable version would be just having them there directly instead of inside a pointless struct:
class Owner {
// stuff..
private:
string name;
int age;
short int gender;
};

You're misunderstanding the syntax of the struct keyword, furthermore the actual member variable has to be declared before the member functions accessing it. So change your class declarations to something like
class Owner
{
private:
struct
{
string name;
int age;
short int gender;
} info;
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
};

The declaration:
private:
struct info
{
string name;
int age;
short int gender;
};
... defines the layout of a nested structure type info, much like the definition of the entire class does. However, info is now a type nested within Owner, not an instance of that type that is a member of Owner. Instead, try naming the struct Info, then declaring Info info = new Info(); in the private section of Owner.

Well you created a struct and therefor told your compiler "info" is a typ with the following attributes...
You need to declare a variable of the type "info".
info personalInfo;
Declare it as class member and you can create your Get-er and Set-er.
string GetName(){return personalInfo.name;}
More Information

You should create a object of struct info. You just cannot access a field of a struct directly without creating an object.. Why do you need the struct at all? try this
class Owner
{
private:
string name;
int age;
short int gender;
public:
// Getters
string GetName(){return this->name;}
int GetAge(){return this->age;}
short int GetGender(){return this->gender;}
// Setters
void SetName(string value){this->name = value;}
void SetAge(int value){this->age = value;}
void SetGender(short int value){this->gender = value;}
};

Related

Never declared custom class members in lemon-generated parser cause compile errors

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.

Making a simple stack in C++

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;
}
}

How to properly use a header file to be a complete class?

(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

class issue in C++

I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.

C++ fstream in class

I started studying classes and now I faced a problem. I'm trying to put all my variables into a class, but I get errors:
main.cpp|6|error: expected identifier before string constant|
main.cpp|6|error: expected ',' or '...' before string constant|
main.cpp|7|error: expected identifier before string constant|
main.cpp|7|error: expected ',' or '...' before string constant|
Although when I make them global everything works fine
class Kauliukas{
ifstream inFile("inFile.in");
ofstream outFile("outFile.out");
int n, akutes[100],k=0;
void ivedimas();
void skaiciavimas();
void isvedimas();
};
What's the problem?
Initialization goes in the constructor. That's different than, for instance, C#.
You must define a constructor like
class Kauliukas {
public:
Kauliukas() : inFile("inFile.in"), outFile("outFile.out"), k(0) {}
private:
ifstream inFile;
ofstream outFile;
int n, akutes[100],k;
void ivedimas();
void skaiciavimas();
void isvedimas();
};
In pre-C++11 versions of the language you can only declare variables inside the class body, you can't also initialize them (ifstream inFile is a declaration; ifstream inFile("infile.in") is a declaration and an initialization).
You have to do it like this:
class Kauliukas
{
public:
Kauliukas();
private:
ifstream inFile;
};
Kauliukas::Kauliukas() // This is the constructor definition
: inFile("infile.in") // This is called an initialization list
{
// ...
}