Accessing Class Through Template

Hey all, it's my first time working with templates and I'm running in to some weird stuff. On top of that, I think my compiler is screwing with me because it's giving output for cout statements I've removed.

I'm making a queue-type program that keeps track of tasks and figures out an order to do them in. I have a normal Task class and a templated Scheduler class. Inside the Scheduler class I'm trying to access parts of a Task but it either prints an address or says it cant find the part.

main
1
2
3
Task t1("low", 25, "3rd");
Scheduler<Task> sch(0,50);
sch.schedule(t1);


task.h
1
2
3
4
5
6
7
8
9
10
11
class Task{   
    string priority_; 
    int arrival_; 
    int duration_; 
    string info_; 
public:
    Task(); //Default constructor
    Task(string, int, string); //Constructor for tasks in main
    friend ostream& operator<<(ostream&, const Task &); //Allows to print Task 
    friend class Scheduler <Task>; //Gives friend status to scheduler class.
};


task.cpp
1
2
3
4
5
6
7
8
9
10
11
Task::Task(){ //Default construtor
    priority_ = "empty";
    duration_ = 0;
    info_ = "";
}

Task::Task(string p, int d, string i){ //Constructor for given info
    p = priority_;
    d = duration_;
    i = info_;
}


scheduler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef SCHEDULER_H
#define	SCHEDULER_H

template<typename T> //In this case, T is a Task class.
class Scheduler;


template <typename T> //In this case, T is a Task class.
class Scheduler{
 private:
   deque<T> low_; //Low priority queue
   deque<T> high_; //High priority queue
   int time_; //Increases based on time it took to complete task
   int late_; //Amount of time Task can be in queue before considered late
 public:   
   Scheduler();
   Scheduler(int, int);
   void schedule(T &);
    
};


template<typename T> 
Scheduler<T>::Scheduler(){ //Default scheduler constructor
    
    time_ = 0;
    late_ = 0;  
}


template<typename T>
Scheduler<T>::Scheduler(int time, int late){ //Scheduler constructor
    
    time_ = time;
    late_ = late;
}


template<typename T>
void Scheduler<T>::schedule(T &task){ //ISSUES IN HERE SOMEWHERE
 
    cout << this->time_ << endl; //Correctly prints the 0 & 50, though output continues on future runs even if these couts are deleted, which is REALLY weird to me. 

    cout << this->late_ << endl;
    cout << task.duration_;   //Duration for the task will not print, gives me a random address or fails. Should be printing a 25 as declared in main earlier.
}
#endif	/* SCHEDULER_H */ 


Any suggestions/help/pointers greatly appreciated! Thanks in advance guys/gals.
Last edited on
Bump (Page 4)
Bump

Would probably help if the constructor for Task was set up properly... *sigh*
Last edited on
Re-build the project (clean, build). It shouldn't give you that anymore.

@ratham: the constructors are fine.
Last edited on
Ratham wrote:
Would probably help if the constructor for Task was set up properly... *sigh*

Does that mean that you corrected this:
1
2
3
4
5
Task::Task(string p, int d, string i){ //Constructor for given info
    p = priority_;
    d = duration_;
    i = info_;
}


I get an error on line 10 in task.h:
friend class Scheduler <Task>;

The following compiles:
template <typename T> friend class Scheduler;
Last edited on
Topic archived. No new replies allowed.