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,然而编译器却说无法转换,会不会是有些隐式转换我没有意识到啊?谢谢大家

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问