首页 > 解决方案 > 我怎样才能摆脱出现在我的方法模板中的类型?方法和类的输入都是模板

问题描述

我收到错误

declaration is incompatible with "void spectrogram<T>::update(<error-type> x) 

我看不出方法的声明和定义之间有任何区别,不知道为什么它只抱怨这个定义而不是构造函数或析构函数。

这是 vComplex.hpp

#ifndef VCOMPLEX_H
#define VCOMPLEX_H

template <class T>
class vComplex {
public:
    T* realp;
    T* imagp;
    int length; // for bookkeeping

    vComplex(void) { }

    vComplex (T* I, T* Q, int len) {
        realp = I;
        imagp = Q;
        length = len;
    }

    ~vComplex(void) {
        free(realp);
        free(imagp);
    }

    void put(T* I, T*Q, int len) {
        realp = I;
        imagp = Q;
        length = len;
    }
};

#endif

spectrogram.hpp 中更新的函数声明,删除了其他成员:

#ifndef SPECTROGRAM_H
#define SPECTROGRAM_H

template <typename T>
class spectrogram {
    public:
        void update(vComplex<T> x);
};
#endif

以及用于更新 spectrogram.cpp 的函数签名(和包含):

#include <stdio.h>
#include <math.h>
#include "spectrogram.hpp"
#include "vComplex.hpp"

template <typename T>
void spectrogram<T>::update(vComplex<T> x) {
   //do stuff
}

在 VS 2017 中,我在更新下得到了红色下划线,其中的所有内容基本上都坏了。VS 说 T 是未定义的,我假设这是由整体错误引起的。我必须使用动态分配的指针,我没有使用其他类型或容器的选项。

标签: c++templateslinker

解决方案


推荐阅读