Cannot call member function std::string Sportist::getIme() without object - c++

I have a class A:
class Sportist{
private:
string ime;
int godina_na_ragjanje;
int godisna_zarabotuvacka_EUR;
public:
Sportist(string i, int g_n_r, int g_z_EUR){
ime = i;
godina_na_ragjanje = g_n_r;
godisna_zarabotuvacka_EUR = g_z_EUR;
}
string getIme(){
return ime;
}
int getGodinaNaRagjanje(){
return godina_na_ragjanje;
}
int getGodisnaZarabotuvackaEUR(){
return godisna_zarabotuvacka_EUR;
}
};
And class B using the class A as public:
class Fudbaler:public Sportist{
private:
int broj_na_odigrani_natprevari;
int danocna_stapka;
public:
Fudbaler(string ime, int godina, int zarabotuvacka, int b, int d)
:Sportist(ime, godina, zarabotuvacka)
{
broj_na_odigrani_natprevari = b;
danocna_stapka = d;
}
float danok(){
return getGodisnaZarabotuvackaEUR() * danocna_stapka;
}
friend ostream& operator<<(ostream &os, Fudbaler F){
return os << "Ime: " << getIme() << endl
<< "Godina na raganje: " << getGodinaNaRagjanje() << endl
<< "Godisna zarabotuvacka(EUR): " << getGodisnaZarabotuvackaEUR() << endl
<< "Danok sto treba da plati: " << danok();
}
};
And I get 4 errors as described in title in these lines:
return os << "Ime: " << getIme() << endl
<< "Godina na raganje: " << getGodinaNaRagjanje() << endl
<< "Godisna zarabotuvacka(EUR): " << getGodisnaZarabotuvackaEUR() << endl
<< "Danok sto treba da plati: " << danok();
cannot call member function 'std::string Sportist::getIme()' without object
cannot call member function 'int Sportist::getGodinaNaRagjanje()' without object
cannot call member function 'int Sportist::getGodisnaZarabotuvackaEUR()' without object
cannot call member function 'float Fudbaler::danok()' without object

i would say the function should be changed to
friend ostream& operator<<(ostream &os, Fudbaler F){
return os << "Ime: " << F.getIme() << endl
<< "Godina na raganje: " << F.getGodinaNaRagjanje() << endl
<< "Godisna zarabotuvacka(EUR): " << F.getGodisnaZarabotuvackaEUR() << endl
<< "Danok sto treba da plati: " << F.danok();
}
I am not shure about operator overloading for the std::streams. i usually have done that outside of the class. From your error messages, you need to use the passed Fudbaler variable to access the methods of it.

Related

Compiler Error - 'class Item' has no member named 'a' Line 46

#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;
}
Item(){}
Item(Item & i){
type = i.type;
abbrv = i.abbrv;
uID = i.uID;
aircraft = i.aircraft;
weight = i.weight;
destination = i.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;
}
bool operator == (Item &ab, Item &bc){
if((ab.a == bc.a) && (ab.u == bc.u)){
return true;
}
else{
return false;
}
}
int main(){
Item I ("Pallet", "PAG32597IB", "PAG", 737, 3321, "SEA");
I.print();
kilotopound(I);
cout << "\nCopy\n\n";
Item I2(I);
I.print();
kilotopound(I);
cout<<"\nSecond Object\n\n";
I2.print();
kilotopound(I2);
Item unit1 ("Pallet", "PAG32597IB", "PAG", 737, 3321, "SEA");
Item unit2(unit1),unit3(I);
if (unit1 == unit2)
cout << "\nUnit1 and Unit2 are the same \n";
else
cout << " \nUnit1 and Unit2 aren't the same \n";
if (unit2 == unit3)
cout << " \nUnit2 and Unit3 are the same\n";
else
cout << " \nUnit2 and Unit3 aren't the same\n";
return 0;
}
Hi! So the code above gives me the error 'class Item' has no member named 'a' at line 46, which is where to bool operator is. Can someone help me out with basically just trying to get the code to function and explain why I get the error even though I have the data made public?
I'm pretty lost at why this happens even if the abbreviations are already stated. Also I tried just using abbrv and uID but got an error that the data was private.
Thank you!
a is the parameter of Item's constructor Item(string t, string a, string u, int aC, double w, string d), not an "alias" of Item::abbrv. Constructors are functions, and the parameters of a function are only available inside the function body.
There is no "member alias" in C++, nor is there any concept of "properties" (like in C#). To provide read-only access to Item::abbrv, define a "getter" function for it:
// Public member function of class Item:
const string& getAbbrv() const {
return abbrv;
}

I can't access to the protected member of my base class

I am new at programming using c++ and having some troubles creating my constructors & objects.
How can I access to my protected members like int p_iID in the Fahrzeug class?
I have to access them for both of my objects seperately.
I would be so happy if you could help me out with this.
class Fahrzeug {
private:
protected:
string p_sName;
int p_iID;
double p_dMaxGeschwindigkeit;
double p_dGesamtStrecke;
double p_dGesamtZeit;
double p_dZeit;
public:
virtual void vAusgeben(Fahrzeug* pFahrzeug1,Fahrzeug* pFahrzeug2);
virtual void vKopf();
virtual void vSimulieren(Fahrzeug *pFahrzeug, Fahrzeug *pFahrzeug2);
class PKW;
class PKW: public Fahrzeug{
PKW(const int p_iMaxID, string p_sName, double p_dMaxGeschwindigkeit, double p_dGesamtStrecke) {
p_iID = p_iMaxID;
this->p_sName = p_sName;
this->p_dMaxGeschwindigkeit = (p_dMaxGeschwindigkeit < 0) ? 0 : p_dMaxGeschwindigkeit;
this->p_dGesamtStrecke = p_dGesamtStrecke;
}
void vAusgeben(PKW pkw1, PKW pkw2) {
cout << "\n";
PKW pkw1;
PKW pkw2;
pkw1.vKopf();
cout << setw(5) << left << pkw1.p_iID<< " " << setw(10) <<pkw1.p_sName << setw(8) << " " << setw(15) << showpoint << pkw1.p_dMaxGeschwindigkeit << setw(3) << " " << pkw1.p_dGesamtStrecke; //Here I have the issue with pkw1.p_sName
cout << "\n";
cout << setw(5) << left << pkw2.p_iID << " " << setw(10) << pkw2.p_sName << setw(8) << " " << setw(15) << showpoint << pkw2.p_dMaxGeschwindigkeit << setw(3) << " " << pkw2.p_dGesamtStrecke;
cout << "\n";
}
}
void vAusgeben(PKW pkw1, PKW pkw2) {
You probably don't want to pass your PKW objects by value (or expect object slicing). Pass const references instead:
void vAusgeben(const PKW& pkw1, const PKW& pkw2) {
Also, why are you shadowing your 2 parameters with these local variables?
PKW pkw1; // ???
PKW pkw2; // ???
Aside from the issues raised in comments (and in another answer), there's a special rule for protected members that sometimes surprises people. An object of a derived type can access protected members of its base sub-object, but it can't access protected members of some other object. So:
struct B {
protected:
int i;
};
struct D : B {
void f(const B&);
};
void D::f(const B& b) {
i = 3; // okay, accessing my own protected member
b.i = 3; // no, access to protected member of different object not allowed
}
In the code in the question, the function PKW::vAusgeben can access its own copies of p_sName, p_dMaxGeschwindigkeit, and p_dGesamtStrecke, but it can't access pkw1.p_sName, pkw1.p_dMaxGeschwindigkeit, or pkw1.p_dGesamtStrecke.

Having difficulty creating operator overload for class module using literal string as argument

I am relatively new to C++. I am hoping to get assistance creating an overload operator that will permit me to use a literal string with a class I am writing. This project is for learning.
I created a custom Date class (and have not worried about the actual logic in the class; I'm just trying to get the interface working and to understand where my roadblock is.
It may not look like it, but I have put in a lot of time on this, so an explanation designed for a beginning c++ progammer would be very helpful to me and anyone who follows in my footsteps.
Basically, I'd like to be able to do this with my class:
Date dt(5,6,92);//create class object
int myint;
myint = dt;//this works
string mystr;
mystr = dt("mm/dd/yy");//this does not compile
Thanks in advance. (A compilable test program shown below)
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
class Date
{
int mo, da, yr;
public:
Date(int m, int d, int y)
{
cout << "Constructor int m, int d, int y called " << endl;
mo = m; da = d; yr = y;
}
string getdatestr(const char *s = "") {//user sends in format "d2/" "mm/dd/yy" etc
cout << "getdatestr const char *s called mo = " << mo << " da = " << da << "yr = " << yr << endl;
return to_string(mo) + "/" + to_string(da) + "/" + to_string(yr);
}
int getdate(){
cout << "getdate int t called" << endl;
string tag;
tag = to_string(yr) + to_string(mo) + to_string(da);
return stoi(tag);
}
string getdate(string &str){
cout << "getdate with string as ref" << endl;
return "5/5/55";
}
int getdate(int &myint){
cout << "getdate with int as ref" << endl;
return 12345;
}
void setdate(string dt){
cout << "setdate string dt called " << dt << endl;
mo = 1;
da = 2;
yr = 2020;
}
void setdate(int intdte){
cout << "setdate int to " << intdte << endl;
mo = 99;//any int for now
da = 98;
yr = 1997;
}
void setdate(const char *dte){
cout << "setdate char* dte = " << dte << endl;
mo = 94;
da = 95;
yr = 1996;
}
~Date(){
cout << "destructor called" << endl;
}
//below code permits this in main(): myint = dt;
operator int() {
cout << "operator int()" << endl;
string tag;
tag = to_string(yr) + to_string(mo) + to_string(da);
return stoi(tag);
}
//end section to permit: myint = dt;
//what do I need so I can do this: mystr = dt("mm/dd/yy");, nothing below worked; I tried many more iterations
//than what are shown here
/*
operator string() {
cout << "operator string char" << endl;
return "hello world";
}
string operator = (string &rhs){
cout << "string" << endl;
return "return a string";
}
operator = (const string &rhs){
cout << "will this work?" << endl;
return *this;
}
char& operator = (char(&)[9]){
cout << "whoa, it worked" << endl;
//return "got it";
}
operator = (char*){
cout << "try again" << endl;
}
string operator const char(&)[9] {
cout << "one more time" << endl;
string *ptr;
ptr = "one more time";
return ptr;
}
//end nothing worked to permit me to do this mystr = dte("mm/dd/yy"); section
*/
};//end of class
int main()
{
//create a Date class object dt
Date dt(5, 6, 92);
dt.setdate("02/15/22");
cout << endl;
cout << "next two mystr messages return null because I " << endl;
cout << "can't seem to write a valid overload operator for a literal string" << endl;
string mystr;
//mystr = dt("mm/dd/yy");//does not compile (no match for call to '(Date) (const char [9])'
cout << "mystr using dt(str) = " << mystr << endl;
string myconv = "mm/dd/yy";
//mystr = dt(myconv);//does not compile (no match for call to '(Date) (std::__cxx11::string&)'
cout << "mystr using dt(mm//dd//yy) = " << mystr << endl;
cout << endl;
//this section works
//can I assign dt to an integer (as #days from a reference date)
cout << "this section seems to work" << endl;
int myint;
cout << "myint = dt;" << endl;
myint = dt;//does not compile
cout << "myint (using = dt;) = " << myint << endl;
cout << endl;
system("pause");
}

operator << of enum type uses the overloaded operator or not?

I hope this time my question is better formulated and formatted.
Here's the code that produces two separate outputs when I think it should not since I use everytime (I think) the overloaded operator<< for an enum type.
#include <iostream>
using namespace std;
enum Etat { Intact = 5 };
class Ship {
public:
Etat etat_;
Ship ( Etat t = Intact) : etat_(t) {}
~ Ship() {}
ostream& description ( ) const { return cout << "Etat: " << etat_ << " --- ";}
//---------------------------------------ˆˆˆˆ----
};
ostream& operator<< ( ostream& s, const Etat& etat_ )
{
switch ( etat_ )
{
case Intact: s << "intact"; break;
default: s << "unknown state";
}
return s;
}
ostream& operator<< ( ostream& s, Ship n ) { return s << "Etat: " << n.etat_ ; }
int main()
{
Etat etat_ = Intact;
cout << endl << endl << "Etat: "
<< etat_ << " \"cout << etat_\"" << endl << endl;
cout << Ship(etat_)
<< " \"cout << Ship(etat_)\"" << endl << endl;
cout << Ship(etat_).description()
<< " \"cout << Ship(etat_).description()\"" << endl << endl;
return 0;
}
This is what I get in the terminal:
Etat: intact "cout << etat_"
Etat: intact "cout << Ship(etat_)"
Etat: 5 --- 1 "cout << Ship(etat_).description()"
Can anyone explain to me why, in the last case, not only it takes the integer value of the enum attribut, but also adds a "1" after the test string " --- "???
The only thing I can think of is because I used an unorthodox return method in description(), ie 'return cout << ..", but it seems to work since the test string appears.
Is there a way to force the use of the operator<< overload in description()?
Thanks
In the description() function you are returning a reference to std::cout and use it in the std::cout call in main function. There is a reason why operator<< takes an ostream reference as it's first argument. You should modify your function like this and all should work:
ostream& description(ostream& os) const {
return os << "Etat: " << etat_ << " --- ";
}
The random "1" printed out there is caused likely due to the ostream in your example trying to print out reference to itself.

How can I use the insertion operator with polymophism [duplicate]

This question already has answers here:
Overriding of operator<< in c++
(2 answers)
Closed 7 years ago.
I want to implement a composite pattern where I can print the contents using std::cout
When I print, the base class insertion operator (operator<<) is used instead of the most derived type. How can I cause the most derived type operator to be used?
Example:
#include <iostream>
#include <memory>
using namespace std;
class Base {
int i;
public:
Base(int _i) : i(_i) {}
virtual ~Base() {}
friend inline ostream& operator<<(ostream& os, Base& value) { return os << "i: " << value.i; }
friend inline ostream& operator<<(ostream& os, shared_ptr<Base> value) { return os << "i: " << value->i; }
};
class Derived : public Base {
int x;
public:
Derived(int _x) : Base(2*_x), x(_x) {}
friend inline ostream& operator<<(ostream& os, Derived& value) { return os << "x: " << value.x; }
friend inline ostream& operator<<(ostream& os, shared_ptr<Derived> value) { return os << "x: " << value->x; }
};
int main () {
Base* a = new Base(1);
Derived* d = new Derived(6);
Base* b = new Derived(7);
shared_ptr<Base> ba = make_shared<Base>(3);
shared_ptr<Derived> de = make_shared<Derived>(4);
shared_ptr<Base> bd = make_shared<Derived>(5);
cout << "a is: " << a << endl;
cout << "*a is: " << *a << endl;
cout << "d is: " << d << endl;
cout << "*d is: " << *d << endl;
cout << "b is: " << b << endl;
cout << "*b is: " << *b << endl << endl;
cout << "ba is: " << ba << endl;
cout << "*ba is: " << *ba << endl;
cout << "de is: " << de << endl;
cout << "*de is: " << *de << endl;
cout << "bd is: " << bd << endl;
cout << "*bd is: " << *bd << endl;
delete a;
delete d;
delete b;
return 0;
}
live code
This spits out
a is: 0x1fe2bb0
*a is: i: 1
d is: 0x1fe2bd0
*d is: x: 6
b is: 0x1fe2bf0
*b is: i: 14
ba is: i: 3
*ba is: i: 3
de is: x: 4
*de is: x: 4
bd is: i: 10
*bd is: i: 10
But I would like to see *b print 7 and bd print 5 (ie. use the Derived class' insertion operator)
Ok, after searching a lot more, I found this answer. After smacking my head and saying 'of course', I summarize:
Don't overload the insertion operator since you can't make it virtual (it's not a member function), instead declare a virtual function in the base class that is overridden in the child classes. The insertion operator uses this function:
class Base {
...
friend inline ostream& operator<<(ostream& os, shared_ptr<Base> value) { return value->display(os, value); }
virtual ostream& display(ostream& os) { os << "i: " << i << endl; }
};
class Derived : public Base {
...
ostream& display(ostream& os) { os << "x: " << x << endl; }
};
live code