Pieter Pieter - 28 days ago 5
C++ Question

changing variables in another class

I have 2 classes (Project and Task) and a main. A Project has different Tasks. At some point in a method of 'Project' I need to change the starttime of a specific task.

task.h:

#pragma once
#include <vector>

using namespace std;

class Task
{

private:
//Info:

int StartTime_Solo;

public:

Task(); //constructor

void SetStartTime_Solo(int st_s);

int GetStartTime_Solo();

};


task.cpp:

#include "Task.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;


Task::Task()
{
//constructor
StartTime_Solo = 0;
}

int Task::GetStartTime_Solo()
{
return StartTime_Solo;
}

void Task::SetStartTime_Solo(int st_s)
{
StartTime_Solo = st_s;
}


Project.h:

#pragma once
#include "Task.h"
#include <vector>

using namespace std;

class Project
{
private:
vector<Task> Tasks;
public:

Project::Project(int readinnumber);//constructor
void IndividScheduling(Project pr);

vector<Task>& GetTasks();
};


Project.cpp:

#include "Project.h"
#include <string>
#include <vector>
Project::Project(int inleesgetal)
{

//constructor
Tasks.resize(Numbertasks);
}

vector<Task>& Project::GetTasks()
{
return Tasks;
}
void Project::IndividScheduling(Project pr)
{
//code...
pr.GetTasks()[CurrentTask].SetStartTime_Solo(time);
}


main:

#include <iostream>
#include <vector>

#include "Task.h"
#include "Project.h"

using namespace std;


int main()
{
Project Project1(6);
Project1.IndividScheduling(Project1);
for (int j = 0; j < Project1.GetTasks().size(); j++)
{
cout << "starttime task " << j + 1 << "is:" << Project1.GetTasks()[j].GetStartTime_Solo() << "\n";
}
}


Now the question: When I run the program it still says starttime task x is: 0 (as in the constructor).
How can I make sure it gives me the right starttime? But later on I'll have to do the same for other projects. So basically how can I store the newly calculated value by the method InidivdScheduling of the starttime so that my program 'links' it to the right Project ?
Also, when I tried to access the starttime in the Project class then it would give me the right value.

Thanks a lot!

Also, another (small) question.
In the main I have the line :

Project1.IndividScheduling(Project1);


I find it a bit odd that I have to write 'Project1' two times. Is this the right way to do it? I have to use the method IndividScheduling with arguments coming from Project1.

Extra:
In Project.cpp I have the line

pr.GetTasks()[CurrentTask].SetStartTime_Solo(time);


So this line will change the starttime of the 'CurrentTask' to 'time'. But when I go in the main and do this:

Project1.GetTasks()[j].GetStartTime_Solo();


It still gives me the value created by the constructor (for the jth+1 task), while I did change it already with the 'SetStartTime' method above. I hope this makes it a bit more clear?

Answer
void Project::IndividScheduling(Project pr)
{
//code...
pr.GetTasks()[CurrentTask].SetStartTime_Solo(time);
}

The code above makes a copy of the project class So this will result in that you are not changing the tasks of your Project1, you are changing a the tasks of a copy of Project1. And why do you give Project1 as parameter into IndividScheduleing? It doesn't seem to be a static method so following code should work

void Project::IndividScheduling()
{
//code...
GetTasks()[CurrentTask].SetStartTime_Solo(time);
}
Comments