【发布时间】:2014-11-27 11:36:24
【问题描述】:
当我去构建我的 C++ 项目时,我得到了 53 个错误。但是,从我的项目中的 5 个头文件之一中,它连续 4 次出现相同的错误列表。我检查了输出,发现它试图编译那个头文件 5 次。看来第一次成功了。其他 4 次出错,但他们一遍又一遍地出现相同的错误。我遵循了导致的包括的地方。根据我包含该头文件的所有位置,每次包含该头文件时它都会尝试编译它是有道理的。
这是多次编译的头文件。第一次成功编译是有道理的,但我不明白为什么它在构建项目时每隔一次编译就会出现一堆错误:
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
#include "Account.h"
#include "BSTree.h"
using namespace std;
class Transaction
{
public:
Transaction();
Transaction(char type, string firstName, string lastName, int ID, Account* account1, int fund1, Account* account2, int fund2, int amount);
~Transaction();
void setPtrAccounts(BSTree* ptrAccounts);
bool Transact();
private:
static BSTree* ptrAccounts;
char type;
string firstName;
string lastName;
int ID;
Account* account1;
int fund1;
Account* account2;
int fund2;
int amount;
void Deposit();
void History();
void Open();
bool Transfer();
bool Withdraw();
};
#endif
这是重复的错误列表。这些错误完全是假的。上面头文件中的代码没有错:
错误 C2061:语法错误:标识符“帐户”\thejollybanker\transaction.h 14 1
错误 C2061:语法错误:标识符“BSTree”\thejollybanker\transaction.h 16 1
错误 C2143:语法错误:缺少 ';'在'*'之前 \thejollybanker\transaction.h 19 1
错误 C4430:缺少类型说明符 - 假定为 int。 \thejollybanker\transaction.h 19 1
错误 C2143:语法错误:缺少 ';'在'*'之前 \thejollybanker\transaction.h 24 1
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int \thejollybanker\transaction.h 24 1
错误 C2143:语法错误:缺少 ';'在'*'之前 \thejollybanker\transaction.h 26 1
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int \thejollybanker\transaction.h 26 1
以下是输出窗口的摘要:
Transaction.cpp
TheJollyBanker.cpp
Transaction.h 错误
基金.cpp
BSTree.cpp
Transaction.h 错误
银行.h
Transaction.h 错误
帐户.cpp
Transaction.h 错误
生成代码...
如何让它只编译一次,以便第一次编译成功?
【问题讨论】:
-
很难说它为什么会在没有更多上下文的情况下失败。但是,此标头不会自行编译。例如,这取决于使用的
namespace std。因此,如果您在using namespace std语句之后包含此文件,它就可以工作。
标签: c++ compilation header