locked
How Do I Declare This Struct In a funtion Prototype RRS feed

  • Question

  • Hi,

    I am getting errors about declaring a struct in a function prototype. Here are the errors:

    Error	7	error C2146: syntax error : missing ')' before identifier 'sProbability'	C:\mvc++2010e\cardShuffler.cpp\cardShuffler.cpp\printCards.cpp	4
    

    and

    Error	8	error C2059: syntax error : ')'	C:\mvc++2010e\cardShuffler.cpp\cardShuffler.cpp\printCards.cpp	4
    

    here is the header for that funtion

    #ifndef PRINT_CARDS
    #define PRINT_CARDS
    
    int printCards(Probability sProbability, double totalCards);
    
    #endif

    here is the function call in my main function

    printCards(sProbCards, totalCards);

    here is the function prototype

    int printCards(Probability sProbability, double totalCards)

    the red error lines are under the lines that I have underlined.

    Thank you for any help

    Sunday, June 3, 2012 9:25 PM

Answers

  • First, move the #include "stdafx.h" to the top of the cpp file, not the h file. (That line needs to be at the top of all cpp files.)

    Next, remove the semicolon at the end of struct Probability;

    And then add the function prototype below the struct declaration.

    Monday, June 4, 2012 12:49 PM
  • >Is that better for the header?:

    >#ifndef PROBABILITY
    >#define PROBABILITY

    >typedef double probability;

    >struct Probability;
    >{

    Again, NO.

    Why have you still not made the fix that Scott described?

    You appear to have misunderstood *which* semicolon
    should be removed. You removed the one at the end of
    the struct definition after the closing brace. Put it
    back, and remove the one at the end *of this line*:

    >struct Probability; // <<<=====
    >{

    - Wayne
    • Proposed as answer by Elegentin Xie Tuesday, June 5, 2012 7:42 AM
    • Marked as answer by Elegentin Xie Friday, June 15, 2012 5:46 AM
    Tuesday, June 5, 2012 3:04 AM
  • Using standard vocabulary in answers would help.

    When you say

    struct Probabiliy; // note semicolon

    you are DECLARING the name "Probability" but not DEFINING it.

    Similarly, you DECLARE a function by giving its "prototype":

    double someFunc(Probability p); // note semicolon

    Both of these kinds of statements are valid at both file (or namespace) scope and inside a function, but usually they should appear only at file scope and in a header if you are developing a program with more than one source file.

    Now, the specific combination of declarations I've shown above is NOT valid.  The reason is that the function is declared to take as an argument a structure BY VALUE and the DEFINITION of that structure is not in scope at the point of the function declaration.

    Passing the structure BY REFERENCE (& or * decorator) should fix the specific issue.

    The other issue here is that you are ATTEMPTING to write a DEFINITION on Probability in your header file but because you put a semicolon where it doesn't belong (between the structure tag name and the opening brace of the definition body) the compiler sees it as a DECLARATION followed by an invalid brace.


    Stephen W. Nuchia StatSoft, Inc. Tulsa, Oklahoma USA

    • Proposed as answer by Elegentin Xie Tuesday, June 12, 2012 2:54 AM
    • Marked as answer by Elegentin Xie Friday, June 15, 2012 5:46 AM
    Tuesday, June 5, 2012 2:06 PM

All replies

  • Probability must be defined before you can declare int printCards(Probability sProbability, double totalCards); because the function declaration uses Probability.

    This could be solved by having your print cards .h file include your probability.h file.

    (Which language do you already know, if you're new to C++, by the way?  That might help me fine tune my answers for you.)

    Sunday, June 3, 2012 9:31 PM
  • I am new to c++.

    I don't know any other programming languages.

    Is probability.h where I declare the struct Probability?

    Sunday, June 3, 2012 9:43 PM
  • I am new to c++.

    I don't know any other programming languages.

    Is probability.h where I declare the struct Probability?

    It can go in any arbitrary header file of your choosing. If Probability is only used in one *.cpp file, then it simply could be placed near the beginning of that file before the function definitions.
    Sunday, June 3, 2012 11:49 PM
  • I have made my header file, but the error is still there.
    Monday, June 4, 2012 2:32 AM
  • You haven't shown us the declaration of struct Probability and you haven't explained where it is declared. The problem is that it must be declared before the compiler encounters its use in the function prototype. Put it in the Probability.h files, before the function prototype.

    Monday, June 4, 2012 2:49 AM
  • This is what I already had in the header file:

    #include "stdafx.h"
    
    #ifndef PROBABILITY
    #define PROBABILITY
    
    typedef double probability;
    
    struct Probability;
    {
    	probability dProbOf1;
    	probability dProbOf2;
    	probability dProbOf3;
    	probability dProbOf4;
    	probability dProbOf5;
    	probability dProbOf6;
    	probability dProbOf7;
    	probability dProbOf8;
    	probability dProbOf9;
    	probability dProbOf10;
    	probability dProbOf11;
    	probability dProbOf12;
    	probability dProbOf13;
    	probability dProbOf14;
    	probability dProbOf15;
    };
    
    #endif

    Monday, June 4, 2012 12:19 PM
  • First, move the #include "stdafx.h" to the top of the cpp file, not the h file. (That line needs to be at the top of all cpp files.)

    Next, remove the semicolon at the end of struct Probability;

    And then add the function prototype below the struct declaration.

    Monday, June 4, 2012 12:49 PM
  • Is this how I would write the header file?:

    #include "stdafx.h"
    
    #ifndef PROBABILITY
    #define PROBABILITY
    
    typedef double probability;
    
    struct Probability;
    {
    	probability dProbOf1;
    	probability dProbOf2;
    	probability dProbOf3;
    	probability dProbOf4;
    	probability dProbOf5;
    	probability dProbOf6;
    	probability dProbOf7;
    	probability dProbOf8;
    	probability dProbOf9;
    	probability dProbOf10;
    	probability dProbOf11;
    	probability dProbOf12;
    	probability dProbOf13;
    	probability dProbOf14;
    	probability dProbOf15;
    }
    
    double doTheMath(NumberOfCards sCards, Probability sProbability);
    
    
    
    #endif

    Thanks
    Monday, June 4, 2012 10:02 PM
  • Is this how I would write the header file?:

    ...
    struct Probability;
    {
    	probability dProbOf1;
    ...
    No. Reread Scott's last post and make *all* of the
    changes he suggested.

    - Wayne

    Monday, June 4, 2012 10:08 PM
  • Is that better for the header?:

    #ifndef PROBABILITY
    #define PROBABILITY
    
    typedef double probability;
    
    struct Probability;
    {
    	probability dProbOf1;
    	probability dProbOf2;
    	probability dProbOf3;
    	probability dProbOf4;
    	probability dProbOf5;
    	probability dProbOf6;
    	probability dProbOf7;
    	probability dProbOf8;
    	probability dProbOf9;
    	probability dProbOf10;
    	probability dProbOf11;
    	probability dProbOf12;
    	probability dProbOf13;
    	probability dProbOf14;
    	probability dProbOf15;
    }
    
    double doTheMath(NumberOfCards sCards, Probability sProbability);
    
    
    
    #endif

    If that is right, I am still getting the error C2059 in the function header in the function doTheMath():

    Error	44	error C2059: syntax error : ')'	C:\mvc++2010e\cardShuffler.cpp\cardShuffler.cpp\doTheMath.cpp	4
    

    Here is the function header for doTheMath():

    double doTheMath(NumberOfCards sNumCards, Probability sProbability, double totalCards)

    I underlined where the error was in Visual C++.

    (NumberOfCards is another struct that I have done the same thing with as you told me to do with Probability)

    Thanks

    Tuesday, June 5, 2012 2:38 AM
  • >Is that better for the header?:

    >#ifndef PROBABILITY
    >#define PROBABILITY

    >typedef double probability;

    >struct Probability;
    >{

    Again, NO.

    Why have you still not made the fix that Scott described?

    You appear to have misunderstood *which* semicolon
    should be removed. You removed the one at the end of
    the struct definition after the closing brace. Put it
    back, and remove the one at the end *of this line*:

    >struct Probability; // <<<=====
    >{

    - Wayne
    • Proposed as answer by Elegentin Xie Tuesday, June 5, 2012 7:42 AM
    • Marked as answer by Elegentin Xie Friday, June 15, 2012 5:46 AM
    Tuesday, June 5, 2012 3:04 AM
  • I still am getting the error.

    I must have done something else.

    Thank you.

    Tuesday, June 5, 2012 12:38 PM
  • Using standard vocabulary in answers would help.

    When you say

    struct Probabiliy; // note semicolon

    you are DECLARING the name "Probability" but not DEFINING it.

    Similarly, you DECLARE a function by giving its "prototype":

    double someFunc(Probability p); // note semicolon

    Both of these kinds of statements are valid at both file (or namespace) scope and inside a function, but usually they should appear only at file scope and in a header if you are developing a program with more than one source file.

    Now, the specific combination of declarations I've shown above is NOT valid.  The reason is that the function is declared to take as an argument a structure BY VALUE and the DEFINITION of that structure is not in scope at the point of the function declaration.

    Passing the structure BY REFERENCE (& or * decorator) should fix the specific issue.

    The other issue here is that you are ATTEMPTING to write a DEFINITION on Probability in your header file but because you put a semicolon where it doesn't belong (between the structure tag name and the opening brace of the definition body) the compiler sees it as a DECLARATION followed by an invalid brace.


    Stephen W. Nuchia StatSoft, Inc. Tulsa, Oklahoma USA

    • Proposed as answer by Elegentin Xie Tuesday, June 12, 2012 2:54 AM
    • Marked as answer by Elegentin Xie Friday, June 15, 2012 5:46 AM
    Tuesday, June 5, 2012 2:06 PM
  • How exactly would I fix this?
    Tuesday, June 5, 2012 4:47 PM
  • Hi NJM11111011111,

    As you mentioned:

    >I still am getting the error.

    Could you please show your errorand your ultimate code? I think their replies may be ok, and I suggest you to do some reading about c++. And maybe it will be better.

    Tuesday, June 12, 2012 3:04 AM