recallelephant 2019-08-18 21:04
浏览 216

c++模板和container混用导致出现的问题,c2662指针无法从const type转换成type &?

#ifndef DATATABLE_H
#define DATATABLE_H

#include
#include
#include
#include
#include
#include
using namespace std;

extern int FW;

namespace sict {

template<typename T>
class DataTable {
    float Ymean = 0.0;
    float Ysigma = 0.0;

    deque<T> buffer;
    typename std::deque<T>::iterator i;

public:
    T sigmaCalculator(deque<T>);


    DataTable(ifstream & ifile) {

        float reader1;
        float reader2=0.0;
        float reader2Buffer;
        if (ifile.is_open())
        {
            while (ifile)
            {
                ifile >> reader1 >> reader2;
                reader2Buffer += reader2;

                buffer.push_back(reader1);
                buffer.push_back(reader2);
            }
        }

        Ymean = reader2Buffer / buffer.size() / 2;

        ifile.close();
    }

    void displayData(std::ostream& os) const {
        os << "Data Values" << endl;
        os << "------------" << endl;
        os.width(FW);
        os << "x";
        os.width(FW);
        os << "y" << endl;
        for (i = buffer.begin(); i != buffer.end(); i+2)
        {
            os.width(FW);
            os << *i;
            os.width(FW);
            os << *(++i) << endl;
        }
    }

    void displayStatistics(std::ostream& os)const{
        os << "Statistics" << endl;
        os << "----------" << endl;

        os << "  y mean    =";
        os.width(FW);
        os << Ymean << endl;
        os << " y sigma   =";
        os.width(FW);
        Ysigma = sigmaCalculator(buffer);
        os << Ysigma << endl;

    }

};



template<typename T>
 T DataTable<T>::sigmaCalculator(deque<T> input)
{
    float tempForMeanDifference = new float[input.size()];
    float sumBuffer = 0.0;
    for (int num = 0; num < input.size(); num++)
    {
        tempForMeanDifference[num] = input[num] - Ymean;
        tempForMeanDifference[num] = pow(tempForMeanDifference[num], 2);
    }

    for (int num = 0; num < input.size(); num++)
    {
        sumBuffer = sumBuffer + tempForMeanDifference[num];
    }
    sumBuffer = sumBuffer / input.size();

    sumBuffer = sqrt(sumBuffer);

    delete []tempForMeanDifference;

    return sumBuffer;
}

}

#endif // DATATABLE_H

//////////////////////////////////以上是头文件,就是出问题的内容


// Workshop 7 - STL Algorithms
// w7.cpp
// updated by Cornel on 18.10.2018
// updated by Chris Szalwinski 
// 2019/03/10

#include <iostream>
#include <fstream>
#include <string>
#include "DataTable.h"
#include "DataTable.h" // this is intentional
using namespace std;
int FW = 8; // field width
int ND = 4; // precision for numbers

// Reports the Statistics for file named pFileName
//
void processFile(const char* pFileName) {

    cout << endl;
    cout << "****************************************" << endl;
    cout << "*** Processing file [" << pFileName << "]" << endl;
    cout << "****************************************" << endl;

    std::ifstream dataFile(pFileName);
    if (!dataFile) {
        cerr << endl << "***Failed to open file " << pFileName << "***" << endl;
        return;
    }

    try {
        sict::DataTable<float> data(dataFile);
        cout << endl;
        data.displayData(cout);
        data.displayStatistics(cout);
    }
    catch (std::string& msg) {
        cout << "ERROR: " << msg << endl;
    }
}

int main(int argc, char** argv) {
#ifndef SICT_DATA_TABLE_H
    cout << "Improper header guard for DataTable.h! Follow the convention "
        << "SICT_DATA_TABLE_H when defining a header guard.\n";
#endif

    cout << "Command Line: " << argv[0];
    for (int i = 1; i < argc; i++)
        cout << " " << argv[i];
    cout << endl;
    if (argc < 2) {
        cerr << endl << "***Incorrect number of arguments***" << endl;
        return 1;
    }

    for (int i = 1; i < argc; ++i)
        processFile(argv[i]);
}

/////////////////////////这里是固定的地方,不用查找错误

大家好,最近开始学容器和迭代器,发现了一个奇怪的问题,就是明明没有declare任何函数或变量是const,然而编译器却说无法转换,会不会是有些隐式转换我没有意识到啊?谢谢大家

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 spring后端vue前端
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题