c++ - 成员没有默认构造函数

标签 c++ oop openframeworks

我是 openFrameworks/C++ 的新手,但使用 Processing/Java 已有一段时间了。我在实例化一个对象时遇到问题,该对象的类是我在 testApp header 中创建的。

它抛出错误:

Implicit default constructor for 'testApp' must explicitly initialize the member 'currentSeq' which does not have a default constructor.

这是我的 Sequence.h 文件:

#pragma once
#include "ofMain.h"

class Sequence{

public:
    Sequence(long _start, long _stop){
        start = _start;
        stop = _stop;
    }

    long start;
    long stop;

};

这是我的 testApp.h:

#pragma once

#include "ofMain.h"
#include "sequence.h"

class testApp : public ofBaseApp{
    public:

        void setSequences();
        bool needsNewSeq();

        void setup();
        void update();
        void draw();

        void keyPressed(int key);
        void keyReleased(int key);
        void mouseMoved(int x, int y);
        void mouseDragged(int x, int y, int button);
        void mousePressed(int x, int y, int button);
        void mouseReleased(int x, int y, int button);
        void windowResized(int w, int h);
        void dragEvent(ofDragInfo dragInfo);
        void gotMessage(ofMessage msg);

        int numSequences;
        int seqIndex;
        bool isPaused;

        Sequence currentSeq;
        vector <Sequence> sequences;
        ofVideoPlayer myVideo;

};

问题是 currentSeq 变量。出于某种原因,序列 vector 很好。根据this openFrameworks 教程我似乎做的一切都是正确的。

最佳答案

您的参数化构造函数 Sequence(long, long) 覆盖编译器生成的默认构造函数,从而隐式删除 testApp 的默认构造函数。在你做的那一行:

Sequence currentSeq;

这会抑制 testApp 的默认构造,因为 currentSeq 没有可行的构造函数,因此会出现错误。要解决此问题,请为您的 Sequence 类应用默认构造函数(您还应将 startstop 成员初始化为 0):

class Sequence
{
public:
    Sequence() : start(0), stop(0)
//  ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
    { }

    Sequence(long _start, long _stop)
        : start(_start), stop(_stop) // Use member-initializer list here too
    { }

    long start;
    long stop;

};

或者,您可以为 testApp 提供默认构造函数并像这样构造 currentSeq:

class testApp : public ofBaseApp
{
    testApp() : currentSeq(0, 0)
    { }
    // ...
};

关于c++ - 成员没有默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879131/

相关文章:

c++ - 向上移动对象层次结构

c++ - 将元素插入 vector 时避免复制参数

c++ - 从 QML 访问 C++ QLists

c# - 为什么 Func<> 和 Expression<Func<>> 可以互换?为什么一个对我有用?

c# - 管理引用计数和对象生命周期的模式

c++ - 坚持 twitcurl

c++ - 在Nuke中翻转OpenEXR RgbaOutputFile

c++ - 在 C++ 中嵌套 for()

c# - 在 C# 中从具有依赖接口(interface)的抽象类继承的正确方法

c++ - Sublime Text 找不到外部 C++ 库