custom class vector

This is a discussion on custom class vector within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to use the stl::vector with a class I wrote and the compiler is telling me that it ...

  1. #1
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    custom class vector

    Hi,
    I'm trying to use the stl::vector with a class I wrote and the compiler is telling me that it can't use the template.
    The compiler output is:
    Code:
    Borland C++ 5.5.1 for Win32 Copyright
    addbk.cpp:
    Error E2102 addbk.cpp 7: Cannot use template vector<T, Allocator> without specifying specialization parameters
    Error E2293 addbk.cpp 7: ) expected
    And the same for five or six lines. I've no idea where it needs the `)'.
    Do I have to do something to my class to make it work with it? Or do I have to write the special template for my class?
    Thanks.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    vector is a container template, which means that you must at least
    specify the type you want it to hold
    e.g
    Code:
    class Foo {
    
    std::vector<int> vec;
    ....
    };

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Wledge, I think you missunderstood me. What I'm trying to do is something like:

    vector<myclass_t> vec;

    and that's where I have the problem.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Post the actual code.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    this andthis might be helpful
    Away.

  6. #6
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Thanks for those links, blackrat, it turns out I don't have a copy constructor and I think my copy assignment operator isn't written correctly.
    Right now I'm not where my code is so I'll have to wait a couple of days 'till I'm back and I can test it.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by -=SoKrA=-
    Thanks for those links, blackrat, it turns out I don't have a copy constructor and I think my copy assignment operator isn't written correctly.
    Right now I'm not where my code is so I'll have to wait a couple of days 'till I'm back and I can test it.
    Well, EVERY class always has a copy constructor and an assignment operator. If you don't specify them, but use them, the compiler must auto-generate them. If shallow copies of the data are OK, then you don't need to make either. Else, you need to make both (and almost certainly a destructor too).

  8. #8
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Yeah, you're right, Cat, I just meant that I hadn't written one but compiler-made one isn't good enough for my class.
    The problem was that when I was passing the vector by reference to my functions, All I wrote was
    Code:
    int function(vector& entries)
    but it turns out that you have to write it
    Code:
    int function(vector<myclass> entries)
    I though that vector& was enough and that's why it was giving me all those errors.
    One more fact about the stl I guess...
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can still pass by reference:

    void f(const vector<type>&)
    or...
    void f(vector<type>&)

    for example.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>You can still pass by reference

    Oops. Missed the `&'. I meant to write

    int function(vector<myclass>& entries)

    I actually have to pass by reference because the functions change the contents. I was doing two things at a time and missed it.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Including a custom class file
    By Beowolf in forum C++ Programming
    Replies: 29
    Last Post: 09-29-2007, 06:29 PM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  4. Linker error in templated vector class
    By BigDaddyDrew in forum C++ Programming
    Replies: 6
    Last Post: 01-10-2003, 01:06 PM
  5. help on some vector of custom class code
    By cozman in forum C++ Programming
    Replies: 1
    Last Post: 08-10-2001, 12:55 AM

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