weighted links

This is a discussion on weighted links within the C++ Programming forums, part of the General Programming Boards category; The whole point of my current project is to write a graph of sorts and my problem at this early ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    weighted links

    The whole point of my current project is to write a graph of sorts and my problem at this early stage is the weighted link data type which basically attaches an integer value to the pointer. Please bear in my I'm treating this as an extension of a tree.
    Code:
    //------------------------------------ Enumerations -----------------------------------
    enum colour {None, Green, Blue, Grey, Yellow, Black, Orange, Red, Brown, Pink, Maroon};
    const string line_name[] = {"None", "District", "Picadilly", "Jubilee", "Circle", 
                                "North_London", "East_London", "Central", "Bakerloo",
                                "Hammersmith_City", "Metropolitan"};
    //--------------------------------------------------------------------------------------
    //----------------------------- structure definition -----------------------------------
    template <class cl_pntr> struct weighted_link {
    int weight;
    colour line_colour;
    cl_pntr *link;
    
    void setinfo(int w, colour lc, cl_pntr &lnk); //<------------- Not sure how to pass the 3rd parameter. And this is where my problem stems from
    }; //ends weighted_link
    
    template<class cl_pntr> void weighted_link<cl_pntr>::setinfo(int w, colour lc, cl_pntr &lnk)
    { weight = w; line_colour = lc; link = lnk }//<---------- here
    //--------------------------------------------------------------------------------------
    //----------------------------- station definition -------------------------------------
    class uground{
    string name;
    int zone;
    vector<weighted_link<uground *> > connections;
    public:
    uground();
    uground(string nm, int zn){name = nm; zone = zn;}//end constructor
    void setdetails(string nm, int zn){name = nm; zone = zn;}
    void setlink(uground x, uground &y, int w, colour cl){                      
    connections[connections.size()].setinfo(w, Orange, y); //<------------ here 
    }//end of setlink
    
    int getzone(){return zone;}
    string getname(){return name;}
    int num_of_connections(){return connections.size();}
    This is a snippet of code and my problem has to do with my knowledge of C++. I am aware that I'm simplifying things but the whole idea is to really get a grip with playing with C++ and learning its features.
    Originally I started using auto_ptr but I'm not entirely sure how they work.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    your function header is correct. If you want the pointer to point to that struct, you would do this:
    Code:
    link = &lnk;
    if you want the pointer to point to a copy of that struct, you would do this:
    Code:
    link = new cl_pntr(lnk);
    to do this, whatever class cl_pntr is needs a copy constructor;

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I am having a problem with pointers and references' logic or rather C++s' internal treatment of them.
    Code:
    #include <iostream>
    #include <memory> //for auto pointers
    #include <string>
    #include <vector>
    using namespace std;
    
    //------------------------------------ Enumerations -----------------------------------
    enum colour {None, Green, Blue, Grey, Yellow, Black, Orange, Red, Brown, Pink, Maroon};
    const string line_name[] = {"None", "District", "Picadilly", "Jubilee", "Circle", 
                                "North_London", "East_London", "Central", "Bakerloo",
                                "Hammersmith_City", "Metropolitan"};
    //--------------------------------------------------------------------------------------
    //----------------------------- structure definition -----------------------------------
    template <class cl_pntr> struct weighted_link {
    int weight;
    colour line_colour;
    cl_pntr *link;
    
    void setinfo(int w, colour lc, cl_pntr &lnk);
    }; //ends weighted_link
    
    template<class cl_pntr> void weighted_link<cl_pntr>::setinfo(int w, colour lc, cl_pntr &lnk)
    { weight = w; line_colour = lc; link = new cl_pntr(lnk); }
    //------------------------------------------------------------------------------------------------------
    //----------------------------- station definition -----------------------------------------------------
    class uground{
    string name;
    int zone;
    vector<weighted_link<uground *> > connections;
    public:
    uground();
    uground(string nm, int zn){name = nm; zone = zn;}//end constructor 
    uground& operator=(const uground &x){name = x.name; zone = x.zone; connections = x.connections; return *this;}// The vector copied in the rightt way??
    
    void setdetails(string nm, int zn){name = nm; zone = zn;}
    void setlink(const uground x, int w, colour cl){
    connections[connections.size()].setinfo(w, cl, x); // I have a logical problem here with c++ internal workings
    }//end of setlink
    
    int getzone(){return zone;}
    string getname(){return name;}
    int num_of_connections(){return connections.size();}
    };//ends station
    //-------------------------------------------------------------------------------------------------------
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Code:
    void setinfo(int w, colour lc, cl_pntr &lnk); //<------------- Not sure how to pass the 3rd parameter. And this is where my problem stems from
    The only reason to pass a non-const reference is if you intend to modify the parameter.
    Code:
    template<class cl_pntr> void weighted_link<cl_pntr>::setinfo(int w, colour lc, cl_pntr &lnk)
    >> { weight = w; line_colour = lc; link = lnk }//<---------- here
    link has type "cl_pntr *link" and lnk has type "cl_pntr &lnk" - one's a pointer and one's a reference, that's why the equals doesn't work.
    Code:
    vector<weighted_link<uground *> > connections;
    ...
    void setlink(const uground x, int w, colour cl){
        connections[connections.size()].setinfo(w, cl, x); // I have a logical problem here with c++ internal workings
    }//end of setlink
    The templated type of weighted_link is "uground*". That means that the actual type for "link" is "uground **link". I don't think that's what you want.

    It's hard to advise what you should be doing because I can't tell what you're trying to do.
    Here are some suggestions that may help though:
    • Remove the template off of wieghted_link until you get your design hashed out. Once it's working for one type, turn it into a template for handling any type.
    • Only use a non-const reference paramter if you intend to modify that parameter.
    • When passing any object larger than a pointer, pass it as a constant reference, unless the previous bullet applies.


    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Magnet Links
    By anirban in forum Tech Board
    Replies: 3
    Last Post: 12-08-2007, 07:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Creating URL links
    By Hexxx in forum C Programming
    Replies: 11
    Last Post: 12-31-2003, 09:52 AM
  5. Useful Links
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2002, 02:29 PM

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