Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;<;的运算符重载&书信电报;抽象类_C++ - Fatal编程技术网

C++ C++;<;的运算符重载&书信电报;抽象类

C++ C++;<;的运算符重载&书信电报;抽象类,c++,C++,以下是我的头文件: //Frame.h #pragma once class Frame { string frameName; protected: double fileSize; vector<Attribute> attributes; public: Frame(string f, double size, vector<Attribute> d) :frameName(f), fileSize(size), attribute

以下是我的头文件:

//Frame.h
#pragma once

class Frame {
    string frameName;
protected:
    double fileSize;
    vector<Attribute> attributes;
public:
    Frame(string f, double size, vector<Attribute> d) :frameName(f), fileSize(size), attributes(d) {}
    virtual ~Frame() {}
    string& GetFrameName() { return frameName; }
    Attribute& operator[](int);
    int size() { return attributes.size(); }
    virtual void Compress() = 0;
    friend ostream& operator<<(ostream&, Frame&);
};

// AudioFrame
#pragma once

class AudioFrame :public Frame {
    static const int RATES = 3;
    static constexpr double BITRATE[]{128,160,192};
    static constexpr double COMPRESSION_RATIO[]{11.1,9.1,7.1};
public:
    AudioFrame(string frameName, double fileSize, vector<Attribute> d) :Frame(frameName,fileSize, d) {}
    ~AudioFrame(){}
    void Compress();
    friend ostream& operator<<(ostream&, AudioFrame&);
};


//ImageFrame.h
#pragma once

class ImageFrame :public Frame {
    static const int BITS = 8;
    static constexpr double COMPRESSION_RATIO = 6.0;
    static constexpr double BITDEPTH_FACTOR[] {11.1,4.6,3.5,2.4,1.9,1.5,1.2,1.0};
public:
    ImageFrame(string fileName, double fileSize, vector<Attribute> d) :Frame(fileName, fileSize, d) {}
    ~ImageFrame(){}
    void Compress();
    friend ostream& operator<<(ostream&, ImageFrame&);
};

如果你真的想
cout如果你真的想
cout在这里工作得很好:,这真的就是你调用操作符的方式吗?我实现了操作符,我不应该是你的朋友,或者你可以给我们看你的测试代码吗?ImageFrame没有默认的构造函数。如何编写ImageFrame测试;?在这里工作很好:,这真的就是你调用操作符的方式吗?我实现了操作符不应该是friend ostream&operator你能给我们看你的测试代码吗?ImageFrame没有默认的构造函数。如何编写ImageFrame测试;?嗯,我对虚拟函数很在行,但是是教授制作了标题,不希望我们更改它们!但是太棒了,这很有效,谢谢你。你知道我怎样才能给你名声吗,我是新来的@阿格尼塞萨尼你接受这个答案已经给了我名声!可怕的代码,但满足苛刻的先决条件。孩子们,不要在家里模仿这个!(闭门课程,专业开发人员。)我对虚拟函数很在行,但是教授制作了标题,不想让我们更改!但是太棒了,这很有效,谢谢你。你知道我怎样才能给你名声吗,我是新来的@阿格尼塞萨尼你接受这个答案已经给了我名声!可怕的代码,但满足苛刻的先决条件。孩子们,不要在家里模仿这个!(非公开课程,专业开发人员)
ImageFrame test;
cout << test << endl;
// code from AudioFrame.cpp
ostream& operator<<(ostream& os, AudioFrame& obj) {
    os << "AudioFrame" << endl;
    os << "Name = " << obj.GetFrameName() << endl;
    for (unsigned int i = 0; i < obj.attributes.size(); i++) {
        os << "\tBandwidth #" << i << ": " << obj.attributes[i] << endl;
    }

    return os;
}

// code from ImageFrame.cpp
ostream& operator<<(ostream& os, ImageFrame& obj) {
    os << "ImageFrame" << endl;
    os << "Name = " << obj.GetFrameName() << endl;
    for (unsigned int i = 0; i < obj.attributes.size(); i++) {
        os << "\tResolution #" << i << ": " << obj.attributes[i] << endl;
    }

    return os;
}

// code from Frame.cpp
ostream& operator<<(ostream& os, Frame& obj) {
    return os;
}
int type;
deque<Frame*> frames; // all frames are stored here
// user is promoted a console menu for selecting a frame type (ImageFrame or AudioFrame)

if (type == 1)
    frames.push_back(new AudioFrame(...));
else
    frames.push_back(new ImageFrame(...));

// now when i need to print all frames i do
for (unsigned int i = 0; i < frames.size(); i++)
    cout << *(frames[i]) << endl;
ostream& operator<<(ostream& stream, Frame& frame) {
  AudioFrame* as_audio_frame = dynamic_cast<AudioFrame*>(&frame);
  ImageFrame* as_image_frame = dynamic_cast<ImageFrame*>(&frame);

  if(as_audio_frame) {
    return stream << *as_audio_frame;
  }

  if(as_image_frame) {
    return stream << *as_image_frame;
  }

   //normal frame code
}
class FrameAdapterInterface {
public:
  virtual ~FrameAdapterInterface() {};

protected:
  virtual std::ostream print(std::ostream&) = 0;
  friend ostream& operator<<(ostream&, FrameAdapterInterface &);
};

template<typename FRAME_T>
class AdapatedFrameType : public FrameAdapterInterface {
  FRAME_T data_;
public:
  template<typename... ARGS_T>
  AdapatedFrameType(ARGS_T&&... args)
    : data_(std::forward<ARGS_T>(args)...) {}

  ostream& print(std::ostream& stream) override {
    return stream << data_; 
  }
};

ostream& FrameAdapterInterface::operator<<(ostream& stream, FrameAdapterInterface& frame) {
  return frame.print(stream);
}