struct not yet defined...[newb]

This is a discussion on struct not yet defined...[newb] within the C++ Programming forums, part of the General Programming Boards category; I have created a file.h that contains a struct (Cards) and it compiles well, but when I create another file ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    struct not yet defined...[newb]

    I have created a file.h that contains a struct (Cards) and it compiles well, but when I create another file that includes my .h file (#include deck.h) the compiler complains about my struct is not yet defined! I'm using dev-c++ version 4, the two files are in a project.dev. Keep in mind, I'm a newb let's just say this is kind of my first program.

    code:

    Code:
    //deck.h
    
    #ifndef _CARDS_
    #define _CARDS_
    #include <iostream>
    struct Cards {
    
      private:
    
      float value; //2-14
      float colorvalue; //1=red,2=black
      float suitvalue; //1=hearts,2=diamonds,3=clubs,4=spades
    
      public:
    
      Cards(float a_value, float a_colorvalue, float a_suitvalue) {
        value = a_value; colorvalue = a_colorvalue; suitvalue = a_suitvalue;
      }
      inline void dispCard() const {
          switch(suitvalue) {
            case 1: cout << "Hearts of ";
                            break;
            case 2: cout << "Diamonds of ";
                            break;
            case 3: cout << "Clubs of ";
                            break;
            case 4: cout << "Spades of ";
                            break;
          }
    };
    
    const Cards card[52] = {Cards(2, 1, 1), Cards(3, 1, 1), Cards(4, 1, 1),
          Cards(5, 1, 1), Cards(6, 1, 1), Cards(7, 1, 1), Cards(8, 1, 1),
          Cards(9, 1, 1), Cards(10, 1, 1), Cards(11, 1, 1), Cards(12, 1, 1),
          Cards(13, 1, 1), Cards(14, 1, 1),
    
          Cards(2, 1, 2), Cards(3, 1, 2), Cards(4, 1, 2), Cards(5, 1, 2),
          Cards(6, 1, 2), Cards(7, 1, 2), Cards(8, 1, 2), Cards(9, 1, 2),
          Cards(10, 1, 2), Cards(11, 1, 2), Cards(12, 1, 2), Cards(13, 1, 2),
          Cards(14, 1, 2),
    
          Cards(2, 2, 3), Cards(3, 2, 3), Cards(4, 2, 3), Cards(5, 2, 3),
          Cards(6, 2, 3), Cards(7, 2, 3), Cards(8, 2, 3), Cards(9, 2, 3),
          Cards(10, 2, 3), Cards(11, 2, 3), Cards(12, 2, 3), Cards(13, 2, 3),
          Cards(14, 2, 3),
    
          Cards(2, 2, 4), Cards(3, 2, 4), Cards(4, 2, 4), Cards(5, 2, 4),
          Cards(6, 2, 4), Cards(7, 2, 4), Cards(8, 2, 4), Cards(9, 2, 4),
          Cards(10, 2, 4), Cards(11, 2, 4), Cards(12, 2, 4), Cards(13, 2, 4),
          Cards(14, 2, 4)};
    
    #endif
    Code:
    //main.cpp
    #include "deck.h"
    int main() {
    
    system("PAUSE");
    return 0;
    }

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. _CARDS_ should not be used at the top. Underscore-capital constants are reserved. Use something like cardsH instead.

    2. Hmmm. I can't see why it wouldn't work. Change 'struct' to 'class' and give it a shot.

    EDIT:
    3. You should also add "using namespace std;" after #include <iostream>. That should have triggered errors, because cout wouldn't be defined.

    EDIT:
    4. When I tried to compile the code in VS.NET, I had beef with the opening curly brace here:
    Code:
    const Cards card[52] = {Cards(2, 1, 1),
    And that was all that flagged an error. What compiler are you using, and what are all the errors you got?
    EDIT:
    5. Found the problem. Unmatched braces. Add one more here:
    Code:
           case 4: cout << "Spades of ";
                            break;
          }
      }//<------New brace
    };
    6. Why are you using floats here?
    Code:
      float value; //2-14
      float colorvalue; //1=red,2=black
      float suitvalue; //1=hearts,2=diamonds,3=clubs,4=spades
    You should be using ints.
    7. Mine now compiles fine.
    Last edited by bennyandthejets; 04-24-2004 at 05:27 AM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    THANK YOU!
    thanks for taking your time! It works fine now.
    I also changed the floats to ints (don't know why I had floats )

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    My pleasure. Brace mismatches are very tricky, because compilers never seem to give you enough information to find them. What could help is better indenting. Make sure you are very strict with your indents, keeping pairs of braces at the same indent.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    yeah I noticed they can be tricky, thanks for the tips.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 05:20 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21