Help with vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SVincent240
    New Member
    • Dec 2008
    • 1

    Help with vectors

    I am having a problem creating a vector of type struct. this is my code that will not work:
    [code=cpp]
    //Initialization steps prior to code.
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;

    int main ()
    {

    char letter;
    int numb = 0;

    struct acct
    {
    char first [15];
    char last [15];
    char ssn [9];
    char logon [8];
    };

    vector <acct> accounts;

    ifstream roster; //These lines initialize the text file containing the account data.
    roster.open("/roster.txt");
    if (roster.fail()) return 1;
    [/code]

    the error msg is:

    error: 'main()::acct' uses local type 'main()::acct'
    error: trying to instantiate 'template<class _Alloc> class std::allocator'
    error: template argument 2 is invalid
    error: invalid type in declaration before ';' token
    Last edited by Frinavale; Dec 16 '08, 08:45 PM. Reason: added [code] tags
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Try moving your struct definition outside of main().

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      What compiler are youusing?

      Your code example compiles and links using Visual Studio.NET 2008.

      Comment

      • vmpstr
        New Member
        • Nov 2008
        • 63

        #4
        It doesn't compile on g++ 4.2.4
        Making the struct definition global produces a different kind of error. But, if the struct is made global and the line
        vector <acct> accounts;
        is replaced with
        vector <struct acct> accounts;
        it compiles and links

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by vmpstr
          Making the struct definition global produces a different kind of error. But, if the struct is made global and the line
          vector <acct> accounts;
          is replaced with
          vector <struct acct> accounts;
          it compiles and links
          I don't understand your reply: a) when you say struct definition, do you mean declaration? b) what is the difference between (a) and struct is made global ? and c) C++ does not require the use of struct when referring to a user defined type.

          Comment

          • vmpstr
            New Member
            • Nov 2008
            • 63

            #6
            Originally posted by weaknessforcats
            I don't understand your reply: a) when you say struct definition, do you mean declaration? b) what is the difference between (a) and struct is made global ? and c) C++ does not require the use of struct when referring to a user defined type.
            I've always been very week in my terminology. I apologize for that. I believe it's called "type definition". I'm referring to the chunk of code that is something along the lines of the following:

            Code:
            struct a
            {
                int i;
            };
            By "struct made global", I'm referring to the previous reply (by Ganon11) which suggested that the struct definition should be moved outside of the function.

            Lastly, I agree that C++ does not require struct keyword. This has been my experience as well, although I tend to put it there anyway. However, without the struct keyword in the parameter to the template, I get the following messages on g++ (when compiling the code posted above)

            Code:
            ~/test$ g++ compileme.cpp
            compileme.cpp: In function ‘int main()’:
            compileme.cpp:23: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
            compileme.cpp:23: error:   expected a type, got ‘acct’
            compileme.cpp:23: error: template argument 2 is invalid
            compileme.cpp:23: error: invalid type in declaration before ‘;’ token
            I don't claim that putting struct keyword there is good style or bad style. I simply say that it seems to be the only way to compile that code. This might be simply a g++ problem...

            I hope this clears it up a little bit.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This looks like a g++ problem.

              As I say, your original code is OK and does compile and link with Visual Studio.NET 2008.

              The g++ complaint about expecting a type and got acct is odd since acct is a type. Worse, when vector<struct acct> works and vector<acct doesn't leads one to think that g++ thinks it's compiling C.

              As to definition vs declaration, the difference is that a definition occupies memory while a declaration does not.

              These are declarations:
              Code:
              struct X   //or class X
              {
                  int a;
              };
              
              extern int data;
              
              enum Colors {RED, BLUE, GREEN};
              
              void A Function(int, int);
              Whereas these are definitions:
              Code:
              X    Avariable:
              
              int data;
              
              Colors MyColor = RED;
              
              void A Function(int, int)
              {
                 //do something
              }

              Comment

              • vmpstr
                New Member
                • Nov 2008
                • 63

                #8
                Hmm... Thanks for explaining that, it makes a bit easier to think about definitions and declarations.

                :D

                Comment

                Working...