无效的使用:: - C ++禁止使用不带类型的向量

时间:2012-06-07 21:34:44

标签: c++ vector iterator

我偶然发现了一个对我没用的编译错误。所以,当然,我转向StackOverflow .....

系统

Linux(Ubuntu 10.04 32位,gcc(Ubuntu 4.4.3-4ubuntu5.1)4.4.3,Eclipse Indigo

**问题**

当然是明显的问题;什么是gcc抱怨?

背景

我有一个类ThreadTime,用自己的cpp和h文件,ThreadTime.cpp和.h编码,并在自己的命名空间THREAD_TIME中编码;

在main()中,我启动了两个线程,然后调用ThreadA和ThreadB,然后进入一个无限循环,休眠2秒,然后检查ThreadA和ThreadB是否仍在运行。特别是,如果ThreadA已经停止,它将重新启动。

每个线程都会创建一个关联的ThreadTime对象。在我的两个线程中都有循环。在每个循环期间,ThreadTimer都会更新(在<2秒内),因此main()循环可以看到时间在线程中前进。

在创建每个ThreadTimer时,其地址将添加到类型为std::vector<ThreadTime*>的全局向量g_CthreadVector中。 g_CthreadVector是ThreadType.cpp中的文件全局。

最好只在这里粘贴全班。所以......

ThreadTime.cpp

#include "ThreadTime.

namespace THREAD_TIME
{
   std::vector<ThreadTime*>      g_CthreadVector; // threads to kill and monitor

   ThreadTime::ThreadTime(thread_t* a, time_t b, void* c)
     : m_pthread(a), m_time(b), m_lasttime(b-2), m_function(c), m_terminate (FALSE)
   {
      m_name = "a ThreadTime object";
      g_CthreadVector.push_back( this );
   }

   ThreadTime::~ThreadTime()
   {
      std::vector<ThreadTime*>::iterator     it       (GetIterator());
      if ( it != g_CthreadVector.end() )
         g_CthreadVector.erase( it );
   }

   void     ThreadTime::UpdateTime( void )
   {
      m_lasttime = m_time;                                              // save the time to lasttime
      m_time = time(NULL);                                              // store the current time
      if ( m_lasttime == m_time )                                       // safety valve keep m_time
         m_lasttime--;                                                  //     ahead of m_lasttime
   }

   //---------------------------------------------------------
   // did time get advanced?
   //---------------------------------------------------------
   bool     ThreadTime::TimeAdvanced( void )
   {
      if ( m_lasttime < m_time )       return TRUE;
      else                             return FALSE;
   }

   void     ThreadTime::print( void )
   {
      std::vector<ThreadTime*>::iterator      tt_it   (GetIterator());
      std::vector<ThreadTime*>::iterator      it      (g_CthreadVector.end());

         for ( it = g_CthreadVector.begin(); it < g_CthreadVector.end(); it++ )
         {
            if ( tt_it == it )
               std::cout << "  ################### CURRENT OBJECT ###################### " << std::endl;
            else
               std::cout << "  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ " << std::endl;

            std::cout << "  ThreadTime Object: " << (*it)->getName()    << std::endl
                      << "         thread ptr: " << (*it)->m_pthread    << std::endl
                      << "               time: " << (*it)->m_time       << std::endl
                      << "           lasttime: " << (*it)->m_lasttime   << std::endl
                      << "       function ptr: " << (*it)->m_function   << std::endl
                      << "          terminate: " << (*it)->m_terminate  << std::endl;
         }
         std::cout << "  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ " << std::endl;
   }     // for ( it ... )

   std::vector<ThreadTime*>::iterator  ThreadTime::GetIterator( void )
   {
      std::vector<ThreadTime*>::iterator      it      (g_CthreadVector.end());

      for ( it = g_CthreadVector.begin(); it < g_CthreadVector.end(); it++)
      {
         if ( (*it)->m_pthread == m_pthread )
            break;
      }

      return it;
   }

} /* namespace THREAD_TIME */

ThreadTime.h

#ifndef THREADTIME_H_
   #define THREADTIME_H_

   #include "platform.h"
   #include "opsys_common.h"
   #include "NAS_Thread.h"


   namespace THREAD_TIME
   {
      class ThreadTime;

      class ThreadTime
      {
         public:
            std::string       getName() const                  { return m_name; }
            void              setName(std::string name)        { m_name = name; }

            ThreadTime(thread_t* a, time_t b, void* c);
            ~ThreadTime();
            void     UpdateTime( void );
            bool     TimeAdvanced( void );
            void     print( void );

         private:
            std::vector<ThreadTime*>::iterator       GetIterator( void );

            std::string    m_name;
            thread_t*      m_pthread;
            time_t         m_time;
            time_t         m_lasttime;
            void*          m_function;
            bool           m_terminate;
   };

} /* namespace THREAD_TIME */

#endif /* THREADTIME_H_ */

最后,这是错误消息:

  

‘;’令牌ThreadTime.h第37行之前预期‘<’

     

无效使用‘::’ ThreadTime.h第37行

     

ISO C ++禁止声明‘vector’没有类型ThreadTime.h第37行

1 个答案:

答案 0 :(得分:11)

您的课程范围内没有std::vector的声明。您需要#include <vector>