子牛 发表于 2012-6-11 17:37:59

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

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ace/Malloc.h"
#include "ace/OS.h"
#include "ace/ACE.h"
#include "ace/Malloc_T.h"
#include "ace/Synch_Traits.h "
//#include "ace/Cached_Allocator.h"
//#include "ace/SYNCH_MUTEX.h"

//A chunk of size 1K is created. In our case we decided to use a simple array

//as the type for the chunk. Instead of this we could use any struct or class

//that we think is appropriate.

typedef charMEMORY_BLOCK;
//Create an ACE_Cached_Allocator which is passed in the type of the

//“chunk” that it must pre-allocate and assign on the free list.

// Since the Cached_Allocator is a template class we can pretty much

//pass it ANY type we think is appropriate to be a memory block.

typedef ACE_Cached_Allocator<MEMORY_BLOCK,ACE_SYNCH_MUTEX> Allocator;


class MessageManager

{

public:

        //The constructor is passed the number of chunks that the allocator

        //should pre-allocate and maintain on its free list.

        MessageManager(int n_blocks):

          allocator_(n_blocks),message_count_(0)

          {

                  mesg_array_=new char*;

          }

          

          //Allocate memory for a message using the Allocator. Remember the message

          //in an array and then increase the message count of valid messages

          //on the message array.

          void allocate_msg(const char *msg)

          {

                  mesg_array_=(char*)allocator_.malloc(ACE_OS::strlen(msg)+1);

                  ACE_OS::strcpy(mesg_array_,msg);

                  message_count_++;

          }

          

          //Free all the memory that was allocated. This will cause the chunks

          //to be returned to the allocator’s internal free list

          //and NOT to the OS.

          void free_all_msg()

          {

                  for(int i=0;i<message_count_;i++)

                          allocator_.free(mesg_array_);

                  

                  message_count_=0;

          }

          

          //Just show all the currently allocated messages in the message array.

          void display_all_msg()

          {

                  for(int i=0;i<message_count_;i++)

                          ACE_OS::printf("%s\n",mesg_array_);

          }

          

private:

        char **mesg_array_;

        Allocator allocator_;

        int message_count_;

};

char message;

int main(int argc, char* argv[])

{

        if(argc<2)

        {

                ACE_DEBUG((LM_DEBUG, "Usage: %s <Number of blocks>\n", argv));

                exit(1);

        }

       

        int n_blocks=ACE_OS::atoi(argv);

       

        //Instantiate the Memory Manager class and pass in the number of blocks

        //you want on the internal free list.

        MessageManager mm(n_blocks);

       

        //Use the Memory Manager class to assign messages and free them.

        //Run this in your favorite debug environment and you will notice that the

        //amount of memory your program uses after Memory Manager has been

        //instantiated remains the same. That means the Cached Allocator

        //controls or manages all the memory for the application.

        //Do forever.

        while(1)

        {

                //allocate the messages somewhere

                ACE_DEBUG((LM_DEBUG,"\n\n\nAllocating Messages\n"));

                for(int i=0; i<n_blocks;i++){

                        ACE_OS::sprintf(message,"Message %d: Hi There",i);

                        mm.allocate_msg(message);

                }

               

                //show the messages

                ACE_DEBUG((LM_DEBUG,"Displaying the messages\n"));

                ACE_OS::sleep(2);

                mm.display_all_msg();

               

                //free up the memory for the messages.

                ACE_DEBUG((LM_DEBUG,"Releasing Messages\n"));

                ACE_OS::sleep(2);

                mm.free_all_msg();

        }

       

        return 0;

}


winston 发表于 2012-6-11 20:10:47

问题是啥?细节!大家有几个人有时间帮你调试程序啊

子牛 发表于 2012-6-12 14:53:00

1>------ Build started: Project: test1, Configuration: Debug Win32 ------
1>Compiling...
1>test1.cpp
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'
1>      with
1>      [
1>            T=ACE_Cached_Mem_Pool_Node<MEMORY_BLOCK >,
1>            ACE_LOCK=ACE_MT_SYNCH::MUTEX
1>      ]
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
1>      with
1>      [
1>            T=ACE_Cached_Mem_Pool_Node<MEMORY_BLOCK >,
1>            ACE_LOCK=ACE_MT_SYNCH::MUTEX
1>      ]
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
1>      with
1>      [
1>            T=MEMORY_BLOCK,
1>            ACE_LOCK=ACE_MT_SYNCH::MUTEX
1>      ]
1>Build log was saved at "file://e:\ACE-6.1.0\ACE_TEST\test1\Debug\BuildLog.htm"
1>test1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

子牛 发表于 2012-6-12 14:53:27

这是什么错误啊,是不是MEMORY_BLOCK要搞成类

winston 发表于 2012-6-12 19:08:46

include ACE_Thread_Mutex 这个类的头文件。
页: [1]
查看完整版本: 网上的例子编译通不过,求帮助