找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4644|回复: 4

网上的例子编译通不过,求帮助

[复制链接]
发表于 2012-6-11 17:37:59 | 显示全部楼层 |阅读模式
  1. // test1.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "ace/Malloc.h"
  5. #include "ace/OS.h"
  6. #include "ace/ACE.h"
  7. #include "ace/Malloc_T.h"
  8. #include "ace/Synch_Traits.h "
  9. //#include "ace/Cached_Allocator.h"
  10. //#include "ace/SYNCH_MUTEX.h"
  11. //A chunk of size 1K is created. In our case we decided to use a simple array
  12. //as the type for the chunk. Instead of this we could use any struct or class
  13. //that we think is appropriate.
  14. typedef char  MEMORY_BLOCK[1024];
  15. //Create an ACE_Cached_Allocator which is passed in the type of the
  16. //“chunk” that it must pre-allocate and assign on the free list.
  17. // Since the Cached_Allocator is a template class we can pretty much
  18. //pass it ANY type we think is appropriate to be a memory block.
  19. typedef ACE_Cached_Allocator<MEMORY_BLOCK,ACE_SYNCH_MUTEX> Allocator;
  20. class MessageManager
  21. {
  22. public:
  23.         //The constructor is passed the number of chunks that the allocator
  24.         //should pre-allocate and maintain on its free list.
  25.         MessageManager(int n_blocks):
  26.           allocator_(n_blocks),message_count_(0)
  27.           {
  28.                   mesg_array_=new char*[n_blocks];
  29.           }
  30.           
  31.           //Allocate memory for a message using the Allocator. Remember the message
  32.           //in an array and then increase the message count of valid messages
  33.           //on the message array.
  34.           void allocate_msg(const char *msg)
  35.           {
  36.                   mesg_array_[message_count_]=(char*)allocator_.malloc(ACE_OS::strlen(msg)+1);
  37.                   ACE_OS::strcpy(mesg_array_[message_count_],msg);
  38.                   message_count_++;
  39.           }
  40.           
  41.           //Free all the memory that was allocated. This will cause the chunks
  42.           //to be returned to the allocator’s internal free list
  43.           //and NOT to the OS.
  44.           void free_all_msg()
  45.           {
  46.                   for(int i=0;i<message_count_;i++)
  47.                           allocator_.free(mesg_array_[i]);
  48.                   
  49.                   message_count_=0;
  50.           }
  51.           
  52.           //Just show all the currently allocated messages in the message array.
  53.           void display_all_msg()
  54.           {
  55.                   for(int i=0;i<message_count_;i++)
  56.                           ACE_OS::printf("%s\n",mesg_array_[i]);
  57.           }
  58.           
  59. private:
  60.         char **mesg_array_;
  61.         Allocator allocator_;
  62.         int message_count_;
  63. };
  64. char message[100];
  65. int main(int argc, char* argv[])
  66. {
  67.         if(argc<2)
  68.         {
  69.                 ACE_DEBUG((LM_DEBUG, "Usage: %s <Number of blocks>\n", argv[0]));
  70.                 exit(1);
  71.         }
  72.        
  73.         int n_blocks=ACE_OS::atoi(argv[1]);
  74.        
  75.         //Instantiate the Memory Manager class and pass in the number of blocks
  76.         //you want on the internal free list.
  77.         MessageManager mm(n_blocks);
  78.        
  79.         //Use the Memory Manager class to assign messages and free them.
  80.         //Run this in your favorite debug environment and you will notice that the
  81.         //amount of memory your program uses after Memory Manager has been
  82.         //instantiated remains the same. That means the Cached Allocator
  83.         //controls or manages all the memory for the application.
  84.         //Do forever.
  85.         while(1)
  86.         {
  87.                 //allocate the messages somewhere
  88.                 ACE_DEBUG((LM_DEBUG,"\n\n\nAllocating Messages\n"));
  89.                 for(int i=0; i<n_blocks;i++){
  90.                         ACE_OS::sprintf(message,"Message %d: Hi There",i);
  91.                         mm.allocate_msg(message);
  92.                 }
  93.                
  94.                 //show the messages
  95.                 ACE_DEBUG((LM_DEBUG,"Displaying the messages\n"));
  96.                 ACE_OS::sleep(2);
  97.                 mm.display_all_msg();
  98.                
  99.                 //free up the memory for the messages.
  100.                 ACE_DEBUG((LM_DEBUG,"Releasing Messages\n"));
  101.                 ACE_OS::sleep(2);
  102.                 mm.free_all_msg();
  103.         }
  104.        
  105.         return 0;
  106. }
复制代码
发表于 2012-6-11 20:10:47 | 显示全部楼层
问题是啥?细节!大家有几个人有时间帮你调试程序啊
 楼主| 发表于 2012-6-12 14:53:00 | 显示全部楼层
  1. 1>------ Build started: Project: test1, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>test1.cpp
  4. 1>e:\ace-6.1.0\ace_wrappers\ace\free_list.h(131) : error C2079: 'ACE_Locked_Free_List<T,ACE_LOCK>::mutex_' uses undefined class 'ACE_Thread_Mutex'
  5. 1>        with
  6. 1>        [
  7. 1>            T=ACE_Cached_Mem_Pool_Node<MEMORY_BLOCK >,
  8. 1>            ACE_LOCK=ACE_MT_SYNCH::MUTEX
  9. 1>        ]
  10. 1>        e:\ace-6.1.0\ace_wrappers\ace\malloc_t.h(130) : see reference to class template instantiation 'ACE_Locked_Free_List<T,ACE_LOCK>' being compiled
  11. 1>        with
  12. 1>        [
  13. 1>            T=ACE_Cached_Mem_Pool_Node<MEMORY_BLOCK >,
  14. 1>            ACE_LOCK=ACE_MT_SYNCH::MUTEX
  15. 1>        ]
  16. 1>        e:\ace-6.1.0\ace_test\test1\test1.cpp(113) : see reference to class template instantiation 'ACE_Cached_Allocator<T,ACE_LOCK>' being compiled
  17. 1>        with
  18. 1>        [
  19. 1>            T=MEMORY_BLOCK,
  20. 1>            ACE_LOCK=ACE_MT_SYNCH::MUTEX
  21. 1>        ]
  22. 1>Build log was saved at "file://e:\ACE-6.1.0\ACE_TEST\test1\Debug\BuildLog.htm"
  23. 1>test1 - 1 error(s), 0 warning(s)
  24. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
复制代码
 楼主| 发表于 2012-6-12 14:53:27 | 显示全部楼层
这是什么错误啊,是不是MEMORY_BLOCK要搞成类
发表于 2012-6-12 19:08:46 | 显示全部楼层
include ACE_Thread_Mutex 这个类的头文件。
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 06:27 , Processed in 0.024955 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表