【问题标题】:Preprocessor #define T causes error in Xcode预处理器#define T 在 Xcode 中导致错误
【发布时间】:2014-04-11 04:20:50
【问题描述】:

每当我在我的代码中使用 #define T 时,我都会收到大约 19 个与 Xcode 使用的 type_traits 文件相关的错误。我已将其降低到我的一个头文件中的一行。这是头文件:

#ifndef __SlidingWindowCompression__Utils__
#define __SlidingWindowCompression__Utils__

// DEFINITIONS:
#define CONT_FLAG 0x00
#define STOP_FLAG 0xFF
#define BYTE_SIZE 8
#define A_KEY 0x000
#define T_KEY 0x001
#define G_KEY 0x010
#define C_KEY 0x100
#define N_KEY 0x011
#define A 0x100
#define T 0x101 //ERROR RIGHT HERE WHEN INCLUDED!!!
#define G 0x110
#define C 0x111

// INCLUDES:
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdint>

// FUNCTION DECLERATIONS:
std::string getKey(std::ifstream& inStream);
int  getReadSize(std::ifstream& inStream);
void outputHeader(const int READ_SIZE, const std::string& KEY, std::ofstream& outStream);
bool setRead(std::string& read, std::ifstream& inStream);
void stuffit(unsigned char value, int bits, int endFlag, std::ofstream& outStream);
void setIndexAndBitset(const std::string& KEY, std::string& read, uint16_t& index, std::vector<int>& bits);
unsigned char getHex(const char readChar, const char keyChar);

#endif

如果我更改 T 的名称或不包含所有错误,则消失。我不能真正发布带有错误的文件,因为它有 1000 行长。它的名字是“type_traits”,大部分错误是这样的:

在 'typename' 之后需要一个限定名称

【问题讨论】:

  • 您使用的是reserved identifier。无论如何,T 经常在模板中用作类型名称。更改它不适用于任何使用它的模板。
  • 请尽量避免#define。将它们替换为const int 或其他东西
  • 您能否详细说明为什么我不想使用#defines?您是说改为将它们全局声明为 const int?

标签: c++ xcode c-preprocessor


【解决方案1】:

当你使用时

#define T 0x101 

您将文件中使用的每个 T 字母替换为 0x101 包含标题的文件

更好的解决方案是使用枚举而不是定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    相关资源
    最近更新 更多