Error for getters and setters - c++

I have a vector of vertices and I wish to set vertice.setVisit as false or 0 initially. I defined some getters and setters for this, but getting some type error. Code is as per below:
//Vector:
std::vector<project3::Vertex<VertexType, EdgeType>*> Vertice1;
//Class containing methods:
template <class VertexType, class EdgeType> class Vertex{
protected:
//std::vector<std::pair<int, EdgeType>> VertexList;
VertexType vertice;
EdgeType edge;
int num;
int mark;
//int status=0;
public:
void setNum(int){ this.num = num; }
int getNum() { return num; }
int getMark(){ return mark; }
void setVisit(int) { this.mark = mark; }
};
In some function I am assigning the values to it as :
for(int i=0; i<g1.Vertice1.size(); i++)
{
g1.Vertice1.at(i)->setNum(0);
g1.Vertice1.at(i)->setVisit(0);
}
Below is the error I am getting while compilation of the code for "this.mark=mark" and for "this.num=num" in the function definition in class.
Error: left of '.mark' must have class/struct/union
Error: left of '.num' must have class/struct/union
Isn't this the correct way to assign the values through getter and setters?

In C++, this is a pointer-type. Try this->mark or (*this).mark
EDIT: As Richard pointed out below, also be sure to name your parameters which you are trying to assign. In context, this->mark = mark is the same thing as this->mark = this->mark. Unless mark is a parameter, this is really unnecessary in this case. So realistically, you can get rid of this all together in your example by doing something like this:
void setVisit(int newMark) { mark = newMark; }
Regards,
Dennis M.

Change setNum and setVisit to the following
void setNum(int num) {this->num = num; }
void setVisit(int mark) {this->mark = mark; }

this is a pointer, so you have to use this->num.

Related

global function and class reference

I want to send integer value in class to global function from main. How should I send it as a parameter,I am sending the parameters wrong
Tetris
{
private:
int num;
}
printBoard(Tetris &t);
int main()
{
Tetris tetris;
printBoard(board,tetris);
}
i want to send num , to printboard.
There are several problems with the shown code.
First, you're missing the class keyword when defining the class.
Second, the return type of the function is also missing.
Third, the function has only one parameter but you're passing two arguments.
i want to send num , to printboard
You can add a getter called getNum which you can use from inside your free function as shown below:
vvvvv------------->added this class keyword
class Tetris
{
private:
int num = 0; //don't forget to initialize
public:
//add a getter
int getNum() const
{
return num; //return a copy
}
};
vvvv----------------------->added return type as void
void printBoard(Tetris &t)
{
std::cout << t.getNum(); //use the getter
}
int main()
{
Tetris tetris;
printBoard(tetris);
}
Demo

What is the difference between int x_ and int x in C++

class Stack{
public:
char data_[100];
int top_;
};
class Stack{
public:
char data[100];
int top;
};
What is the difference between the above two classes ? When I am using the class where the variable names are like int top_ ,the stack operations are running fine but when I am using the class with variables int top, errors like this are popping up :
error: invalid use of member ( did you forget the '&' ?)
error: conflicts with previous declaration.
What is the role of the _(underscore) in this code ? Why is it making such a difference ?
#include<iostream>
#include<cstring>
using namespace std;
class Stack{
public:
char data[100];
int top;
bool empty()
{
return (top == -1);
}
char top()
{
return (data[top]);
}
void pop()
{
top--;
}
void push(char c)
{
data[++top] = c;
}
};
int main()
{
Stack s;
s.top = -1;
char str[10] = "ABCDEFGH";
for(int i=0;i<strlen(str);i++)
s.push(str[i]);
cout<<str<<endl;
cout<<"Reversed string is : ";
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}
What is the role of the _(underscore) in this code ?
It makes top and top_ 2 different identifiers. The same way you can make it top1 or topFOOBAR.
Why is it making such a difference ?
When you use top for member here you have a conflict with method named top as well. Changing top to top_ or top1 would make that conflict to disappear - you cannot have 2 different things in your class with the same name.
Some people have a habit to give member variables special names, like m_member or member_ or even _member (last one is not safe but still used sometimes). This decoration allows reader to see that code is dealing with member var on one side and avoiding name collisions like one you had on another.

Initializing object to an empty list from a reference parameter

class ListOfGifts
{
private:
Gift list[50];
int count = 0;
public:
void suggest(ListOfGifts& affordable, float dollarLimit) const
{
// how do I initialize affordable to an empty list without a constructor
}
}
Trying to initialize a list from a parameter that is a reference. How can I do this?
Use an std::array:
class ListOfGifts
{
private:
std::array<Gift, 50> list;
int count = 0;
public:
void suggest(ListOfGifts& affordable, float dollarLimit) const
{
affordable.list = std::array<Gift, 50>{};
}
}
FYI, C++ is literally built on constructors. They will come up eventually, and they're actually quite helpful.

Does the order in which my functions matter?

I cannot explain why I am getting this logic error! Take a look:
This is in the header -
class PayRoller
{
public:
void initialize();
double getNum();
void setNum(double);
double getGrossPay();
void setGrossPay(double);
double getWage();
void setWage(double);
double getAddTotal();
void setAddTotal(double);
}
And here is the first function that is called after the object is made-
void PayRoller::initialize();
{
setGrossPay(0.0);
setWage(0.0);
setAddTotal(0.0);
cout << (getGrossPay() + getAddTotal());
start();
}
And finally here are the getters and setters-
void PayRoller::setGrossPay(double temp)
{
grossPay = temp;
}
double PayRoller::getWage()
{
return wage;
}
void PayRoller::setWage(double temp)
{
wage = temp;
}
double PayRoller::getAddTotal()
{
return addTotal;
}
void PayRoller::setAddTotal(double temp)
{
wage = temp;
}
When I start the code (without debugging) the value I get from the cout
in initialize() is -9.25596e+061
What am I doing wrong here? I can't seem to figure it out. Thanks in advance!
Your setAddTotal Method is not setting addTotal, its setting wage.
void PayRoller::setAddTotal(double temp)
{
wage = temp;
}
should be
void PayRoller::setAddTotal(double temp)
{
addTotal = temp;
}
Otherwise, addTotal isn't actually set to 0, it just contains garbage.
You need to scrap the initialize() method and use a member initialization list.
What you call as Initialization is actually Assignment, Initialization can be done only in the member initialization list. What you need is initialization, ie: tying up a value to an member at the time of creation not assigning value to the member after creation.
Problem with Assignment approach is that you need to rely on the user of your class to call the methods in your defined way, which they may not.
Seems precesion issue number is almost 0. Try changing the values to long or int and this can be verfied.

I am having errors when i deal with this object i create

I cant seem to figure this out, its probably simple. I have a customer class and I am trying to create an object form that and its not working I get an undeclared identifier error and a syntax error ; missing before identifier c1. thanks
class Customer{
string customerID;
string list;
public:
Customer(void);
~Customer(void);
string getcustomerID(){
return customerID;
}
string getList(){
return list;
}
void setcustomerID(string x){
customerID = x;
}
void setList(int x){
if(x==1)
list = "bread";
if(x==2)
list = "eggs";
if(x==3)
list = "cheese";
}
};
void checkout(){
srand(time(NULL));
int random = rand() % 3 + 1;
Customer c1;
c1.setcustomerID(0);
You need a default constructor (and a destructor). If you want a really simple one (and make it compile) just do this:
Customer() {}
~Customer() {}
EDIT:
KerrekSB is right, on this particular case you are better off not defining or declaring either of them.