Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Stuck on another C/C++ error

  • 06-05-2010 11:13AM
    #1
    Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭


    I'm still trying to port C++ from a Windows platform to Linux based Android (Posix).

    The windows code uses a Queue or First in last out data structure.

    In the Android build enviroment only the very basic c libraries are supplied so I cant use a list or a stack or a queue or a dequeue or anything helpful at all.

    So after an internet search and some tutorial browsing I have come up with the following header and c file.

    //queue.h
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int ElementType;
    
    #ifndef _Queue_h
    
      struct QueueRecord;
      typedef struct QueueRecord *Queue;
    
      int         IsEmpty(Queue Q);
      int         IsFull(Queue Q);
      Queue     CreateQueue(int MaxElements);
      void        DisposeQueue(Queue Q);
      void        MakeEmpty(Queue Q);
      void        Enqueue(ElementType X, Queue Q);
      ElementType Front(Queue Q);
      void        Dequeue(Queue Q);
      ElementType FrontAndDequeue(Queue Q);
    
    #endif  /* _Queue_h */
    



    //queue.c
    #include "inc/queue.h"
    #include <stdlib.h>
    
    #define MinQueueSize (5)
    
    struct QueueRecord {
      int Capacity;
      int Front;
      int Rear;
      int Size;
      ElementType *Array;
    };
    
    int IsEmpty(Queue Q) {
      return Q->Size == 0;
    }
    
    int IsFull(Queue Q) {
      return Q->Size == Q->Capacity;
    }
    
    Queue CreateQueue(int MaxElements) {
      Queue Q;
    
      if (MaxElements < MinQueueSize) {
        Error("CreateQueue Error: Queue size is too small.");
      }
    
      Q = malloc (sizeof(struct QueueRecord));
      if (Q == NULL) {
        FatalError("CreateQueue Error: Unable to allocate more memory.");
      }
    
      Q->Array = malloc( sizeof(ElementType) * MaxElements );
      if (Q->Array == NULL) {
        FatalError("CreateQueue Error: Unable to allocate more memory.");
      }
    
      Q->Capacity = MaxElements;
      MakeEmpty(Q);
    
      return Q;
    }
    
    void MakeEmpty(Queue Q) {
    
      Q->Size = 0;
      Q->Front = 1;
      Q->Rear = 0;
    
    }
    
    void DisposeQueue(Queue Q) {
      if (Q != NULL) {
        free(Q->Array);
        free(Q);
      }
    }
    
    static int Succ(int Value, Queue Q) {
      if (++Value == Q->Capacity) {
        Value = 0;
      }
      return Value;
    }
    
    void Enqueue(ElementType X, Queue Q) {
    
      if (IsFull(Q)) {
        Error("Enqueue Error: The queue is full.");
      } else {
        Q->Size++;
        Q->Rear = Succ(Q->Rear, Q);
        Q->Array[Q->Rear] = X;
      }
    
    }
    
    ElementType Front(Queue Q) {
    
      if (!IsEmpty(Q)) {
        return Q->Array[Q->Front];
      }
      Error("Front Error: The queue is empty.");
    
    
      return 0;
    
    }
    
    void Dequeue(Queue Q) {
    
      if (IsEmpty(Q)) {
        Error("Dequeue Error: The queue is empty.");
      } else {
        Q->Size--;
        Q->Front = Succ(Q->Front, Q);
      }
    
    }
    
    ElementType FrontAndDequeue(Queue Q) {
    
      ElementType X = 0;
    
      if (IsEmpty(Q)) {
        Error("FrontAndDequeue Error: The queue is empty.");
      } else {
        Q->Size--;
        X = Q->Array[Q->Front];
        Q->Front = Succ(Q->Front, Q);
      }
      return X;
    
    }
    

    The problem arises when I try to use this Queue in a C++ file.

    //RTCPConnection.cpp
    ...
    
    Queue m_InMsgs;
    
    m_InMsgs = CreateQueue(10);
    
    //The above seems to compile fine
    
    ...
    
    while( (retVal == 0) && (m_InMsgs->Size > 0)) [B]//Line 237 error[/B]
    {
    	RTCP_Packet* msg=front(m_InMsgs); [B]//Line 239 error[/B]
    	retVal = ProcessReport(msg, mask);
    	Dequeue(m_InMsgs);
    	if( msg != NULL ) delete msg;
    }
    
    ...
    
    while ( m_OutMsgs->Size > 0 && sentdata && (m_RAddrStruct.sin_port!=0) ) [B]//Line 348 error[/B]
    {
           ......
    }
    
    ...
    
    if (result==SOCKET_ERROR)
    {
    	int error= errno;
    	if ((error!=EWOULDBLOCK) || (error!=WSAEINPROGRESS))
    	{
    		m_OutMsgs.MakeEmpty(); [B]//Line 386 error[/B]
    		m_Error=error;
    	}
    	}
    	else
    	{
    		m_OutMsgs.MakeEmpty(); [B]//Line 392 error[/B]
    		sentdata=true;
    	}
    	}
    		else m_Error=-1;
    	}
    }
    

    And I get the following errors:
    apps/phone/project/jni//src/RTCPConnection.cpp: In member function 'int RTCP_Connection::ReceiveReport(UINT32&)':
    apps/phone/project/jni//src/RTCPConnection.cpp:237: error: invalid use of
    incomplete type 'struct QueueRecord'
    apps/phone/project/jni/inc/queue.h:11: error: forward declaration of 'struct QueueRecord'
    apps/phone/project/jni//src/RTCPConnection.cpp:239: error: invalid conversion from 'ElementType' to 'RTCP_Packet*'
    apps/phone/project/jni//src/RTCPConnection.cpp: In member function 'int RTCP_Connection::SendData()':
    apps/phone/project/jni//src/RTCPConnection.cpp:348: error: invalid use of
    incomplete type 'struct QueueRecord'
    apps/phone/project/jni/inc/queue.h:11: error: forward declaration of 'struct QueueRecord'
    apps/phone/project/jni//src/RTCPConnection.cpp:386: error: request for member 'MakeEmpty' in '((RTCP_Connection*)this)->RTCP_Connection::m_OutMsgs', whic
    h is of non-class type 'QueueRecord*'
    apps/phone/project/jni//src/RTCPConnection.cpp:392: error: request for member 'MakeEmpty' in '((RTCP_Connection*)this)->RTCP_Connection::m_OutMsgs', which is of non-class type 'QueueRecord*'
    make: *** [out/apps/phone/armeabi/objs/audiodatastream//src/RTCPConnectio
    n.o] Error 1

    I've been googling the errors but cant quite get my head around what they mean.

    Can anybody help?


Comments

  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    The queue you're using is written in C, but you're trying to treat it like a C++ class.

    It's not m_Queue.MakeEmpty(), it's MakeEmpty(&m_Queue);

    Look at the header file for the queue!

    Also, you might run into C++ name mangling issues here.


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    As the other poster mentioned about mangling issues, so you may have to specify C linkage to stop the C++ compiler mangling the Queue library for some (if not all) declarations in your queue header file.


Advertisement