首页 > 解决方案 > 使用 g++ 编译时出现多个错误?

问题描述

编译我的代码时,我收到许多不同的错误,例如:

frame.h:5:48: error: expected identifier before '(' token
frame.h:5:17: error: variable or field 'writeFrame' declared void
frame.h:5:17: error: 'GifWriter' was not declared in this scope
frame.h:5:34: error: expected primary-expression before 'double'

这些错误或多或少适用于所有函数参数。

这是头文件:

#ifndef frame_h
#define frame_h

void writeFrame(GifWriter* gptr, double delay, (std::vector<uint8_t>& frameOut), ImageData* image1ptr, struct Options mode, int frameNum, bool* runptr);
  
void runPRGM(ImageData* image1ptr, struct Options mode);
  
#endif

我的 .cpp 文件确实包含所需的库和这个 .h 文件,并且这两个函数的声明方式相同。我确定括号和括号已正确解析。除了 .cpp 中的函数定义之外,这些是函数声明的唯一实例。我可能做错了什么?我确实在其他一些线程中读到 g++ 也与某些 void 函数发生冲突,但对此没有太多详细说明。

标签: c++headerg++declaration

解决方案


更改(std::vector<uint8_t>& frameOut)std::vector<uint8_t>& frameOut

不知道为什么你觉得需要把括号放在那里,第一条错误消息告诉你这就是问题所在。

另请注意,您应该编写头文件,以便它们是自包含的。这意味着这个头文件应该具有#include <vector>以及声明GifWriter等。如果情况合适,这些可能是前向声明。

所以标题应该看起来像这样

#ifndef frame_h
#define frame_h

#include <vector>  // for vector
#include <cstdint> // for uint8_t

class GifWriter; // forward declarations
class ImageData;
struct Options;

void writeFrame(GifWriter* gptr, double delay, std::vector<uint8_t>& frameOut, ImageData* image1ptr, Options mode, int frameNum, bool* runptr);
  
void runPRGM(ImageData* image1ptr, Options mode);
  
#endif

图片假设GifWriter和是您ImageData编写Options的类,因此可以安全地转发声明它们。如果没有,那么您应该在此处包含这些类的头文件。


推荐阅读