my own struct on vector

R

remlostime

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?
 
J

Jim Langston

remlostime said:
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.
 
R

remlostime

remlostimewrote:


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.

another question is how to add items to this list?
if I want to add a=1 b = 2 into it, how to do?
 
E

Erik Wikström

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));
 
R

remlostime

---------- Forwarded message ----------
From: remlostime <[email protected]>
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
 
E

Erik Wikström

---------- Forwarded message ----------
From: remlostime <[email protected]>
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"
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top