2017-05-04 77 views
-4

我收到此错误:纯虚函数错误

In function 'bool detect_feedback(AudioEffect*)': 

54:30: error: cannot convert 'std::shared_ptr<AudioEffect>' to 'AudioEffect*' in assignment 

55:16: error: cannot convert 'std::shared_ptr<AudioEffect>' to 'AudioEffect*' in assignment 

55:40: error: cannot convert 'std::shared_ptr<AudioEffect>' to 'AudioEffect*' in assignment 

的代码如下:

#include<stdlib.h> 
#include<iostream> 
#include<memory>` 

//BASE CLASS 

// audio and have a subsequent effect (next). 

struct AudioEffect 
{ 
    virtual ~AudioEffect() = default; 
    virtual void process(float* buf, size_t num) = 0; 
    std::shared_ptr<AudioEffect> next; 
}; 

//DERIVING MULTIPLE AUDIO-EFFECTS 

//Noise Gate 

struct NoiseGate: public AudioEffect 
{ 
    float threshold; 
    void process(float *buf, size_t num) 
    { 
     if (*buf > threshold) 
      *buf = threshold; 
    } 
}; 

//Gain Boost 

struct GainBoost: public AudioEffect 
{ 
    float signal; 
    void process(float *buf, size_t num) 
    { 
     *buf = *buf + signal; 
    } 
}; 

//Compressor 

struct Compressor: public AudioEffect 
{ 
    float offset; 
    void process(float *buf, size_t num) 
    { 
     *buf = *buf - offset; 
    } 
}; 

//Function 
// Implement a function that checks if there is a feedback loop 
// in the effects chain. 
//... detect_feedback(...) 
//{ 
//} 

bool detect_feedback (AudioEffect *ae) 
{ 
    AudioEffect *p, *q; 
    for (p = ae;p != NULL; p = p->next) 
     for (q = p->next; q != NULL; q = q->next) 
      if (typeid(*p) == typeid(*q)) 
       return true; 
    return false; 
} 

//MAIN 

int main(int argc, char *argv[]) 
{  
    return 0; 
} 
+0

我提醒您熟悉'std :: shared_ptr',特别是'*'运算符。 – lfgtm

+0

为什么混合原始指针和共享指针?你写了这段代码,还是其他人? –

+0

这与纯虚函数完全无关。你应该减少你的问题给[MCVE]。 –

回答

1

您无法分配shared_ptr到原始指针。您需要使用.get()方法来获取原始指针。

(p->next).get()