Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   error: expected primary-expression... (http://www.programmingforums.org/showthread.php?t=9377)

rup Apr 15th, 2006 2:30 AM

error: expected primary-expression...
 
I am in Linux, using g++ to compile.

My code looks like this:

This is the header:

:

class Test
{
  //...code...
 
  public:
  void Function();
 
  //...code...
};


In a cpp file I have the declaration:

:

void Test::Function(...)
{
  //....code...
}


I got this error when compiling:
....error: expected primary-expression before ‘void’....

What does this mean.
BTW, the code fragments are highly simplified and only a part of a programming project. But the interface is similar.
Can somebody help?

DaWei Apr 15th, 2006 5:54 AM

You show an error that says, "expected primary-expression before 'void'..." Then you show us a snippet with everything before 'void' snipped out....hilarious. Incidentally, that's the definition in the cpp file, the declaration is in the class. Did you mean to have those three dots in the arg list in the definition and not up in the declaration?

grumpy Apr 15th, 2006 7:37 AM

As Dawei said, the code snippets given do not give any useful information. It is better to find a SMALL but COMPLETE example that repeatably illustrates your problem (and post it verbatim in code tags). I recommend you read the sticky threads on "How to ask a question" as well .... they give advice on how to ask a question to increase your chances of getting a useful response.

The most common reasons the compiler would expect an expression before a function declaration or definition are a missing closing brace i.e. } or a missing semi-colon (or possibly both). But, given that you haven't provided enough information, that is only a stab in the dark.

rup Apr 15th, 2006 7:48 AM

Sorry for the incomplete post, should have followed intructions.
Here's the complete code:

The header gIPC.h:
:

#ifndef __GIPC_H__
#define __GIPC_H__

#include "gMsg.h"

class MessageQueue
{
        private:
                int qid;
               
        public:
                MessageQueue();
                ~MessageQueue();
                void MsgSnd(Msg);
                void MsgRcv(Msg);
};

#endif // __GIPC_H__


The cpp, gIPC.cpp:
:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "gIPC.h"

MessageQueue::MessageQueue()
{
        qid = msgget(key_t(1234), 0666|IPC_CREAT);
}

MessageQueue::~MessageQueue()
{
        msgctl(qid,IPC_RMID,0);
}

void MessageQueue::MsgSnd(Msg m)
{
        msgsnd(qid, void*(&m), m.GetMsgSize(),0);
}

void MessageQueue::MsgRcv(Msg m)
{
        msgrcv(qid, void*(&m), BUFSIZE, m.GetMsgType(), 0);
}


This is the error:
:

rup@ubuntu:~/gui$ make
g++ -c gIPC.cpp
gIPC.cpp: In member function ‘void MessageQueue::MsgSnd(Msg)’:
gIPC.cpp:18: error: expected primary-expression before ‘void’
gIPC.cpp: In member function ‘void MessageQueue::MsgRcv(Msg)’:
gIPC.cpp:23: error: expected primary-expression before ‘void’
make: *** [gIPC.o] Error 1


InfoGeek Apr 15th, 2006 7:58 AM

:

SYSTEM CALL: msgsnd();                                                         

  PROTOTYPE: int msgsnd ( int msqid, struct msgbuf *msgp, int msgsz, int msgflg );

msgsnd function expects a struct msgbuf * as the second argument. Why are you passing a void *?

DaWei Apr 15th, 2006 8:05 AM

:

        msgsnd(qid, void*(&m), m.GetMsgSize(),0);
One doesn't declare a function's parameter type when one calls it, only when one declares or defines it. Did you perhaps mean to cast it?

rup Apr 15th, 2006 1:11 PM

I picked msgsnd from the book Beginning Linux Programming (Wrox).
It has the following prototype:
:

int msgsnd(int msqid, const void* msg_ptr, size_t msg_sz, int msgflag);
and is called this way in an example C program in the book:
:

msgsnd(msgid, (void*)&some_data, MAX_TEXT, 0);
where some_data is the structure:
:

struct msg{
    long int msg_type;
    char text[MAX_TEXT];
};


Yes, I meant to cast it.

DaWei Apr 15th, 2006 1:28 PM

So, did that clear your error message or not? Inquiring members want to know. It's the fee.

rup Apr 16th, 2006 8:26 AM

Yes, it cleared my error message.

I hade written this:
:

msgrcv(qid, void*(&m), BUFSIZE, m.GetMsgType(), 0);

Because in C++ a cast is applied this way : void(variable) I had left it that way. Making it (void*)(&m) solves the problem.

DaWei Apr 16th, 2006 8:44 AM

Excuse me? :confused:


All times are GMT -5. The time now is 11:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC