julian
julian

Reputation: 157

Super Noob C++ variable help

Ok, I must preface this by stating that I know so so little about c++ and am hoping someone can just help me out...

I have the below code:

string GoogleMapControl::CreatePolyLine(RideItem *ride)
{
    std::vector<RideFilePoint> intervalPoints;
    ostringstream oss;

    int cp;
    int intervalTime = 30;  // 30 seconds
    int zone =ride->zoneRange();
    if(zone >= 0)
    {
        cp = 300;  // default cp to 300 watts
    }
    else
    {
        cp = ride->zones->getCP(zone);
    }

    foreach(RideFilePoint* rfp, ride->ride()->dataPoints())
    {
        intervalPoints.push_back(*rfp);
        if((intervalPoints.back().secs - intervalPoints.front().secs) > intervalTime)
        {
            // find the avg power and color code it and create a polyline...
            AvgPower avgPower = for_each(intervalPoints.begin(),
                                         intervalPoints.end(),
                                         AvgPower());
            // find the color
            QColor color = GetColor(cp,avgPower);
            // create the polyline
            CreateSubPolyLine(intervalPoints,oss,color);
            intervalPoints.clear();
            intervalPoints.push_back(*rfp);
        }

    }
    return oss.str();
}


void GoogleMapControl::CreateSubPolyLine(const std::vector<RideFilePoint> &points,
                                         std::ostringstream &oss,
                                         QColor color)
{
    oss.precision(6);
    QString colorstr = color.name();
    oss.setf(ios::fixed,ios::floatfield);
    oss << "var  polyline  = new GPolyline([";

    BOOST_FOREACH(RideFilePoint rfp, points)
    {
        if (ceil(rfp.lat) != 180 && ceil(rfp.lon) != 180)
        {
            oss << "new GLatLng(" << rfp.lat << "," << rfp.lon << ")," << endl;
        }
    }

    oss << "],\"" << colorstr.toStdString() << "\",4);";

    oss << "GEvent.addListener(polyline, 'mouseover', function() {" << endl
    << "var tooltip_text = 'Avg watts:" << avgPower <<" <br> Avg Speed: <br> Color: "<< colorstr.toStdString() <<"';" << endl
    << "var ss={'weight':8};" << endl
    << "this.setStrokeStyle(ss);" << endl       
    << "this.overlay = new MapTooltip(this,tooltip_text);" << endl
    << "map.addOverlay(this.overlay);" << endl
        << "});" << endl
    << "GEvent.addListener(polyline, 'mouseout', function() {" << endl
    << "map.removeOverlay(this.overlay);" << endl
    << "var ss={'weight':5};" << endl
    << "this.setStrokeStyle(ss);" << endl
    << "});" << endl;

    oss << "map.addOverlay (polyline);" << endl;
}

And I'm trying to get the avgPower from this part:

AvgPower avgPower = for_each(intervalPoints.begin(),
                                             intervalPoints.end(),
                                             AvgPower());

the first part to cary over to the second part:

<< "var tooltip_text = 'Avg watts:" << avgPower <<" <br> Avg Speed: <br> Color: "<< colorstr.toStdString() <<"';" << endl

But of course I haven't the slightest clue how to do it... anyone feeling generous today?

Thanks in advance

Upvotes: 2

Views: 189

Answers (2)

Todd Gardner
Todd Gardner

Reputation: 13521

It seems like you are asking how to access the local variable avgPower within a different function (sorry if I am misunderstanding). This is less about c++ specifically and more about functional or object oriented design. There are many different ways to do this, but here are the sane ones I can think of, in the order of my preference.

  1. Create avgPower (and color), within CreateSubPolyLine. There doesn't really seem to be any reason they are in CreatePolyLine anyway. Implement a separate call if there are other consumers. The function signature would change to

    void GoogleMapControl::CreateSubPolyLine(const std::vector &points, std::ostringstream &oss)

  2. Include it in the function's paramemters, e.g., change the signature to:

    void GoogleMapControl::CreateSubPolyLine(const std::vector &points, std::ostringstream &oss, const QColor& color, const AvgPower& avgPower)

  3. Put it in a member variable of GoogleMapControl. It doesn't seem like this would be a bad design choice in my opinion.

Upvotes: 1

Chris H
Chris H

Reputation: 6601

Well you didn't state the problem with the code other than it doesn't work. I'm suspicious of your use of AvgPower() in the for_each. Also, you have AvgPower as a class. why not a double or something? For this code I would have expected to see something like this:

PowerType addIntervals(const RideFilePoint &p1, const RideFilePoint &p2) {
    //Add and return.
}
   ...
   ...
PowerType total = accumulate(i.begin(), i.end(), PowerType(0.0), &addIntervals);
avg = total/i.length();

read these docs carefully: http://www.sgi.com/tech/stl/accumulate.html

Upvotes: 2

Related Questions