C++ "Class Enum" Novice Problems - c++

I'm starting learning to code in C++ (coming from VB.net) and i need some help in enum class usage.
I've done this simple code from a lerning exercise (originally divided in header.h and Exercise.cpp, but i putted it all together here):
#include <iostream>
#include <string>
#define BEGIN_WITH(x) { \
auto &_ = x;
#define END_WITH() }
using namespace std;
enum class Gender { Male, Female };
struct PersonStruct {
string _Name;
string _SurName;
int _Age;
double _Heigth;
Gender _Gender; };
class Person { public:
string _Name{};
string _SurName{};
int _Age{};
double _Heigth{};
Gender _Gender{}; };
int ModifyPerson(Person& PassPersona, PersonStruct Attribute) {
PassPersona._Name = Attribute._Name;
PassPersona._SurName = Attribute._SurName;
PassPersona._Heigth = Attribute._Heigth;
PassPersona._Age = Attribute._Age;
PassPersona._Gender = Attribute._Gender;
return(0); }
int main() { Person TestingPerson;
BEGIN_WITH(TestingPerson)
_._Age = 23;
_._Gender = Gender::Male;
_._Heigth = 1.94;
_._Name = "John";
_._SurName = "Smith";
END_WITH()
cout << "Person attributes: " << endl;
cout << "Name: " << TestingPerson._Name << endl;
cout << "Surname: " << TestingPerson._SurName << endl;
cout << "Age: " << TestingPerson._Age << endl;
cout << "Gender: " << TestingPerson._Gender << endl;
cout << "Heigth: " << TestingPerson._Heigth << endl;
cout << endl;
ModifyPerson(TestingPerson, PersonStruct{ "Poca","Hontas",24,1.85,Gender::Female });
cout << "New Person attributes: " << endl;
cout << "Name: " << TestingPerson._Name << endl;
cout << "Surname: " << TestingPerson._SurName << endl;
cout << "Age: " << TestingPerson._Age << endl;
cout << "Gender: " << TestingPerson._Gender << endl;
cout << "Heigth: " << TestingPerson._Heigth << endl;
return(0); }
I've made the structure in order to group all Person class parameters. Doing this i've learned that using enum class is more secure than class. But when i switched to enum class a lot of errors prompted on my code. I've solved almost all of them, except this:
cout << "Gender: " << TestingPerson._Gender << endl;
Error code E0349 "no operator "<<" matches these operands"
I've searched around but i find no solution to this. Thanks in advance for your time! (any suggestion or recommendation on my code should be appreciated)

<< operator is not defined for class Gender because it's your own class. You'll need to overload it. More info here.
enum class Gender
{
male,
female
};
ostream& operator<< (ostream & os, const Gender & g)
{
switch(g)
{
case Gender::male: os << "Male"; return os;
case Gender::female: os << "Female"; return os;
}
return os;
};

Related

Compiler error - is private within this context - Line 31

#include<iostream>
#include<string>
using namespace std;
class Item{
private:
string type;
string abbrv;
string uID;
int aircraft;
double weight;
string destination;
public:
void print(){
cout << "ULD: " << type << endl;
cout << "Abbreviation: " << abbrv << endl;
cout << "ULD-ID: " << uID << endl;
cout << "Aircraft: " << aircraft << endl;
cout << "Weight: " << weight << " Kilograms" << endl;
cout << "Destination: " << destination << endl;
}
friend void kilotopound(Item);
};
void kilotopound(Item I){
cout << "Weight in Pounds: " << I.weight * 2.2 << " LBS " << endl;
}
int main(){
Item I;
I.type = "Container";
I.uID = "AYK68943IB";
I.abbrv = "AYK";
I.aircraft = 737;
I.weight = 1654;
I.destination = "PDX";
I.print();
kilotopound(I);
return 0;
}
Starting on line 31 I'm getting the error 'std::__cxxll::string Item::type' is private within this context
I'm basically trying to make the data private from this code
class Item{
public:
string type;
string abbrv;
string uID;
int aircraft;
double weight;
string destination;
void print(){
cout << "ULD: " << type << endl;
cout << "Abbreviation: " << abbrv << endl;
cout << "ULD-ID: " << uID << endl;
cout << "Aircraft: " << aircraft << endl;
cout << "Weight: " << weight << " Kilograms" << endl;
cout << "Destination: " << destination << endl;
}
friend void kilotopound(Item);
};
void kilotopound(Item I){
cout << "Weight in Pounds: " << I.weight * 2.2 << " LBS " << endl;
}
int main(){
Item I;
I.type = "Container";
I.uID = "AYK68943IB";
I.abbrv = "AYK";
I.aircraft = 737;
I.weight = 1654;
I.destination = "PDX";
I.print();
kilotopound(I);
return 0;
}
Any help would be greatly appreciated, I'm just sort of lost on how I can resolve the error. Thanks!
Also I need to be able to copy and output the copied data once again if anyone can help with that as well, with private data too. Thanks again!
#include<iostream>
#include<string>
using namespace std;
class Item{
private:
string type;
string abbrv;
string uID;
int aircraft;
double weight;
string destination;
public:
Item(string t, string a, string u, int aC, double w, string d){
type = t;
abbrv = a;
uID = u;
aircraft = aC;
weight = w;
destination = d;
}
void print() {
cout << "ULD: " << type << endl;
cout << "Abbreviation: " << abbrv << endl;
cout << "ULD-ID: " << uID << endl;
cout << "Aircraft: " << aircraft << endl;
cout << "Weight: " << weight << " Kilograms" << endl;
cout << "Destination: " << destination << endl;
}
friend void kilotopound(Item);
};
void kilotopound(Item I){
cout << "Weight in Pounds: " << I.weight * 2.2 << " LBS " << endl;
}
int main(){
Item I ("Container", "AYK68943IB", "AYK", 737, 1654, "PDX");
I.print();
kilotopound(I);
return 0;
}

C++ proper usage of ostream inside a class and passing arguments?

I've recently started learning c++ and for the life of me, I can't seem to get the syntax of using ostream in a class and what arguments should I pass. Here's the code:
This is the class in question:
#include <iostream>
#include <string>
using namespace std;
class Pokemon{
friend ostream& operator<<(ostream&, Pokemon);
public:
string name, level, cp;
Pokemon(string x="Pikachu", string y="5", string z="1000"){
name = x;
level = y;
cp = z;
}
Pokemon name(){
return this->name;
}
Pokemon level(){
return this->level;
}
Pokemon cp(){
return this->cp;
}
Pokemon display_stats(){
cout << this-> name << "stats are:" << endl;
cout << " " << "Attack: 2716.05" << endl;
cout << " " << "Defence: 1629.63" << endl;
cout << " " << "HP: 1086.42" << endl;
}
};
template<typename TYPE> //i dont understand this and the things i've written down here are only based on samples i've seen
ostream& operator<<(ostream& os, Pokemon & c){
os << "The level of " << c.name << " is" << c.level << " with cp of " << c.cp;
}
As you could see, I already tried constructing the ostream thing but I don't really understand how it works. This is my main function:
int main()
{
Pokemon a, b, c, d;
a = Pokemon();
b = Pokemon("Weezing");
c = Pokemon("Nidoking", 100);
d = Pokemon("Mewtwo", 50, 5432.1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "Jessie: You are no match to me! Go " << b.name << "!" << endl;
cout << "Gary: Go lvl " << c.level << " " << c.name << "! Crush them" << endl;
cout << "Ash: " << a.name << " can do it even thouh he is only level " << a.level << endl;
cout << "Jessie: Hahaha! My " << b.name << " CP is " << b.cp << endl;
cout << "Gary: "<< c.name << " CP is " << c.cp << endl;
cout << "Ash: " << a.name << " CP is " << a.cp << endl;
cout << "Giovanni: Behold " << d.name << " is here." << endl;
d.display_stats();
return 0;
}
I'm getting errors of:
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [9], int) //on line c = Pokemon("Nidoking", 100);
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [7], int, double) //on line d = Pokemon("Mewtwo", 50, 5432.1);
All of your Pokemon class methods are returning the wrong type. And your main() is not calling any of the methods correctly at all.
Change your Pokemon class to look more like this:
#include <iostream>
#include <string>
using namespace std;
class Pokemon {
private:
string m_name;
int m_level;
double m_cp;
friend ostream& operator<<(ostream&, const Pokemon&);
public:
Pokemon(string x="Pikachu", int y=5, double z=1000) {
m_name = x;
m_level = y;
m_cp = z;
}
string name() const {
return m_name;
}
int level() const {
return m_level;
}
double cp() const {
return m_cp;
}
void display_stats() const {
cout << m_name << " stats are:" << endl;
cout << " " << "Attack: 2716.05" << endl;
cout << " " << "Defense: 1629.63" << endl;
cout << " " << "HP: 1086.42" << endl;
}
};
ostream& operator<<(ostream& os, const Pokemon &c) {
os << "The level of " << c.m_name << " is " << c.m_level << " with cp of " << c.m_cp;
return os;
}
And then change main() to look more like this:
int main()
{
Pokemon a;
Pokemon b("Weezing");
Pokemon c("Nidoking", 100);
Pokemon d("Mewtwo", 50, 5432.1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "Jessie: You are no match to me! Go " << b.name() << "!" << endl;
cout << "Gary: Go lvl " << c.level() << " " << c.name() << "! Crush them" << endl;
cout << "Ash: " << a.name() << " can do it even though he is only level " << a.level() << endl;
cout << "Jessie: Hahaha! My " << b.name() << " CP is " << b.cp() << endl;
cout << "Gary: " << c.name() << " CP is " << c.cp() << endl;
cout << "Ash: " << a.name() << " CP is " << a.cp() << endl;
cout << "Giovanni: Behold " << d.name() << " is here." << endl;
d.display_stats();
return 0;
}
Live Demo

How to get variable values from main and send them to a function?

I have encountered a problem while running this code in my IDE. You can see very early on that I've attempted to use a function. The reason for this is to save memory later on by outputting text, but the problem occurs with the variables in the function. The classType variable is uninitialized, how do I prevent this? I have defined them in main, but when I try to output the text with the variables from main then it doesn't work properly.
#include<iostream>
using namespace std;
string getName()
{
   string charName;
   int classType;
   cout << "What is your " << classType << "'s name?" << endl;
   cin >> charName;
   return charName;
}
int main()
{
   int classType; //Later we will ask the user what class they're playing.  
   string charName;
   /*We will use a function to ask a question.  
   We use a function to save memory instead of copy-pasting the text*/
   cout <<"Welcome to \"Orcs and Ogres\"" << endl;
   cout << "What class do you want to play?  " << endl;
   cout << "\tType 1 for Warrior class]" << endl;
   cout << "\tType 2 for Archer class ]" << endl;
   cout << "\tType 3 for Mage class   ]" << endl;
   cin >> classType;
   if(classType == 1)
   {
       cout << endl << "You are a warrior" << endl;
       string classType;
       classType = "warrior";
       getName();
   }
   else if(classType == 2)
   {
       cout << endl << "You are an archer" << endl;
       string classType;
       classType = "archer";
       getName();
   }
   else if(classType == 3)
   {
       cout << endl << "You are a mage" << endl;
       string classType;
       classType = "mage";
       getName();
   }
   else
   {
       cout << endl << "UserError:  Number too high or too low";
   }
}
On the lines of code that use getName(), it outputs something like "What is your blank's name?" instead of the proper classType. I want to know how I can send variable values to a function from main so that it outputs text properly here.
The reason why it's not working is because your getName function has no knowledge of whats stored in the classType variable. Read on how function variable scope works to understand the whole mechanism works might be beneficial.
If you wish to keep the current implementation of your program. Re-write your getName function to accept the string class as a parameter
string getName(string classType)
{
string charName;
cout << "What is your " << classType << "'s name?" << endl;
cin >> charName;
return charName;
}
and in your main you'd call the function as follow :
getName("Warrior"); // to ask warrior for a warriors' name
getName("Mage"); // to ask for a mage's name.
You may also want to add to include the string library at the top of your file as not having it might also cause your code to simply not work. As well as making sure to properly store the name returned from your getName() function as follow :
string name = getName("Warrior");
Also , as others have said , maybe reading a bit more on how function receive and return values might be beneficial to you.
It is as simple as this. Try this updated code...
#include<iostream>
using namespace std;
string getName(string classType)
{
string charName;
cout << "What is your " << classType << "'s name?" << endl;
cin >> charName;
cout<<"your "<<classType<< "'s name is "<<charName<<endl;
return charName;
}
int main()
{
int Type;
string charName;
cout <<"Welcome to \"Orcs and Ogres\"" << endl;
cout << "What class do you want to play? " << endl;
cout << "\tType 1 for Warrior class]" << endl;
cout << "\tType 2 for Archer class ]" << endl;
cout << "\tType 3 for Mage class ]" << endl;
cin >> Type;
if(Type == 1)
{
cout << endl << "You are a warrior" << endl;
string classType;
classType = "warrior";
getName("warrior");
}
else if(Type == 2)
{
cout << endl << "You are an archer" << endl;
string classType;
classType = "archer";
getName("archer");
}
else if(Type == 3)
{
cout << endl << "You are a mage" << endl;
string classType;
classType = "mage";
getName("mage");
}
else
{
cout << endl << "UserError: Number too high or too low";
}
return 0;
}

Why does using my print method with std::cout result in an error?

#include <iostream>
using namespace std;
class Fam
{
public:
char you, urmom, urdad;
void addPerson(char y, char m, char f)
{
you = y;
urmom = m;
urdad = f;
}
};
class Tree: public Fam
{
public:
void showFamtree()
{
cout<< "Name: " << you << endl;
cout<< "Mother's name: " << urmom <<endl;
cout<< "Father's name: " << urdad <<endl;
}
};
int main(void)
{
Tree tree;
char a,b,c;
cin >> a;
cin >> b;
cin >> c;
tree.addPerson(a,b,c);
cout<< "Family tree: " << tree.showFamtree() <<endl;
return 0;
}
I wanted to print the family tree with
the person's name, mother's name, father's name
but when I compile it, I get the following error:
invalid operands to binary expression (basic_ostream<char, std::char_traits<char> > and void)
tree.showFamtree() returns nothing (i.e. void), it doesn't make any sense to try to pass it to std::cout. You might change
cout<< "Family tree: " << tree.showFamtree() <<endl;
to
cout << "Family tree: " << endl;
tree.showFamtree();
If you define operator << like this
ostream& operator << ( ostream & ostr , const Tree & t ){
ostr << "Name:" << t.you << endl
<< "Mother's name:" << t.urmom << endl
<< "Father's name:" << t.urdad << endl;
return ostr;
}
then you can use
cout<< "Family tree: " << tree <<endl;
This is known as operator overloading in C++.
To use something similar to this:
void showFamtree()
{
cout<< "Name: " << you << endl;
cout<< "Mother's name: " << urmom <<endl;
cout<< "Father's name: " << urdad <<endl;
}
with:
cout << "Family tree: " << tree.showFamtree() << endl;
One C++ approach would be to use std::stringstream, as in:
std::string showFamtree()
{
std::stringstream ss;
ss << "Name: " << you << endl;
ss << "Mother's name: " << urmom <<endl;
ss << "Father's name: " << urdad <<endl;
return (ss.str());
}
I also often add a label. So consider using
std::string showFamtree(std::string label)
{
std::stringstream ss;
ss << label;
ss << "Name: " << you << endl;
ss << "Mother's name: " << urmom <<endl;
ss << "Father's name: " << urdad <<endl;
return (ss.str());
}
and change invocation to
cout << tree.showFamtree("Family tree: ") << endl;
Note - Perhaps the label should be on its own line, for consistent white space on the left of the 'tree'.

C++ no operator "<<" match these operand (inheritance)

I have just recently started class inheritance in c++. While I was making a "Test" program, a error occurred with the cout statement. No clue how to fix it and would be appreciate your response.
#include <iostream>
using namespace std;
class Power{
public:
void isWeak(){
cout << " Weak"<< endl;
}
void isStrong(){
cout << " Strong" << endl;
}
};
class Person:public Power{};
class Person2:public Power{};
int main(){
Person human;
Person2 human2;
cout << "Human is " << human.isWeak() << endl; //error
cout << "Human 2 is " << human2.isStrong() << endl; //error
system("pause");
return 0;
}
the main()'s cout statement has that error between the output and human
Change the functions to
char const *isWeak(){
return " Weak";
}
char const *isStrong(){
return " Strong";
}
As currently defined, both functions have void return type, which means the cout statements within main are trying to print void, which doesn't make sense, and is the cause of the error.
You are attempting to print a void:
cout << "Human is " << human.isWeak() << endl;
is the same as typing
cout << "Human is " << void << endl;
Which will not compile. What you need to do is define your functions in either of the following ways:
class Power
{
public:
std::string isWeak()
{
return std::string(" is weak");
}
std::string isStrong()
{
return std::string(" is strong");
}
};
Or, change your code:
cout << "Human is ";
human.isWeak();
cout << endl;
cout << "Human 2 is ";
human2.isStrong();
cout << endl;
Problem is with isWeak() and isStrong() return type. these two functions return void and you are trying to print it. you can try this-
cout << "Human is " ;
human.isWeak();
cout << endl;
cout << "Human 2 is " ;
human2.isStrong();
cout << endl;
You're trying to 'print' a 'void' statement in cout << "Human is " << human.isWeak() << endl;
You'll need to change your isWeak and isStrong functions to return a std::string/const char* or change the way you call them:
to string:
const char* isWeak() {
return " Weak";
}
// then you can do
cout << "Human is " << human.isWeak() << endl;
Or change the way you call the function:
cout << "Human is ";
human.isWeak();
Your isWeak and isStrong functions are void they do not return anything; calling cout << human.isWeak() is expecting isWeak to return something (an int, string, double, etc.).