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

时间:2014-02-01 03:59:00

标签: 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[]);

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
};
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]

2 个答案:

答案 0 :(得分:2)

只需切换标题的顺序,您的代码就可以正常编译。由于您未提供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

答案 1 :(得分:1)

头文件应包含足够的定义,以便它们自给自足。在这种情况下,您需要提供必需类型的定义,前缀。只需将include添加到EAN.h的顶部:

#include <GS1Prefix.h>
bool isValid(const char* str);
bool isRegistered(const Prefix* p, const char* str, char area[],char publisher[],
                  char title[]);

然后,您可以在任何源文件中包含EAN.h,因为您知道没有依赖项需要担心。