How to properly use a header file to be a complete class? - c++

(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

Related

Compilation error with struct/class defined in a function

What is the problem with the following code. Because if I define the class inside the main function, the compilation fails and I don't understand the compiler error.
Test the code from here
Comment the 1st definition of drift_f (outside of main()) and uncomment the inner definition of drif_t (inside the main() function) and the compiler will get the following error message:
prog.cpp: In function ‘int main()’:
prog.cpp:27:24: error: template argument for ‘template<class> class std::allocator’ uses local type ‘main()::drift_t’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:24: error: trying to instantiate ‘template<class> class std::allocator’
prog.cpp:27:24: error: template argument 2 is invalid
prog.cpp:27:31: error: invalid type in declaration before ‘;’ token
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:39: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:69: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
#include <iostream>
#include <deque>
using namespace std;
class drift_t{ //It works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
int main() {
/*class drift_t{ //It doesn't works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
return 0;
}
Isn't the error the one that the compiler says it is? You can't use the local class for that template initialization.
Try compiling with -std=c++11 as I believe it relaxes on that.

I'm getting a error when I declare a class as a member of other class. error : a class-key must be used when declaring a friend

I am a C++ rookie and I was experimenting with boost serialization and I wanted to see if it works when a class is declared as a member of another class. But when I compile my code I get loads of errors. I tried declaring baseds as a struct but no change in errors. My code :
#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
class baseds{};
class superior{};
class baseds {
private:
friend class boost::serialization::access;
public:
int a;
int b;
int c;
baseds(){}
~baseds(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
}
};
class superior {
private:
friend class boost::serialization::access;
public:
int x;
int y;
baseds lag;
superior(){}
~superior(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & lag;
}
};
int main() {
superior myData,myData2;
myData.x=10;
myData.y=20;
myData.lag.a=1;
myData.lag.b=2;
myData.lag.c=3;
ofstream ofs("steps.txt");
{
boost::serialization::archive one(ofs);
one << myData;
}
ifstream ifs("steps.txt");
{
boost::serialization::archive two(ifs);
two >> myData2;
}
std::cout<<"\n"<<myData2.x;
std::cout<<"\n"<<myData2.y;
std::cout<<"\n"<<myData2.lag.a;
std::cout<<"\n"<<myData2.lag.b;
std::cout<<"\n"<<myData2.lag.c;
return 0;
}
errors:
tier2.cpp:10: error: a class-key must be used when declaring a friend
tier2.cpp:29: error: a class-key must be used when declaring a friend
tier2.cpp:32: error: expected `;' before "int"
tier2.cpp: In member function `void superior::serialize(Archive&, unsigned int)':
tier2.cpp:38: error: `x' undeclared (first use this function)
tier2.cpp:38: error: (Each undeclared identifier is reported only once for each function it appears in.)
tier2.cpp:39: error: `y' undeclared (first use this function)
tier2.cpp:40: error: `lag' undeclared (first use this function)
tier2.cpp: In function `int main()':
tier2.cpp:47: error: 'class superior' has no member named 'x'
tier2.cpp:48: error: 'class superior' has no member named 'y'
tier2.cpp:49: error: 'class superior' has no member named 'lag'
tier2.cpp:50: error: 'class superior' has no member named 'lag'
tier2.cpp:51: error: 'class superior' has no member named 'lag'
tier2.cpp:53: error: `ofstream' undeclared (first use this function)
tier2.cpp:53: error: expected `;' before "ofs"
tier2.cpp:55: error: `archive' is not a member of `boost::serialization'
tier2.cpp:55: error: expected `;' before "one"
tier2.cpp:56: error: `one' undeclared (first use this function)
tier2.cpp:57: error: expected `;' before '}' token
tier2.cpp:59: error: `ifstream' undeclared (first use this function)
tier2.cpp:59: error: expected `;' before "ifs"
tier2.cpp:61: error: `archive' is not a member of `boost::serialization'
tier2.cpp:61: error: expected `;' before "ones"
tier2.cpp:62: error: `ones' undeclared (first use this function)
tier2.cpp:63: error: expected `;' before '}' token
tier2.cpp:64: error: 'class superior' has no member named 'x'
tier2.cpp:65: error: 'class superior' has no member named 'y'
tier2.cpp:66: error: 'class superior' has no member named 'lag'
tier2.cpp:67: error: 'class superior' has no member named 'lag'
tier2.cpp:68: error: 'class superior' has no member named 'lag'
tier2.cpp:71:2: warning: no newline at end of file
You are re-defining both baseds and superior. You should get an error similar to
class baseds{}; // definition
class superior{}; // definition
// second definition
class baseds {
private:
....
error: redefinition of 'class baseds'
Remove the first pair of definitions.
You are defining baseds and superior twice.
On gcc you would get the proper error message:
main.cpp:9:7: error: redefinition of 'baseds'
class baseds {
^
main.cpp:6:7: note: previous definition is here
class baseds{};
^
main.cpp:27:7: error: redefinition of 'superior'
class superior {
^
main.cpp:7:7: note: previous definition is here
class superior{};
Seemingly your compiler does not recognize the redefinition and gets lost, producing the poor error message.
If you meant to provide a forward declaration of both classes, you need to lose the braces:
class baseds;
class superior;
class baseds {
/* ... */
};
But sice you don't use superior for the definition of baseds at all, you can leave out the forward declarations completely - for the definition of superior you need the definition of baseds, since you have a member of that type and therefore a forward declaration is not sufficient.
Sorry fellow stackoverflow citizens, this was a stupid question, figured out most of the errors after a good sleep
The correct code is as below:
#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
class baseds;
class superior;
class baseds {
private:
friend class boost::serialization::access;
public:
int a;
int b;
int c;
baseds(){}
~baseds(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
}
};
class superior {
private:
friend class boost::serialization::access;
friend class baseds;
public:
int x;
int y;
baseds lag;
superior(){}
~superior(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & lag;
}
};
int main() {
superior myData,myData2;
myData.x=10;
myData.y=20;
myData.lag.a=1;
myData.lag.b=2;
myData.lag.c=3;
std::ofstream ofs("steps.txt");
{
boost::archive::text_oarchive one(ofs);
one << myData;
}
std::ifstream ifs("steps.txt");
{
boost::archive::text_iarchive two(ifs);
two >> myData2;
}
std::cout<<"\n"<<myData2.x;
std::cout<<"\n"<<myData2.y;
std::cout<<"\n"<<myData2.lag.a;
std::cout<<"\n"<<myData2.lag.b;
std::cout<<"\n"<<myData2.lag.c;
return 0;
}

I'm getting a error when I declare a class as a member of other class. error : a class-key must be used when declaring a friend [duplicate]

I am a C++ rookie and I was experimenting with boost serialization and I wanted to see if it works when a class is declared as a member of another class. But when I compile my code I get loads of errors. I tried declaring baseds as a struct but no change in errors. My code :
#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
class baseds{};
class superior{};
class baseds {
private:
friend class boost::serialization::access;
public:
int a;
int b;
int c;
baseds(){}
~baseds(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
}
};
class superior {
private:
friend class boost::serialization::access;
public:
int x;
int y;
baseds lag;
superior(){}
~superior(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & lag;
}
};
int main() {
superior myData,myData2;
myData.x=10;
myData.y=20;
myData.lag.a=1;
myData.lag.b=2;
myData.lag.c=3;
ofstream ofs("steps.txt");
{
boost::serialization::archive one(ofs);
one << myData;
}
ifstream ifs("steps.txt");
{
boost::serialization::archive two(ifs);
two >> myData2;
}
std::cout<<"\n"<<myData2.x;
std::cout<<"\n"<<myData2.y;
std::cout<<"\n"<<myData2.lag.a;
std::cout<<"\n"<<myData2.lag.b;
std::cout<<"\n"<<myData2.lag.c;
return 0;
}
errors:
tier2.cpp:10: error: a class-key must be used when declaring a friend
tier2.cpp:29: error: a class-key must be used when declaring a friend
tier2.cpp:32: error: expected `;' before "int"
tier2.cpp: In member function `void superior::serialize(Archive&, unsigned int)':
tier2.cpp:38: error: `x' undeclared (first use this function)
tier2.cpp:38: error: (Each undeclared identifier is reported only once for each function it appears in.)
tier2.cpp:39: error: `y' undeclared (first use this function)
tier2.cpp:40: error: `lag' undeclared (first use this function)
tier2.cpp: In function `int main()':
tier2.cpp:47: error: 'class superior' has no member named 'x'
tier2.cpp:48: error: 'class superior' has no member named 'y'
tier2.cpp:49: error: 'class superior' has no member named 'lag'
tier2.cpp:50: error: 'class superior' has no member named 'lag'
tier2.cpp:51: error: 'class superior' has no member named 'lag'
tier2.cpp:53: error: `ofstream' undeclared (first use this function)
tier2.cpp:53: error: expected `;' before "ofs"
tier2.cpp:55: error: `archive' is not a member of `boost::serialization'
tier2.cpp:55: error: expected `;' before "one"
tier2.cpp:56: error: `one' undeclared (first use this function)
tier2.cpp:57: error: expected `;' before '}' token
tier2.cpp:59: error: `ifstream' undeclared (first use this function)
tier2.cpp:59: error: expected `;' before "ifs"
tier2.cpp:61: error: `archive' is not a member of `boost::serialization'
tier2.cpp:61: error: expected `;' before "ones"
tier2.cpp:62: error: `ones' undeclared (first use this function)
tier2.cpp:63: error: expected `;' before '}' token
tier2.cpp:64: error: 'class superior' has no member named 'x'
tier2.cpp:65: error: 'class superior' has no member named 'y'
tier2.cpp:66: error: 'class superior' has no member named 'lag'
tier2.cpp:67: error: 'class superior' has no member named 'lag'
tier2.cpp:68: error: 'class superior' has no member named 'lag'
tier2.cpp:71:2: warning: no newline at end of file
You are re-defining both baseds and superior. You should get an error similar to
class baseds{}; // definition
class superior{}; // definition
// second definition
class baseds {
private:
....
error: redefinition of 'class baseds'
Remove the first pair of definitions.
You are defining baseds and superior twice.
On gcc you would get the proper error message:
main.cpp:9:7: error: redefinition of 'baseds'
class baseds {
^
main.cpp:6:7: note: previous definition is here
class baseds{};
^
main.cpp:27:7: error: redefinition of 'superior'
class superior {
^
main.cpp:7:7: note: previous definition is here
class superior{};
Seemingly your compiler does not recognize the redefinition and gets lost, producing the poor error message.
If you meant to provide a forward declaration of both classes, you need to lose the braces:
class baseds;
class superior;
class baseds {
/* ... */
};
But sice you don't use superior for the definition of baseds at all, you can leave out the forward declarations completely - for the definition of superior you need the definition of baseds, since you have a member of that type and therefore a forward declaration is not sufficient.
Sorry fellow stackoverflow citizens, this was a stupid question, figured out most of the errors after a good sleep
The correct code is as below:
#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
class baseds;
class superior;
class baseds {
private:
friend class boost::serialization::access;
public:
int a;
int b;
int c;
baseds(){}
~baseds(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
}
};
class superior {
private:
friend class boost::serialization::access;
friend class baseds;
public:
int x;
int y;
baseds lag;
superior(){}
~superior(){}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & lag;
}
};
int main() {
superior myData,myData2;
myData.x=10;
myData.y=20;
myData.lag.a=1;
myData.lag.b=2;
myData.lag.c=3;
std::ofstream ofs("steps.txt");
{
boost::archive::text_oarchive one(ofs);
one << myData;
}
std::ifstream ifs("steps.txt");
{
boost::archive::text_iarchive two(ifs);
two >> myData2;
}
std::cout<<"\n"<<myData2.x;
std::cout<<"\n"<<myData2.y;
std::cout<<"\n"<<myData2.lag.a;
std::cout<<"\n"<<myData2.lag.b;
std::cout<<"\n"<<myData2.lag.c;
return 0;
}

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

C++ simple class

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