不能在函数 C++ 中使用覆盖

Posted

技术标签:

【中文标题】不能在函数 C++ 中使用覆盖【英文标题】:Can't use override in function c++ 【发布时间】:2019-01-08 16:29:17 【问题描述】:

所以我一直在尝试使用 JUCE 处理音频并按照教程进行操作,他在其中一个函数上使用了覆盖,但是当我这样做时,它显示“函数声明符之后的预期函数体”。目标是播放视频中解释的随机白噪声。

视频链接:https://www.youtube.com/watch?v=GjNeYI6-uNE&index=11&list=PLLgJJsrdwhPxa6-02-CeHW8ocwSwl2jnu

代码:

/*
  ==============================================================================

    This file was auto-generated!

  ==============================================================================
*/

#include "MainComponent.h"

//==============================================================================
MainComponent::MainComponent()

    // Make sure you set the size of the component after
    // you add any child components.
    setSize (800, 600);

    // specify the number of input and output channels that we want to open
    setAudioChannels (2, 2);


MainComponent::~MainComponent()

    // This shuts down the audio device and clears the audio source.
    shutdownAudio();


//==============================================================================
void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)

    // This function will be called when the audio device is started, or when
    // its settings (i.e. sample rate, block size, etc) are changed.

    // You can use this function to initialise any resources you might need,
    // but be careful - it will be called on the audio thread, not the GUI thread.

    // For more details, see the help for AudioProcessor::prepareToPlay()


void MainComponent::getNextAudioBlock (const AudiosourceChannelInfo& bufferToFill) override /*this one gets the error*/

    for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
    
        float* const buffer = bufferToFill.buffer->getWritePointer(channel, bufferToFill.startSample);

        for (int sample = 0; sample < bufferToFill.numSamples; ++sample)
        
            buffer[sample] = (random.nextFloat() * 2.0f - 1.0f) * 0.25;
        
    

    bufferToFill.clearActiveBufferRegion();


void MainComponent::releaseResources()

    // This will be called when the audio device stops, or when it is being
    // restarted due to a setting change.

    // For more details, see the help for AudioProcessor::releaseResources()


//==============================================================================
void MainComponent::paint (Graphics& g)

    // (Our component is opaque, so we must completely fill the background with a solid colour)
    g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));

    // You can add your drawing code here!


void MainComponent::resized()

    // This is called when the MainContentComponent is resized.
    // If you add any child components, this is where you should
    // update their positions.

.h 文件:

    /*
  ==============================================================================

    This file was auto-generated!

  ==============================================================================
*/

#pragma once

#include "../JuceLibraryCode/JuceHeader.h"

//==============================================================================
/*
    This component lives inside our window, and this is where you should put all
    your controls and content.
*/
class MainComponent   : public AudioAppComponent

public:
    //==============================================================================
    MainComponent();
    ~MainComponent();

    //==============================================================================
    void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
    void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override;
    void releaseResources() override;

    //==============================================================================
    void paint (Graphics& g) override;
    void resized() override;

private:
    //==============================================================================
    // Your private member variables go here...

    Random random;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
;

Main.cpp

/*
  ==============================================================================

    This file was auto-generated!

    It contains the basic startup code for a JUCE application.

  ==============================================================================
*/

#include "../JuceLibraryCode/JuceHeader.h"
#include "MainComponent.h"

//==============================================================================
class AudioOutApplication  : public JUCEApplication

public:
    //==============================================================================
    AudioOutApplication() 

    const String getApplicationName() override        return ProjectInfo::projectName; 
    const String getApplicationVersion() override     return ProjectInfo::versionString; 
    bool moreThanOneInstanceAllowed() override        return true; 

    //==============================================================================
    void initialise (const String& commandLine) override
    
        // This method is where you should put your application's initialisation code..

        mainWindow.reset (new MainWindow (getApplicationName()));
    

    void shutdown() override
    
        // Add your application's shutdown code here..

        mainWindow = nullptr; // (deletes our window)
    

    //==============================================================================
    void systemRequestedQuit() override
    
        // This is called when the app is being asked to quit: you can ignore this
        // request and let the app carry on running, or call quit() to allow the app to close.
        quit();
    

    void anotherInstanceStarted (const String& commandLine) override
    
        // When another instance of the app is launched while this one is running,
        // this method is invoked, and the commandLine parameter tells you what
        // the other instance's command-line arguments were.
    

    //==============================================================================
    /*
        This class implements the desktop window that contains an instance of
        our MainComponent class.
    */
    class MainWindow    : public DocumentWindow
    
    public:
        MainWindow (String name)  : DocumentWindow (name,
                                                    Desktop::getInstance().getDefaultLookAndFeel()
                                                                          .findColour (ResizableWindow::backgroundColourId),
                                                    DocumentWindow::allButtons)
        
            setUsingNativeTitleBar (true);
            setContentOwned (new MainComponent(), true);

           #if JUCE_IOS || JUCE_android
            setFullScreen (true);
           #else
            setResizable (true, true);
            centreWithSize (getWidth(), getHeight());
           #endif

            setVisible (true);
        

        void closeButtonPressed() override
        
            // This is called when the user tries to close this window. Here, we'll just
            // ask the app to quit when this happens, but you can change this to do
            // whatever you need.
            JUCEApplication::getInstance()->systemRequestedQuit();
        

        /* Note: Be careful if you override any DocumentWindow methods - the base
           class uses a lot of them, so by overriding you might break its functionality.
           It's best to do all your work in your content component instead, but if
           you really have to override any DocumentWindow methods, make sure your
           subclass also calls the superclass's method.
        */

    private:
        JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
    ;

private:
    std::unique_ptr<MainWindow> mainWindow;
;

//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (AudioOutApplication)

【问题讨论】:

哪一行给出了错误? 您是否在 C++11 模式(或更高版本)下编译?否则override 确实会产生错误。 这些成员函数是否在基类中定义为virtual 很确定我在 c++11 中编译是的 是的,它似乎是虚拟的 【参考方案1】:

您需要从.cpp 文件中的函数定义中删除override。此说明符只能出现在成员函数声明中(即,在头文件中),除非您在头文件中定义函数,在这种情况下,它可以出现在函数体之前。

【讨论】:

以上是关于不能在函数 C++ 中使用覆盖的主要内容,如果未能解决你的问题,请参考以下文章

C++ 中,类的继承:父类当使用虚函数时候,子类对该函数进行重写的话,属于子类成员函数对虚函数的覆盖!

如何在 C++ 中覆盖函数

如何在 C++ 中模拟覆盖父函数(不隐藏)?

Visual C++ - 覆盖从 DLL 导入的函数?

c++ 函数的隐藏和覆盖

我可以在 C++ 中覆盖字符串返回类型函数吗