Forward declaration and template function error

I currently have a nasty problem with the direct declaration and template function. I tried to work and make some changes, but so far nothing has worked. The following is a snippet of code:

class TaskScheduler; --> //forward declaration of ‘struct TaskScheduler’
//
//

class TaskEvent {
//
//
};

class HostTask {
//
//
};

template<class T> inline HostTask*
findT(TaskScheduler* tss, T* e)
{
    map<int, HostTask*>::iterator it;
    bool bEq = false;
    for(it = tss->tasks_.begin(); it != tss->tasks_.end(); it++) { --> //error: invalid use of incomplete type ‘struct TaskScheduler’
    if(dynamic_cast<TaskEvent*>(e))
        bEq = dynamic_cast<TaskEvent*>(e)->equal(it->second->ev_);
    else if(dynamic_cast<HostTask*>(e))
        bEq = dynamic_cast<HostTask*>(e)->equal(it->second);
    if(bEq) {
        return it->second;
    }
}
return NULL;
}
//

//class TaskScheduler definition
class TaskScheduler : virtual public HCIEventsHandler {
friend HostTask* findT<TaskEvent>(TaskScheduler* tss, TaskEvent* e); //findT function is used here
//
//
};

Here is the error message that I have, which is shown in the code:. / bt -taskscheduler.h: 159: error: forward declare 'struct TaskScheduler. / bt -taskscheduler.h: 229: error: invalid use of incomplete type 'struct TaskScheduler

Can someone show me what is going on in this code? Any help is appreciated.

+3
source share
5 answers

findT tss->tasks_, TaskScheduler, , , .

struct TaskScheduler findT.

+5

TaskScheduler for-loop "tss- > tasks_.begin()". , "tasks_" .

, , , . ( ) . "" ( ) .

+1

TaskScheduler findT, :

  • TaskScheduler findT
  • TaskScheduler findT

:

template< class U, class T> 
inline HostTask* findT( U* tss, T* e)
{
   //...
}
+1

, , , findT - scheduler: .

, friend.

, , , , findT -.

, -. .

//class TaskScheduler definition
class TaskScheduler : virtual public HCIEventsHandler {
 public:
  template<class T> inline HostTask* findT(T* e) const
  {
    map<int, HostTask*>::iterator it;
    bool bEq = false;
    for(it = tasks_.begin(); it != tasks_.end(); it++) { 
       if(dynamic_cast<TaskEvent*>(e))
          bEq = dynamic_cast<TaskEvent*>(e)->equal(it->second->ev_);
      else if(dynamic_cast<HostTask*>(e))
          bEq = dynamic_cast<HostTask*>(e)->equal(it->second);
      if(bEq) {
          return it->second;
      }
    }
    return NULL;
  }


};
+1

, TaskScheduler , , .

, , , , , ( , MSVC , , *). , .

loopkup - , , , . , , . , , . - , - , . . , , ; , , , , .

:

 void foo (int);
 template <typename T> void bar(T t) {
   foo(1.0);
   foo(t);
 }
 void foo (double);

 struct qux {};
 void foo (qux);

 void baz () {
   bar (1.0);
   qux q;
   bar (q);
 }

N.B. , . , qux last .

bar foo. , , . foo (int), , . ++. , . baz bar<double>, bar<qux>. bar foo T. double, -, bar foo(int). qux, , , **, foo(qux).

, . , , ; . , .

* MSVC , , . , , .

** In almost every case, an instantiation context includes every declaration that has a definition context. However, the keyword exportcan lead to the fact that this is not so. This keyword is implemented in only one compiler interface - I wonder why no one implemented it? [/Sarcasm]

0
source

Source: https://habr.com/ru/post/1714854/


All Articles