C++ Getting private value with getter - c++

I have a class with private and public members I am trying to use a getter Get_Words() to access the private word member. This all compiles but when the value from dir[NORTH].Get_Words() = "NORTH"; Whenever the word was just a public function without the Get_Words() member function using dir[NORTh].word = "NORTH";
Why isn't the Get_Words assigning the value correctly to word?
class Words
{
public:
static void Set_Words();
string Get_Words();
private:
string word;
}
string Word::Get_Words()
{
return word;
}
...
dir[NORTH].Get_Word() = "NORTH";
and I also tried
dir[NORTH].Get_Word() = Set_Word( string "North");
I'm pretty sure I did the setter wrong but I am new to object oriented programming in c++ and can't figure out the best way to do this.

std::string Get_Word() returns by value, aka it makes a copy of the word and returns it. When you try to assign to it, you are trying to assign to a right hand reference, which is impossible.
A way around this would to return a reference to word:
std::string& Get_Word();
But that is generally considered bad practice as the reference can outlive the class. A better way is to provide a setter along side the getter:
void Set_Word(const std::string& w) {word=w;}
or even better:
template <typename T>
void Set_Word(T&& w) {word=std::foreward<T>(w);}

To get a private value, usually a public get function is implemented.
You want to set the value - usually a public set function is implemented for this task:
void Set_Word(string& newValue) { ... }
Or implement the Getter as returning a reference, as Vlad from Moscow stated.

Related

How call internal method only with certain class and access those class variables

I'm putting together a recursive method, and need to be able to only call one method with one class type, but also access the variables of that class in the method called. How would I do that? I'm not sure what to search for online for this or what it's called. I realize people here don't have info on inner workings on my OurMap, but we can assume it's std::map. We are starting in toJson, once we're ready to convert internal data to the other format and are getting the data out of our data structure recursively. See comment below.
class OurNode: public OurIF
{
public:
void OurNode::toJson()
{
GenericValue doc(Rapidjson::kObjectType);
this.buildTree(doc);
}
void OurNode::buildTree(GenericValue &doc)
{
OurMetaType t == m_value.type();
if(t == OurMapMeta)
m_value.getMap().buildTree(doc);
else if(t == OurListMeta)
m_value.getList().buildTree(doc);
else if(t == string)
doc.AddMember(m_name, m_value);
}
//OurMap is a map of a string, OurValue combination
void OurMap::buildTree(GenericValue &doc)
{
OurMap m = this.OurMap.getValue(); //how would I access a calling class data if not this? Technically, this is OurNode, not OurMap. How do I get the OurMap data?
}
private:
string m_name;
OurValue m_value; //could be int, double, string, OurMap, OurList, etc
}
This was the answer that #AndyG had in the comment that was moved to chat:
I think what you're trying to say is that OurMap is the caller of OurNode::buildTree, and you want to access a member of that caller. C++ does not have a way to interrogate the caller; you would need to pass the caller into the function like void buildTree(OurMap& _mapInstance, GenericValue& doc){ _mapInstance.getValue();}

Cannot assign to non-static data member within const member function

I am attempting to use std::unordered_set as a hash table to store many CreditCard's. CreditCard and another class CardDatabase are defined as follows:
class CreditCard {
private:
string cardHolder;
unsigned long long cardNumber;
int limit;
int balance;
public:
CreditCard(string in_cardHolder, string in_cardNumber, int in_limit) {
cardHolder = in_cardHolder;
cardNumber = stoll(in_cardNumber);
limit = in_limit;
balance = 0;
}
void ChangeBalance(int amount) const {
balance += amount; // SECOND ERROR
}
};
class CardDatabase {
private:
unordered_set<CreditCard> cards;
unordered_set<CreditCard>::iterator iter;
public:
CardDatabase() { }
void AddCard(cardHolder, cardNumber, int limit) {
CreditCard tempCard = CreditCard(cardHolder, cardNumber, limit);
cards.insert(tempCard);
}
void Charge(string cardHolder, int chargeAmount) {
iter = cards.find(cardHolder);
iter->ChangeBalance(chargeAmount); // FIRST ERROR
}
}
Initially I was getting the following compile error at FIRST ERROR: Member function 'ChangeBalance' not viable: 'this' argument has type 'const CreditCard', but function is not marked const. So, I added the "const" to the ChangeBalance function. However, after doing that I get the following compile error at SECOND ERROR: Cannot assign to non-static member within const member function 'ChangeBalance'.
Is there any way to fix this error without changing balance to a static variable? It is obviously important that the balance be different for each CreditCard instance.
Any help is appreciated.
EDIT:
Thank you all for your quick answers. I feel I should clarify something. I already added the proper hash functionality elsewhere in my code:
namespace std {
template <>
struct hash<CreditCard> {
size_t operator()(const CreditCard& cc) const
{
return hash<string>()(cc.GetCardHolder());
}
}
}
Also, the code I posted initially pasted is from a much larger code base and I didn't delete all the necessary namespacing stuff at first before posting the question. My apologies for the confusion.
Members of an unordered_set are constant, and cannot be changed once they're in the unordered_set, by default. You are trying to change the objects in the set, and the compiler is properly telling you that you can't do this.
The only possible way to do this correctly (explained only for educational purposes, because this is bad class design):
Explicitly declare the individual fields that can be modified in this manner as mutable.
Use a custom hash function with your unordered_set, and the hash function must exclude the value of mutable fields from the value of the calculated hash.
Otherwise, modifying the contents of the object in the set obviously changes its hash value, which will result in undefined behavior.
Again, this is explained for informational purposes only. This is not a good class design.
The clean way to do this would be to assign a unique identifier to each CreditCard (you know, like a credit card number?), and use an ordinary std::map, to look up CreditCards by their number.
It's not appropriate for ChangeBalance to have const semantics. By the very nature of it's name, you're modifying the object. Make the function non-const.
void ChangeBalance(int amount) {
balance += amount;
}
The other problem is that you didn't call your function correctly. You should instead do this:
iter->ChangeBalance(chargeAmount);
I will mention there are cases where you want to modify values in a const object, and there is a mutable type modifier for that. Do not use it to solve your current error, however!
void ChangeBalance(int amount) should not be const - it is changing the object.
The problem is before in the iterator: cards.find returns a const object, so you are not allowed to change it.
A way to resolve that is to make your cards set a set of pointers to cards, not of cards; or to use another way to find the matching card
Playing fast and loose with the C++ syntax in that thar code, Hoss. Plenty of errors wait around the corner
First Error:
iter->CreditCard::ChangeBalance(chargeAmount);
should be
iter->ChangeBalance(chargeAmount);
Straight-up bad syntax that likely results from flailing around because of the errors resulting from unordered_set having no idea how to hash a CreditCard. Give this a read: How do I use unordered_set? That said, unordered_set is probably not the right solution for this job. std::map<std::string, CreditCard> looks more on point.
Using the wrong solution to fix the above problem lead to the
Second Error:
void ChangeBalance(int amount) const
const on a method means the method cannot change the state of the object. in ChangeBalance balance += amount; attempts to change the state of the object by updating a member variable.
In addition, the compiler is going to HATE the CreditCard:: in this:
CreditCard::CreditCard(string in_cardHolder, string in_cardNumber, int in_limit) {
cardHolder = in_cardHolder;
cardNumber = stoll(in_cardNumber);
limit = in_limit;
balance = 0;
}
Another solution is to make the "balance" as a static member.
class CreditCard {
private:
string cardHolder;
unsigned long long cardNumber;
int limit;
static int balance;
....
}
And then initialize it in cpp file
int CreditCard::balance = 0;
This code may not be very secure. But this can be one of the workaround.

Using class references to modify public members within another class

Since my last question had too much code in it, I tried to make the simplest example of what I'm trying to do. Take this for example,..
#include <iostream>
using namespace std;
class String
{
public:
private:
};
class ClassTwo
{
public:
int memberVariable;
private:
};
class ClassOne
{
public:
ClassOne (ClassTwo&, String&);
~ClassOne();
private:
ClassTwo& classTwoReference;
String& stringReference;
};
ClassOne::ClassOne (ClassTwo& two, String& string)
: classTwoReference (two), stringReference (string)
{
two.memberVariable = 3;
}
ClassOne::~ClassOne()
{
}
int main()
{
String stringObject;
ClassTwo classTwoObject;
ClassOne classOneObject (classTwoObject, stringObject);
}
In JUCE, which is the API I'm using to code a VST Plugin, there is a string class that JUCE names "String". I'm not sure exactly what the constructor does, but you can use something like this to create a String object.
String newString("string");
The ClassTwo in my case, is the AudioProcessor class which has a public member variable that I can access from ClassOne like this.
two.memberVariable = 3;
The ClassOne in my case, is a custom Component(I named it PixelSlider) that I'm using in my GUI. It uses a slider listener to check the status of a slider and modify the member variable in ClassTwo(AudioProcessor). I can do this fine using the method above, but the issue is that I want to create as many ClassOne(PixelSlider) objects as I need. I want to pass them a String object that tells them what member variable of ClassTwo(AudioProcessor) to modify. Logically, this would be done by passing a reference to a String object with the same string value as the name of the ClassTwo member variable. Like this,...
ClassOne::ClassOne (ClassTwo& two, String& string)
: classTwoReference (two), stringReference (string)
{
two.(string) = 3;
}
This doesn't work in JUCE, but can anybody tell me a way to get this done without having to create a bunch of different classes almost exactly like ClassOne(PixelSlider) that modify different ClassTwo(AudioProcessor) member variables?
If I understand correctly, you're trying to bind a PixelSlider target to a member of AudioProcessor at runtime, which, as you've discovered, can't be done the way you suggest ( two.(string) = 3 ). One way of achieving this binding would be to use the command pattern (http://sourcemaking.com/design_patterns/command/cpp/2).
AudioProcessor could expose a collection of these command objects for each modifiable property ...
AudioProcessorCommand
AudioProcessor::GetCommandByName(String const& properyName) const
{
...
}
... which you can pass to the constructor of PixelSlider. Something along the lines of ...
PixelSlider::PixelSlider(AudioProcessorCommand& command)
: command_{command}
{
...
}
When the PixelSlider's value changes you would invoke the command ...
command_(this->value_);

Call virtual function from another subclass

I've been set an assignment to create an rpn calculator that takes infix notation as input. So part of it is that it has to print out different stages of the process. So first it should separate a string into tokens and then store in a vector. Then it should convert this to rpn notation (e.g 3+4 -> 3 4 +) which is the part im stuck on now the part I'm stuck on now.
I've been recommended to use virtual abstract functions for this. So first I create a class with the abstract function. Then I create a subclass which converts a string to tokens stored in a string vector, this part works fine. Then I should create another subclass which converts the input string to rpn notation, therefore I have to call the function to convert to tokens at the start of this sub-class, this is the bit which I think is going wrong.
I have been given some code as a template and so far it's been very buggy so there might be something wrong with the syntax where the error is.
So I have this as my main class
template<typename T>
class tokenstream {
public:
virtual bool process(const std::string& input, std::vector<T>& output) = 0;
};
Then this as the first subclass
class tokenifier: public tokenstream<std::string> {
public:
bool process(const std::string& input, std::vector<std::string>& output) {
//this part works fine, ive tested it.
};
So then I have to create another subclass and then call the above function inside it, this is the part where it goes wrong.
class infix2rpn: public tokenstream<std::string> {
private:
tokenifier *tokens;
public:
tokenifier(tokenstream *_tokens): tokens(_tokens) {} //I think this line is the problem
bool process(const std::string& input, std::vector<std::string>& output) {
//call the underlying tokenstream object
std::vector<std::string> infixtokens;
if(!tokens->process(input, infixtokens))
{
return false;
}
return shunting_yard(infixtokens, output);
}
bool shunting_yard(const std::vector<std::string>& input, std::vector<std::string>& output){
//i've tested the shunting_yard algorithm and it works fine
}
};
When I try to compile it I get the error "ISO C++ forbids declaration of 'tokenifier' with no type [-fpermissive].
So the part I don't understand is how to call other virtual functions from another subclass.
Thanks
Your class is called infix2rpn, so its constructor should be named infix2rpn as well, not tokenifier. This has nothing to do with virtual functions.
Moreover, your attribute should be a tokenstream<std::string>*, not a tokenifier*, because you can't convert the tokenstream<std::string>* you get in the constructor to a tokenifier*.
tokenifier(tokenstream *_tokens): tokens(_tokens) {}
This was probably meant to be constructor, but in that case, the name of the method should be infix2rpn, same as the class name.
The error means, that you specified method tokenifier that has not specified return type, only constructors and destructors have no return type.
Note that void also specification of return type, in that case it means nothing returned.

C++ Class with member varialble vector <another class>

I have an assignment to make Information system about resorts in a country, be able to read/write data for the resorts from/to file and modifying it.
class CTŠ¾urist{
string m_StrName;
string m_StrCountry;
int m_iAge;
public:
//constructors, mutators, accessors overloading operator <<,>>
};
I don't have problem writing this class. And here I have class which contains as member variable vector of the first class's objects
class CHotel
{
string m_strHotelName;
int m_iNumberOfBets;
double m_dAveragePrice; //average price per bet in the hotel
vector <CTourist> m_vecTourists; //vector of tourists rested in the hotel
public:
.....
};
And one more class Resort containing as member variable vector of the second class's objects
class CResort
{
string m_ResortName;
unsigned m_Height;
vector<CHotel*> m_Hotels;
public:
.....
};
So here is the problem. I'm not sure how to write the accessor,mutator and constructors for that vector variable so I can use them property. Thank you for checking and if someone could help me figure out these functions I'll be really grateful!
if i understand correctly you want to know the best way to get your hotels from cResort.
i would recommend
cHotel* GetHotelByName(std::string& a_sName)
{
for(int i = 0; i < m_Hotels.size(); ++i)
{
if(m_Hotel[i].GetName() == a_sName)
return m_Hotel[i]
}
return nullptr; // if non found return return null
}
and add a GetName function to your hotel class which returns a string of its name.
this also allows you to SetName etc.
1) Accessor, mutator: There are plenty of options.
You can create another class like CTouristList (and CHotelList respectively), that wraps the vector, have it referenced from the CHotel class (accessor methods like CTouristList& CHotel::GetTouristList() and const CTouristList& CHotel::GetTouristList() const) and implement methods like CTouristList::Add, CTouristList::Remove, CTouristList::Get, etc.
Or you can add methods like CHotel::AddTourist() directly on the CHotel class.
2) Constructor. Nothing needed in constructor. But for vector<CHotel*> you may need destructor in CResort to explicitly free the CHotel instances. Though not sure why you want to use pointers to CHotel.
You can put the accessor and mutator functions in CTourist just like you would if they were not being stored in a Vector.
To utilize them once they are in CHotel you could add a function in CHotel that returns a pointer to a CTourist.
// Access a CTourist
Hotel.getTourist(1)->setName("Tourist name");
Adding a method that returns the number of tourist that visited a hotel would make it easier to loop through them.
for(int i = o; i < Hotel.touristCount(); ++i)
{
// Do something useful
std:: cout << "Hello " << Hotel.getTourist(i)->getName();
}
In that case your CHotel::touristCount() would be a wrapper around the vector<>.size();
If you do not want code outside of CHotel to have direct access to a CTourist object then create wrapper functions in CHotel that do what you would want to do externally.
i.e.
std::cout << Hotel.getTouristName(1);
instead of
std::cout << Hotel.getTourist(1)->getName();