0

I am getting the error

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

I don't see any difference between the declaration and the definition of the method, not sure why it is complaining about just this one definition and not the constructor or destructor.

Here is 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

the function declaration for update in spectrogram.hpp, with other members removed:

#ifndef SPECTROGRAM_H
#define SPECTROGRAM_H

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

and the function signature (and includes) for update in 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
}

In VS 2017, I get the red underline under update and everything inside of it breaks basically. VS is saying T is undefined which I'm assuming is caused by the overall error. I have to use dynamically allocated pointers, I don't have the option of using other types or containers.

  • 1
    Unrelated: You're going to run into [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) from the looks of it. – user4581301 Feb 04 '19 at 23:05
  • 1
    Unrelated: You may have removed the special member functions to make the example shorter, but just in case... `vComplex` does not comply with [the Rule of Three.](https://en.cppreference.com/w/cpp/language/rule_of_three) – user4581301 Feb 04 '19 at 23:12
  • vComplex I posted in its entirety, I figured that was relevant. How does it not comply? This is the first I'm hearing of the Rule of Three – user2653897 Feb 04 '19 at 23:18
  • 1
    The short description of the Rule of Three is if you need a destructor, copy constructor or assignment operator, you probably need all three. In this case you have memory allocations that need to be freed. If you copy `vComplex` with the default copy constructor (as you do by passing by value in `void update(vComplex x)`) only the pointers get copies, not what's pointed at. This leaves you with two objects pointing to the same allocation. If the program manages to live long enough, one copy's destructor will fire leaving the other copy pointing at invalid memory. – user4581301 Feb 04 '19 at 23:23
  • Trying to duplicate your problem with VS2015, the best I can do is point out the lack of `#include "vComplex.hpp"` in spectrogram.hpp. This will cause problems, but different problems. This leads me to think that the real problem is either [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) with an error message I'm not familiar with or I'm accidentally solving the problem when manufacturing my test case. I Recommend producing a [mcve]. – user4581301 Feb 04 '19 at 23:27
  • Good write up on [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 Feb 04 '19 at 23:31
  • Why do you need *pointers* to T at all? – Aconcagua Feb 05 '19 at 10:37
  • Calling `free` on member pointers looks very strange in a generic C++ template. This way, you 1. won't call the destructor of the objects pointed to 2. will provoke undefined behaviour if someone created these objects with `new` (which is most likely in C++...). – Aconcagua Feb 05 '19 at 10:40

0 Answers0