Thread: can i "vetor<struct>" ?

  1. #1
    adiinedude
    Guest

    can i "vetor<struct>" ?

    ok... i'm getting an error message what i can't understand.
    this is my first time using vectors so i'm probley doing it wrong.

    Code:
    struct NC_SOCKET_ID{
               int _id;
           };
    
    struct NC_SOCKET_STR{
    ...
               NC_SOCKET_ID _id;
    ...
           };
    
    vector<NC_SOCKET_STR> _SOCKET_LIST;
    Now i get the error
    <>
    no match for `NC_SOCKET_ID & == NS_SOCKET_ID&'
    <>
    at this line in my code.
    Code:
            if(_SOCKET_LIST[n]._id == id)
            ...
    Am i using the vector wrong or can it not be used like this?

    Compiler: Dev-c++ 4.9.7.0

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you ever adding anything to the vector?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Stupid me. I just noticed the problem. You need to put a C++ constructor in your struct.

    Example

    Code:
    struct NC_SOCKET_STR{
        NC_SOCKET_ID _id;
        NS_SOCKET_STR() {
    
        }
    };
    You still do need to do what I said in the other post though.

  4. #4
    adivinedude
    Guest
    yes
    i left out most of the code.

  5. #5
    adivinedude
    Guest
    Originally posted by master5001
    [B]Stupid me. I just noticed the problem. You need to put a C++ constructor in your struct.
    never heard of one in a struct... is it the same as in a class?

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Indeed it is.

    [edit]
    changed do to the fact that it was incorrect.
    [/edit]
    Last edited by master5001; 12-16-2002 at 01:41 AM.

  7. #7
    adivinedude
    Guest
    thx it worked.
    struct's being like classes should be on the faq

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well, I don't think its on the FAQ because saying "structs are like classes in C++" wouldn't totally be accurate. If you are interested in more of the differences I've seen threads come and go about this topic. Just do a search...although I don't know what you would search for.

  9. #9
    adivvinedude
    Guest
    k thx again...
    also just to note...( don't know y i did it this way firstoff)
    Code:
    struct NC_SOCKET_ID{
               int _id;
           };
    //would be better off as
    typedef signed long int NC_SOCKET_ID;

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I didn't realize that all you had in the struct was an int.

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by master5001
    Indeed it is. The only thing a struct can't do that a class can is inherit another struct or class. Otherwise structs and classes are pretty much the same.
    No, actually a struct CAN use inheritance, and is capable of polymorphism. The difference between structs and classes are that structs default to public datamembers/member functions/inheritance while classes default to private.

Popular pages Recent additions subscribe to a feed