Super Noob C ++变量帮助

时间:2010-03-14 00:00:16

标签: c++

好的,我必须先说明我对c ++知之甚少,并希望有人可以帮助我......

我有以下代码:

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;
}

我正试图从这一部分获得avgPower:

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

第一部分是第二部分:

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

但当然我没有丝毫的线索如何去做......今天有人感到慷慨吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

嗯,你没有说明代码的问题,除了它不起作用。我怀疑你在for_each中使用AvgPower()。此外,您将AvgPower作为一个类。为什么不是双重或什么?对于这段代码,我希望看到类似的东西:

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

仔细阅读这些文档: http://www.sgi.com/tech/stl/accumulate.html

答案 1 :(得分:1)

好像你在询问如何在不同的函数中访问本地变量avgPower(对不起,如果我误解了)。这不仅仅是关于c ++而是关于功能或面向对象设计的更多内容。有很多不同的方法可以做到这一点,但这是我能想到的理智的,按照我的偏好顺序。

  1. 在CreateSubPolyLine中创建avgPower(和颜色)。无论如何,他们似乎没有任何理由在CreatePolyLine中。如果有其他消费者,请实施单独的呼叫。函数签名将更改为

    无效GoogleMapControl :: CreateSubPolyLine(const std :: vector&amp; points,                                          std :: ostringstream&amp; oss)

  2. 将其包含在函数的参数中,例如,将签名更改为:

    无效GoogleMapControl :: CreateSubPolyLine(const std :: vector&amp; points,                                          std :: ostringstream&amp; oss,                                          const QColor&amp;颜色,                                          const AvgPower&amp; avgPower)

  3. 将它放在GoogleMapControl的成员变量中。在我看来,这似乎不是一个糟糕的设计选择。