# Vector Struct

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 12-21-2011
Elysia
What errors do you get?
• 12-21-2011
Attachment 11260

i have attached a screenshot of the errors i am getting

thanks
• 12-21-2011
Elysia
compareByCost is a function, so move it outside main or whatever other function you have it placed inside.
• 12-21-2011
Hi, when i move it outside, it says that WEdge is undefined.
do i also need a declaration of the function at the top as it is a function?

thanks
• 12-21-2011
hk_mp5kpdw
#1

A suggestion, implement a constructor for your WEdge struct:
Code:

```struct WEdge {     int From;  // an array for u points     int To; // an array for v points     int Cost;  // an array for w points     WEdge(int f = 0, int t = 0, int c = 0) : From(f), To(t), Cost(c) {} };```
Then your vector/pushback code (post #5) can be simplified:
Code:

```vector<WEdge>myEdges;     myEdges.push_back(WEdge(1,2,5)); // add edge to vector myEdges.push_back(WEdge(2,6,10)); // add edge to vector myEdges.push_back(WEdge(3,4,7)); // add edge to vector```

#2
Quote:

I cant seem to print out data from the vector now, e.g. i have tried
Code:

```for(int i=0; i<myEdges.size(); i++) {         cout << "data" << i << " Is: " << myEdges[i] << "\n"; } how would i access edge1 data such as the To, From, Weight that is held in the vector myEdges?```

You could do a couple things to access the elements. Remember myEdges is the vector of WEdge objects. Therefore, myEdges[i] is an element of this vector, i.e. a WEdge object. A WEdge object has a From/To/Cost member and so to access it you could do this:
Code:

```for(int i=0; i<myEdges.size(); i++) {         cout << "data" << i << " Is: " << myEdges[i].From << ' ' << myEdges[i].To << ' ' << myEdges[i].Cost << "\n"; }```
A better method however might be to look at overloading operator<< which is the stream insertion operator (writes to a stream). You could then do exactly what you had tried above since then the compiler would know how to write a WEdge object using <<. An example:
Code:

```struct city {     std::string name;     int population;     city(std::string n,int pop) : name(n), population(pop) {} }; std::ostream& operator<<(std::ostream& os, const city& rhs) {     return os << "City name: " << rhs.name << "\nPopulation: " << rhs.population; } ... city my_city("San Diego",1000000); std::cout << my_city << std::endl;```
Should output something like:
Code:

```City name: San Diego Population: 1000000```
• 12-21-2011
Elysia
Quote:

do i also need a declaration of the function at the top as it is a function?

Yes.
Also, though this isn't necessary, you may find this interesting:
C++11 - Lambda Closures, the Definitive Guide - Cprogramming.com
It is related to your problem. In a way.
• 12-21-2011
thanks hk_mp5kpdw, i have done that to simply push_back, thats great =) thanks

Elysia...i am not sure what to do, i have put the function outside, and put a function header, but it keeps saying that WEdge is undefined, would i need to put the WEdge struct outisde aswell.
Why do i need to say WEdge in the bool code as we are comparing things inside the vector. ahh im so confused, sorry for all the silly questions.
• 12-21-2011
Elysia
sort is comparing elements inside the vector, but will call upon your comparison function to see which one of two elements in the lesser one.
• 12-21-2011
Code:

``` bool compareByCost (const WEdge &lhs, const WEdge &rhs)         {         return lhs.wCost < rhs.wCost;         }```
• 12-21-2011
Elysia
Ah, put your struct before the compareByCost function declaration.
Also change

"for(int i=0; i<myEdges.size(); i++)"
to
"for(std::size_t i=0; i<myEdges.size(); i++)"

And similarly for the other for loop. That will eliminate warnings (which you should pay heed to!).
The size() function does not return an int.
• 12-21-2011
thats what i was thinking but i remember someone telling me that you shouldnt make things global like that.
but thanks it all seems to be working, i really appreciate everyones help so much

thanks =)
• 12-21-2011
Elysia
What you should avoid are global variables.
• 12-21-2011