What errors do you get?
i have attached a screenshot of the errors i am getting
thanks
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
#1
A suggestion, implement a constructor for your WEdge struct:
Then your vector/pushback code (post #5) can be simplified: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) {} };
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
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: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?
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:for(int i=0; i<myEdges.size(); i++) { cout << "data" << i << " Is: " << myEdges[i].From << ' ' << myEdges[i].To << ' ' << myEdges[i].Cost << "\n"; }
Should output something like: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;
Code:City name: San Diego Population: 1000000
Last edited by hk_mp5kpdw; 12-21-2011 at 02:43 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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.
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.
Code:bool compareByCost (const WEdge &lhs, const WEdge &rhs) { return lhs.wCost < rhs.wCost; }
Last edited by Khadafi; 12-21-2011 at 03:00 PM.
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.
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 =)
oh okay.
thanks very much.