c++ - 错误 : no match for 'operator>>' in 'in >> *(arr + ((long unsigned int)(((long unsigned int)i) * 8ul)))'

标签 c++ templates

为了我在函数加载中的作业,我实例化了一个 ifstream 对象,打开一个文件并开始读入,得到了很长的编译器错误。 我也尝试通过重定向输入来摆脱 fstream 并使用 cin,但我从来没有走那么远。完全相同的错误,除了使用 cin 而不是 in。

    #ifndef _BUBBLE_SORT_H
    #define _BUBBLE_SORT_H

    #include <iostream>
    #include <fstream>
    #include <cstdlib>

    using namespace std;

    template <typename T>
    void swap(T *a, T *b){
            T t = *a; 
            *a = *b; 
            *b = t;
    }

    template <typename T>
    void bubbleSort(T * arr, int n){ 
            bool isSorted = false;
            for (int last = n-1; last > 0 && !isSorted; last--) {
                    isSorted = true;
                    for (int i = 0; i < last; i++)
                            if (arr[i] > arr[i+1]) {
                                    swap((arr+i), (arr+i+1));
                                    isSorted = false;
                             }
                    if (last % 1000 == 0) cerr << ".";
            }
    }

    template <typename T>
    void print(T arr[], int n){ 
            for(int i = 0; i < n; i++)
                    cout << arr[i] << '\t';
    }

    template <typename T>
    T &load(string filename, int &n){
            ifstream in; 
            in.open(filename.c_str());

            in >> n;
            if(!in) {
                    cerr << "Error opening file" ;
                    exit(1);
            }   
            T *arr = new T[n];

            for(int i = 0; i < n; i++)
                    in >> *(arr + i); 
            in.close();
            return *arr;
    } 

#endif

我正在使用 gnu gcc 编译器

最佳答案

问题出在对加载函数的调用中。你是在调用它,表明你正在阅读的数组类型吗?以下是编辑加载函数以返回数组的建议,以及主函数中的调用者:

template <typename T>
T *load(string filename, int &n){
        ifstream in; 
        in.open(filename.c_str());

        in >> n;
        if(in.fail()) {
                cerr << "Error opening file" ;
                exit(1);
        }   
        T *arr = new T[n];

        for(int i = 0; i < n; i++)
                in >> *(arr + i); 
        in.close();
        return arr;
} 

int main(){
        int x, i;
        string fileName = "test.txt";
        double *arr = load<double>(fileName, x); // make sure you have <double> in the call
        for(i = 0; i < x; i++) cout << arr[i] << endl;
        delete [] arr;
        return 0;
        }

确保你包括你正在读取的数组类型,比方说,double,否则你会得到一个错误。另外,以我的拙见,最好返回一个指针,如上面的代码片段所示。希望对您有所帮助。

编辑

关于模板工作方式的一些额外说明。编译代码时,模板会扩展为同一函数的多个不同版本,具体取决于整个程序中使用(并由 T 表示)的不同类型的数量。因此,考虑到这一点并查看问题中提出的代码,尝试从文件输入流读取到类型 T 是没有意义的,因为编译器无法扩展到基于未知类型的任何函数。但是,当模板变量 T 的类型已知时(在您的 typename 都出现在函数参数中的情况下),则 <double>规范可以省略,因为它是从函数调用中传递的参数类型推导出来的。

关于c++ - 错误 : no match for 'operator>>' in 'in >> *(arr + ((long unsigned int)(((long unsigned int)i) * 8ul)))' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209152/

相关文章:

c++ - 尝试使用 std::function 实例化模板时出现错误 C2371

c++ - 在 C++11 或 C++1y 中对非类型模板参数包进行排序?

c++ - C中的编译时错误

c++ - 重载后缀运算符不起作用

c++ - 如何在线程之间传播异常?

c++ - 动态多态内存容器 - 返回值不正确

java - 是否有可能 - 模板化此方法?

c++ - 嵌套模板类

c++ - 这行代码通过bfs在二分图算法中做了什么?

c++ - 如何要求输入密码才能关闭窗口?