my own struct on vector

Discussion in 'C++' started by remlostime, Feb 6, 2008.

  1. remlostime

    remlostime Guest

    6 int main()
    7 {
    8 struct nodeType
    9 {
    10 int a, b;
    11 };
    12 list<nodeType> x;
    13 return 0;
    14 }
    (2 / 5): error: `main()::nodeType' uses local type `main()::nodeType'

    what's wrong with my code? how should I modify?
    remlostime, Feb 6, 2008
    #1
    1. Advertising

  2. remlostime

    Jim Langston Guest

    remlostime wrote:
    > 6 int main()
    > 7 {
    > 8 struct nodeType
    > 9 {
    > 10 int a, b;
    > 11 };
    > 12 list<nodeType> x;
    > 13 return 0;
    > 14 }
    > (2 / 5): error: `main()::nodeType' uses local type `main()::nodeType'
    >
    > what's wrong with my code? how should I modify?


    But me once too. You have to declare the structure outside of any function
    including main. structures and classes used in templates can't be local to
    a function.

    struct nodeType
    {
    int a, b;
    };

    int main()
    {
    list<nodeType> x;
    return 0;
    }

    should work.




    --
    Jim Langston
    Jim Langston, Feb 6, 2008
    #2
    1. Advertising

  3. remlostime

    remlostime Guest

    On Feb 6, 7:32 pm, "Jim Langston" <> wrote:
    > remlostimewrote:
    > > 6 int main()
    > > 7 {
    > > 8 struct nodeType
    > > 9 {
    > > 10 int a, b;
    > > 11 };
    > > 12 list<nodeType> x;
    > > 13 return 0;
    > > 14 }
    > > (2 / 5): error: `main()::nodeType' uses local type `main()::nodeType'

    >
    > > what's wrong with my code? how should I modify?

    >
    > But me once too. You have to declare the structure outside of any function
    > including main. structures and classes used in templates can't be local to
    > a function.
    >
    > struct nodeType
    > {
    > int a, b;
    >
    > };
    >
    > int main()
    > {
    > list<nodeType> x;
    > return 0;
    >
    > }
    >
    > should work.
    >
    > --
    > Jim Langston
    >


    another question is how to add items to this list?
    if I want to add a=1 b = 2 into it, how to do?
    remlostime, Feb 6, 2008
    #3
  4. On 6 Feb, 12:59, remlostime <> wrote:
    > On Feb 6, 7:32 pm, "Jim Langston" <> wrote:
    >
    >
    >
    >
    >
    > > remlostimewrote:
    > > >  6 int main()
    > > >  7 {
    > > >  8   struct nodeType
    > > >  9   {
    > > > 10     int a, b;
    > > > 11   };
    > > > 12   list<nodeType> x;
    > > > 13   return 0;
    > > > 14 }
    > > > (2 / 5): error: `main()::nodeType' uses local type `main()::nodeType'

    >
    > > > what's wrong with my code? how should I modify?

    >
    > > But me once too.  You have to declare the structure outside of any function
    > > including main.  structures and classes used in templates can't be local to
    > > a function.

    >
    > > struct nodeType
    > > {
    > >   int a, b;

    >
    > > };

    >
    > > int main()
    > > {
    > >   list<nodeType> x;
    > >   return 0;

    >
    > > }

    >
    > > should work.


    Please do not quote signatures.

    > another question is how to add items to this list?
    > if I want to add a=1 b = 2 into it, how to do?- Dölj citerad text -


    nodeType n;
    n.a = 1;
    n.b = 2;
    x.push_back(n);

    Or you can add a constructor to your node:

    struct nodeType {
    int a, b;
    nodeType(int a, b) : a(a), b(b) {}
    };

    Then you can use it like so:

    x.push_back(nodeType(1,2));

    --
    Erik Wikström
    Erik Wikström, Feb 6, 2008
    #4
  5. remlostime

    remlostime Guest

    ---------- Forwarded message ----------
    From: remlostime <>
    Date: Feb 6, 6:58 pm
    Subject: my own struct on vector
    To: comp.lang.c++


    6 int main()
    7 {
    8 struct nodeType
    9 {
    10 int a, b;
    11 };
    12 list<nodeType> x;
    13 return 0;
    14 }
    (2 / 5): error: `main()::nodeType' uses local type `main()::nodeType'

    what's wrong with my code? how should I modify?

    The third question is how to print x.a
    remlostime, Feb 6, 2008
    #5
  6. On 6 Feb, 14:30, remlostime <> wrote:
    > ---------- Forwarded message ----------
    > From: remlostime <>
    > Date: Feb 6, 6:58 pm
    > Subject: my own struct on vector
    > To: comp.lang.c++
    >
    >   6 int main()
    >   7 {
    >   8   struct nodeType
    >   9   {
    >  10     int a, b;
    >  11   };
    >  12   list<nodeType> x;
    >  13   return 0;
    >  14 }
    > (2 / 5): error: `main()::nodeType' uses local type `main()::nodeType'
    >
    > what's wrong with my code? how should I modify?
    >
    > The third question is how to print x.a


    You have to loop through all the elements in x and print them, like
    this:

    std::list<nodeType>::iterator it;
    for (it = x.begin(); it != x.end(); ++it)
    {
    std::cout << it->a << "\n"
    }

    --
    Erik Wikström
    Erik Wikström, Feb 6, 2008
    #6
  7. tragomaskhalos, Feb 6, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,723
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,345
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    344
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page