C++ 错误,可能来自 .h 文件,不确定

标签 c++ header-files

好的,我正在尝试编译一个程序:

g++ -std=c++0x a1test.cpp GS1Prefix.cpp EAN.cpp

但是我遇到了以前从未见过的错误。

In file included from a1test.cpp:17:0:
EAN.h:3:25: error: âPrefixâ does not name a type
EAN.h:3:33: error: ISO C++ forbids declaration of âpâ with no type [-fpermissive]
a1test.cpp: In function âbool RegisteredTests(const Prefix*, int&, int*, int*)â:
a1test.cpp:222:68: error: no matching function for call to âisRegistered(const Prefix*&, const char [14], char [6], char [8], char [7])â
a1test.cpp:222:68: note: candidates are:
EAN.h:3:6: note: bool isRegistered(const int*, const char*, char*, char*, char*)
EAN.h:3:6: note:   no known conversion for argument 1 from âconst Prefix*â to âconst int*â
GS1Prefix.h:10:6: note: bool isRegistered(const Prefix*, int)
GS1Prefix.h:10:6: note:   candidate expects 2 arguments, 5 provided

这只是错误的一半,下面是所有的 .h 文件和指向 a1test.cpp 的链接(它是一个长代码)

EAN.h

bool isValid(const char* str);
bool isRegistered(const Prefix* p, const char* str, char area[],char publisher[], char title[]);

GS1前缀.h

const int MAX = 700;
struct Prefix {
    int  no;             // number of entries
    int  area[MAX];      // area elements
    char pubLow[MAX][8]; // low end of publisher range
    char pubHgh[MAX][8]; // high end of publisher range
    int  pubLen[MAX];    // no of chars in publisher string
};
bool load(const char* filename, Prefix* p);
bool isRegistered(const Prefix* p, int area);
int minNoDigits(const Prefix* p, int area);
bool isRegistered(const Prefix* p, int area, const char* publisher);

链接到 a1test.cpp -> a1test.cpp

更新:按照remyabel的建议,我在#include "EAN.h"之后制作了#include "GS1Prefix.h"

In file included from EAN.cpp:6:0:
EAN.h:3:25: error: âPrefixâ does not name a type
EAN.h:3:33: error: ISO C++ forbids declaration of âpâ with no type [-fpermissive]

最佳答案

只需切换 header 的顺序,您的代码就可以正常编译。因为您没有提供 GS1Prefix.cppEAN.cpp 我无法评论其余的错误(如果还有的话)

#include <cstring>

// GS1Prefix.h
const int MAX = 700;
struct Prefix {
    int  no;             // number of entries
    int  area[MAX];      // area elements
    char pubLow[MAX][8]; // low end of publisher range
    char pubHgh[MAX][8]; // high end of publisher range
    int  pubLen[MAX];    // no of chars in publisher string
};
// EAN.h
bool isValid(const char* str);
bool isRegistered(const Prefix* p, const char* str, char area[],char publisher[], char title[]);
// a1test.cpp
bool load(const char* filename, Prefix* p);
bool isRegistered(const Prefix* p, int area);
int minNoDigits(const Prefix* p, int area);
bool isRegistered(const Prefix* p, int area, const char* publisher);
// .. rest of your file

关于C++ 错误,可能来自 .h 文件,不确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21493917/

相关文章:

c++ - 每个 C 或 C++ 文件都应该有一个关联的头文件吗?

c++ - 可以在智能指针管理的内存上创建新的位置吗?

objective-c - 是否可以在与在 Objective-C 中使用它的类头文件相同的文件中定义枚举?

c++ - 如何将 1 到 5 的数字随机插入到一个 vector 中并检查以确保没有重复?

c++ - .pro 文件更改后 Qt 不刷新 Makefile

c - 关于使用 c 进行汇编

c++ - 为什么我的代码显示错误 undefined reference to `WinMain@16' ?

c - 源文件看不到其他 header 的定义

c++ - 在 C++ 中对用户定义的类型使用 extern 关键字

c++ - 如何在一个属性中动态存储基本类型?