accessing a vector in a function - qt

I'm quite new to c++ and am not very good at understanding about references and pointers.
I'm making a project in Qt to book appointments in a salon and am currently trying to assign a stylist object to the stylist data member in the chair object.
The function below is to assign stylist to a particular chair. At the end of this function, if you print out the stylist name of the stylist in the chair it has been assigned, however outside of this function the value is not assigned. All the getters are returning a reference but the Calender vector, a vector of Week objects, is just called as it is a data member of the class holders. I am wondering if this is the issue but I thought using the [] operator meant that the vector value at that index was returned by reference.
void Holders::BookStylistToChair(int week, int day, float timeslot, stylist temp)
{
for (int i = 0; i < Calender[week].getaweek()[day].getaday()[timeslot].GetNumberOfChairsInslot(); i++) {
bool check = Calender[week].getaweek()[day].getaday()[timeslot].GetChairVector()[i].getoccupied();
if (check == false) {
Calender[week].getaweek()[day].getaday()[timeslot].GetChairVector()[i].SetStylist(temp);
Calender[week].getaweek()[day].getaday()[timeslot].GetChairVector()[i].Setoccupied();
break;
}
}
}
The Week class is shown below:
class Week {
private:
//A week full of 7 days
QVector<Day> AWeek;
public:
//CREATE DAYS IN A WEEK
void Create7days(int, int, int);
//Getter
QVector<Day>& getaweek() ;
};
The week class contains a getter getaweek, which returns a vector of objects of class day. Day contains
class Day {
private:
QString DayName;
// A day full of timeslots
QVector<Timeslot> ADay;
public:
//Create Timeslots
void CreateTimeSlot(int, int, int);
void setdayname(QString);
//Getter
QString getdayname();
QVector<Timeslot>& getaday() ;
};
The getters are all returning references but I'm unsure if they are correct because I haven't used const even though I know I probably should but I keep getting errors when I do I'm not sure how to include const references.
I hope this is enough information I'm sorry if it isn't.
Thank you

Related

After I declare an object from a class, and try to set a variable to that object inly, why does it say that it does not declare a type?

I am writing code for a school project that will be used for a Chromebook charging station with security. The problem I am having now is when I am detecting if a Chromebook is actually in the slot after the user has been assigned one, I am using a rocker switch to simulate this but when I am declaring the pin to the rocker, the arduino verfier comes up with that
"'slot1' does not name a type".
Code is below:
//class
class Chromebook_slot {
public:
String Name = "";
String RFID_tag = "";
int rocker = 0;
boolean chromebook_in = false;
//class function to check if chromebook is in.
//if not, redirect already to reassigning so chromebook slot is entered as open and free.
void set_if_in()
{
int momen_1_state = digitalRead(momen_1);
int momen_2_state = digitalRead(momen_2);
// the button has been pushed down and the previous process has been completed
// eg. servos would have been reset if there was a previous user
if (momen_1_state == HIGH || momen_2_state == HIGH)
{
chromebook_in = digitalRead(this->rocker);
if (chromebook_in == 0)
{
re_assigning();
}
else
{
return;
}
}
}
};
//this is now outside the class..
//class declarations
Chromebook_slot slot1;
Chromebook_slot slot2;
//variables for rocker switches which will act for detecting chromebooks.
// in my final version, this will replaced by a photoresistor and laser.
slot1.rocker = 3;
slot2.rocker = 2;
Where the function re_assigning() is a separate function declared further in the code and just resets the slot as open for future use.
slot1.rocker = 3;
slot2.rocker = 2;
These are statements that cannot be at the top level of a C++ (or .ino) file. They need to be inside of a function. What's happening is the compiler is looking looking at the slot1 identifier through the lens of potential valid constructions. It sees an identifier, and about the only thing that could legally exist at this point in the code that starts with an identifier like that is some declaration, e.g. int a = 7;, or more abstractly some_type some_more_stuff. So it expects slot1 to be a type, which it isn't, hence the message.
If you want an assignment like those to happen early on in an Arduino program, the simplest thing you could do is put them in setup():
void setup() {
slot1.rocker = 3;
slot2.rocker = 2;
// ...
}
Or, you'd make these part of the Chromebook_slot's constructor, such that they could be given in slot1 and slot2's declaration:
class Chromebook_slot {
public:
Chromebook_slot(int rocker_init_value) {
rocker = rocker_init_value;
}
// ...
Or in a maybe less familiar but more proper form, using the constructor's initialization list:
class Chromebook_slot {
public:
Chromebook_slot(int rocker_init_value)
: rocker(rocker_init_value) {}
// ...
Once you have a constructor for Chromebook_slot, your variables can become:
Chromebook_slot slot1(3);
Chromebook_slot slot2(2);

Turbo C++ File Handling: Stuck in input loop

I am making a program in turbo C++ to create student records and modify/delete them in a binary file on user's command.
The main class that is needed to know is the student class:
class student
{
private:
int roll_no;
char name[50];
academic ac;
co_curricular cc;
void calculate();
public:
int get_data(int);
void show_data();
void show_tabular();
int ret_roll_no();
};
There is some problem with the get_data() function, specially in the part where the roll number is assigned. The logic to assign the roll number is:
student temp;
fstream fp;
roll_no = random(9000) + 1000;
//Checking if roll number is unique
fp.open("STUDENT.DAT", ios::in);
while(!fp.eof())
{
fp.read((char*)&temp, sizeof(temp));
if(roll_no == temp.ret_roll_no())
roll_no = random(9000) + 1000; //Set roll number to another random value
}
fp.close();
Binary file STUDENT.DAT already exists, but the code doesn't go after the loop. It is somehow stuck.
Please help

MQL4/5 CList Search method always returning null pointer

I'm trying to use the CList Search method in an application. I have attached a very simple example below.
In this example, I always get a null pointer in the variable result. I tried it in MQL4 and MQL5. Has anyone ever made the Search method work? If so, where is my mistake? With my question, I refer to this implementation of a linked list in MQL (it's the standard implementation). Of course, in my application, I do not want to find the first list item, but items that match specific criteria. But even this trivial example does not work for me.
#property strict
#include <Arrays\List.mqh>
#include <Object.mqh>
class MyType : public CObject {
private:
int val;
public:
MyType(int val);
int GetVal(void);
};
MyType::MyType(int val): val(val) {}
int MyType::GetVal(void) {
return val;
}
void OnStart() {
CList *list = new CList();
list.Add(new MyType(3));
// This returns a valid pointer with
// the correct value
MyType* first = list.GetFirstNode();
// This always returns NULL, even though the list
// contains its first element
MyType* result = list.Search(first);
delete list;
}
CList is a kind of linked list. A classical arraylist is CArrayObj in MQL4/5 with Search() and some other methods. You have to sort the list (so implement virtual int Compare(const CObject *node,const int mode=0) const method) before calling search.
virtual int MyType::Compare(const CObject *node,const int mode=0) const {
MyType *another=(MyType*)node;
return this.val-another.GetVal();
}
void OnStart(){
CArrayObj list=new CArrayObj();
list.Add(new MyType(3));
list.Add(new MyType(4));
list.Sort();
MyType *obj3=new MyType(3), *obj2=new MyType(2);
int index3=list.Search(obj3);//found, 0
int index2=list.Search(obj2);//not found, -1
delete(obj3);
delete(obj2);
}

Create simple calculator with Dynamic AX 2012

I'm new with Dynamic AX and I want to create a simple calculator with input values and display the result in the form:
Your question is very broad to be answered precisely because there are a lot of strategies to tackle the task, but judging from the screenshot you have provided you have a class which should contain all calculation logic and a form to provide UI to the user with two input fields and one output field which should display operation result.
So the easiest solution would be:
Implement the Kalkulator class which exposes two parm methods to
set up the operands and four methods which execute the
operation and return the result: add, subtract, multiply and divide.
Create a private instance of the Kalkulator class in your form,
initialize it, set up operands when user clicks one of the buttons,
call appropriate method to run the operation and output the result
on the form field.
So supposing that operands are integer values (for demonstrative purpose) your TRN_Kalkulator may look something like this:
class TRN_Kalkulator
{
private int value1;
private int value2;
public int parmValue1(int _value = value1)
{
value1 = _value;
return value1;
}
public int parmValue2(int _value = value2)
{
value2 = _value;
return value2;
}
public int Sum()
{
return value1 + value2;
}
public int Diff()
{
return value1 - value2;
}
public int Mult()
{
return value1 * value2;
}
public int Div()
{
return value2 == 0 ? 0 : value1 / value2;
}
}
In the class declaration on the form you have to declare a private instance of TRN_Kalkulator which will be initialized by overriding the init() method:
TRN_Kalkulator calculator;
//...
public void init()
{
super();
calculator = new TRN_Kalkulator();
}
Finally when one of the buttons is clicked you parse user input by reading the values of the form fields, set up the operands, run the operation and output the result. All of this is done by overriding click() method on each of the buttons:
// read text values of the textboxes and parse them to integer
int a = str2Int(TxtOperand1.text());
int b = str2Int(TxtOperand2.text());
// set up calculator operands
calculator.parmValue1(a);
calculator.parmValue2(b);
// call the operation depending on which button was clicked
int result = calculator.Sum();
// set result textbox text
TxtResult.text(int2Str(result));
Notice that there are a lot of ways to improve this code (like for example using some display and edit methods on the form) and you definitely should do it, but this implementation suits your current setup and should point you in the right direction.

dart, how to get the symbol for a specific member?

What is the most efficient way of getting the symbol for a single member of a class?
import 'dart:mirrors';
class TestClass{
void iWantThisSymbol(){}
void butNotThisOne(){}
}
/**
* I can get all the symbols and filter down but this isn't nice
*/
void main(){
var allSymbols = reflectClass(TestClass).instanceMembers.keys;
var justTheSymbolIWant = allSymbols.where((symbol) => symbol.toString().contains('iWantThisSymbol')); // this doesnt seem very efficient or maintainable
}
var justTheSymbolIWant = reflectClass(TestClass).instanceMembers[#iWantThisSymbol]
Although, to be a bit pedantic, you're not get getting a Symbol, you're using a Symbol (#iWantThisSymbol) to get a member, which in this case is a method. So I would rewrite this as:
import 'dart:mirrors';
class TestClass{
void iWantThisMethod(){}
void butNotThisOne(){}
}
void main(){
var justTheMethodIWant = reflectClass(TestClass).instanceMembers[#iWantThisMethod];
}
Also, a few things about that use of where():
If you do want to filter a list of Symbols, you don't need to them convert to a String, you can just compare symbol instances directly.
.where() returns an iterable, even if there's only one item that matches. You probably want firstWhere() which always returns a single item.
var justTheSymbolIWant = allSymbols.firstWhere((symbol) => symbol == #iWantThisSymbol);

Resources