Can I use a local struct in the "set" container

This is a discussion on Can I use a local struct in the "set" container within the C++ Programming forums, part of the General Programming Boards category; Can I use a local struct to store data in the "set" conatiner? If so, what is the correct method ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Can I use a local struct in the "set" container

    Can I use a local struct to store data in the "set" conatiner? If so, what is the correct method to do so?

    Compiler: Sun Studio 10 on Unix (Sun Solaris)

    Code:
    #include<iostream>
    #include<set>
    
    using namespace std;
    
    int main()
    {
      struct product
      {
        int id;
        string desc;
        double amt;
      } prdt;
    
      prdt.id = 1234;
      prdt.desc = "Credit Card";
      prdt.amt = 1000.00 ;
    
      cout << " This is a program to test set conatainer. " << endl
           << " Product ID is: " << prdt.id 
           << ", Product Desc is : " << prdt.desc 
           << ", Credit Card Limit is : " << prdt.amt << endl ;
      
    
    // Test program for 'set' container with a struct
     typedef std::set< product, std::less< int > > product_set;
    
      return 0;
    }

    Error Message:"testSet.c", line 26: Error: The local type "product" cannot be used as a template argument.
    "/opt/SunStudio10/SUNWspro/prod/include/CC/Cstd/./set", line 83: Error: The local type "product" cannot be used as a template argument.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    You can use a user-defined object yes. However the set needs the < (less than) operator to be defined for that object.

    Read: http://www.parashift.com/c++-faq-lit...erloading.html

    You will want to do something like:

    Code:
    bool  operator<(const product& lobj, const product &robj) {
        return lobj.id < robj.id;
    }
    That will be a non member function. And it will be used to determine if an object is "less than" another. The Set container is an associative ordered container. It needs that operator in order to determine where it will insert an element on the current list of objects.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    I don't know, I just did as compiler suggested and it compiles
    Code:
    struct product
      {
        int id;
        string desc;
        double amt;
      } ;
    int main()
    {
      struct product
       prdt;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    Can struct be declared in the main function?
    Hello, guys, i come from china, so i am not good at english. If you find the sentence i wrote full of mistakes, please tell me.
    I confirm that my english and programming skills will be improved with your help in this forum. thanks^_^

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by zouyu1983
    Can struct be declared in the main function?
    Not if you want to use it inside a set, no. Local classes can't be passed as parameters to functions.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    9 A local type, a type with no linkage or an unnamed type shall not be
    used as a template-argument for a template type-parameter. [Example:
    Code:
              void f()
              {
                      struct S { /* ... */ };
    
                      X<S> x3; // error: local type used as template-argument
              }
    --end example]
    From http://www.csci.csusb.edu/dick/c++std/cd2/index.html
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

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