如何将c++对象作为参数传递给函数

How can I pass a c++ object as a parameter to function?

本文关键字:参数传递 函数 对象 c++      更新时间:2023-10-16

我在一个项目中有三个类,分别叫Pixel、custFrame和FrameHolder。

我的custFrame类标题是这样的:

#pragma once
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "Pixel.h"
#ifndef CUSTFRAME_H
#define CUSTFRAME_H
class custFrame
{
public:
    custFrame();
    void addPixel(Pixel pix);
    void setWidth(int width);
    void setHeight(int height);
    int getWidth();
    int getHeight();
    int getPixelSize();
    Pixel getPixel(int count);
private:
    std::vector<Pixel> pixels;
    int Height;
    int Width;
};
#endif

我的FrameHolder类标题是这样的:

#pragma once
//Hold all captured frames containing data
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "custFrame.h"
#include "Pixel.h"
#ifndef FRAMEHOLDER_H
#define FRAMEHOLDER_H
class FrameHolder {
public:
    FrameHolder();
    static FrameHolder* instance();
    void addFrame(IDeckLinkVideoFrame* fram);
    void calibrate(custFrame fram);
    int numFrames();
    void setWidth(int width);
    void setHeight(int height);
    static FrameHolder *inst;
    bool calibrating;
    int getHeight();
    int getWidth();
    bool isCalibrating();
private:
    //Member variables
    int Width;
    int Height;
    std::vector<IDeckLinkVideoFrame *>frames;
};
#endif

在我的FrameHolder类中,将custFrame对象传递给函数以将该帧存储在对象中似乎不起作用。我得到一个编译器错误("语法错误:标识符'custFrame'第24行")。然而,在我的custFrame类中,传递一个Pixel对象作为帧的一部分进行存储非常有效。我是不是错过了什么?我看过这篇文章,但没有多大帮助。

问题是由的存在引起的

#include "FrameHolder.h"

在两个.h文件中。因此,custFrame的定义在FrameHolder的定义之前是看不到的。

通过指针/引用传递可能是您在这里应该做的事情。至于语法错误,可能是在头中包含custFrame类时,它没有正确声明。