GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Oct-2005, 22:00
jake_jeckel's Avatar
jake_jeckel jake_jeckel is offline
New Member
 
Join Date: Oct 2005
Posts: 9
jake_jeckel is on a distinguished road
Question

error: parse error before `&' token


card.h:35: error: parse error before `&' token
....what does this error mean??

All I have on line 35 is:
CPP / C++ / C Code:
string cardString(const Card& card);  // prototype for cardString function


and since it made this error...i just put // in front of line 35

and in my card.cpp file (where i show the actual code for this cardString) i've written the code for this function

Then once I go to compile and link these files to run it...i seem to be able to use this function...which shouldn't happen because I commented out the prototype for it.

so now i'm really baffled...any ideas?

(and as a note...i have:
CPP / C++ / C Code:
struct Card {
  unsigned suit;
  unsigned rank;
};

const int numOfCards = 52;
typedef Card cardDeck[numOfCards];
in my header file as well...just so you are clear about what my argument for the function is about)
  #2  
Old 03-Oct-2005, 07:31
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error: parse error before `&' token


hi..
the problem here is in Card&.
i think that & should not come in function prototype...
you may use & while invoking function.
not for function declaration...
  #3  
Old 03-Oct-2005, 07:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error: parse error before `&' token


Quote:
Originally Posted by jake_jeckel
card.h:35: error: parse error before `&' token
....what does this error mean??
You have to show the code if you want meaningful help.

Think about it: Help might be more meaningful if I know:

1. Is this C or C++? I assume C++, but I shouldn't have to guess.
2. Where do you define the function? Where do you call the function?
3. What compiler?


In general, if a function is used (invoked) in a file before it it defined (that is, the code for calling function appears before the code for the called function) a prototype is required so that the compiler can know the nature of the function.

If a function is defined before the function is called (that is, the function definition --- the code itself --- appears in a file before the function is called) the function prototype is not required. It's OK to have a prototype, if you want to put one in, but the prototype must show exactly the same type and the same number of arguments and their types as the function definition.

If your file contains the following code, the compiler will give an error

CPP / C++ / C Code:
int main()
{
  int y, z;
  z = 10;
  y = f(z); // an error, since the compiler knows nothing about f()
  return 0;
}

int f(int x)
{
  return x*x/19;
}

Two ways to fix it

1. define the function before it is used (put the function's code first)

CPP / C++ / C Code:
int f(int x)
{
  return x*x/19;
}

int main()
{
  int y, z;
  z = 10;
  y = f(z); // compiler knows the type and the number and type of argument(s) of f()
  return 0;
}

2. Put a prototype of the function somewhere before it is called:
CPP / C++ / C Code:
int main()
{
  int f(int x); // the prototype of the function (note the semicolon)

  int y, z;
  z = 10;
  y = f(z); // compiler knows the type and the number and type of argument(s) of f()
  return 0;
}

int f(int x) // the definition of the function (no semicolon here, the code follows)
{
  return x*x/19;
}


Regards,

Dave
  #4  
Old 03-Oct-2005, 08:19
jake_jeckel's Avatar
jake_jeckel jake_jeckel is offline
New Member
 
Join Date: Oct 2005
Posts: 9
jake_jeckel is on a distinguished road

Re: error: parse error before `&' token


Paramesh: I checked up in my books/notes and to pass it by reference, It says I'm supposed to include the & in the function prototype.

And Dave...sorry bout not including some key info
1) yes it's in c++
2) okay I prototype in a card.h header file...i define the function in a card.cpp file (with #include "card.h" at the top)...and I actually use the function in another file (say testing.cpp)...I can't remember offhand what I included in that file but I'm not sure why this function (out of like 6) is the only one causing an error. It must have something to do with it being a string returning function because when I change it to a void function, it asks for the prototype but when i use string again, it says that parse error. So I'm not sure what's wrong...I might just delete the code and try starting it over. All my other functions are void and bool and they work perfectly for me.

and 3) i use the g++ compiler...i actually compile like
g++ -c -Wall -pedantic cards.cpp
g++ -c -Wall -pedantic testing.cpp
g++ cards.o testing.o -o program

(i still don't really know what -Wall and -pedantic does but i'm required to compile as such)

I don't have any time this morning to check my testing.cpp file and re-do my function but I'll get back to ya later this afternoon.
  #5  
Old 03-Oct-2005, 08:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error: parse error before `&' token


Quote:
Originally Posted by jake_jeckel
I prototype in a card.h header file...i define the function in a card.cpp file (with #include "card.h" at the top)...and I actually use the function in another file (say testing.cpp)...I can't remember offhand what I included in that file
And we can help you --- how?????

Quote:
Originally Posted by jake_jeckel
(i still don't really know what -Wall and -pedantic does but i'm required to compile as such)

These command line switches are used to make the compiler print out warnings that are not actually fatal compiler errors, but may indicate something that you need to fix. The default compiler settings (if you don't use -Wall and -pedantic) make the compiler somewhat less verbose. I think it's a good idea to make the compiler tell you every little thing that it can about your code.

Regards,

Dave
  #6  
Old 03-Oct-2005, 09:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error: parse error before `&' token


Quote:
Originally Posted by Paramesh
hi..
the problem here is in Card&.
i think that & should not come in function prototype...
you may use & while invoking function.
not for function declaration...

That's why he has to tell us more.

If it is C++, then a function argument can be a reference. (So the ampersand is used in the function prototype and definition but not when it's called.)


CPP / C++ / C Code:
// illustration of C++ call by reference and call with a pointer
//
#include <iostream>

using namespace std;

int main()
{
  void incrementr(int &);
  void incrementp(int *);

  int x, y;

  x = 39;
  
  cout << "Before calling incrementr, x = " << x << endl;
  incrementr(x);
  cout << "After  calling incrementr, x = " << x << endl << endl;

  cout << "Before calling incrementp, x = " << x << endl;
  incrementp(&x);
  cout << "After  calling incrementp, x = " << x << endl;

  return 0;
}

// function argument is reference
//
void incrementr(int &zzz)
{
  zzz = zzz + 1;
}

// function argument is pointer
//
void incrementp(int *zzz)
{
  *zzz = *zzz + 1;
}

In C, of course, there is no "call by reference", but you can use the pointer method to allow changes inside the function to affect values of variables in the calling function.

Regards,

Dave

P.S. The error message reported by the Original Poster is, indeed, what I would expect from a C compiler if he used the call-by-reference. Perhaps one of his files was named "something.c" and the compiler, therefore, thought it was a C program. So your comment is consistent with this case. Commonly available compilers for PCs by default treat files named "something.c" as C language, and files named "something.cpp" as C++ files.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with enums and function program rho C++ Forum 8 27-Jun-2005 19:13
Replacing N'th token in a file. kobi_hikri C Programming Language 2 24-Jun-2005 02:00
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 11:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:48.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.