【问题标题】:'myStateType' was not declared in this scope caused by #typedef'myStateType' 未在此范围内声明,由 #typedef 引起
【发布时间】:2017-04-07 19:45:37
【问题描述】:

为了方便使用,我尝试使用#typedef 将类型声明为我自己的自定义名称:

class Solution {
public:
    void dfs(vector<vector<char>>& board, int i, int j)
    {
        using namespace std;
        #typedef std::pair<int, int> myStateType;

        std::queue<myStateType> q; // error on this line
        ...
    }
};

但是,编译错误提示在这一行std::queue&lt;myStateType&gt; q;:

'myStateType' 未在此范围内声明

我还没有弄清楚这个错误是怎么发生的?有任何想法吗?提前致谢!

【问题讨论】:

  • # 是实际代码的一部分吗? typedef std::pair&lt;int, int&gt; myStateType; 应该没问题。
  • 我觉得奇怪的是,您使用的是using namespace std;,但还指定了std:: 命名空间 IIF 该行出现在using 语句之后?

标签: c++ visual-studio scope macros typedef


【解决方案1】:

typedef 是 C 和 C++ 编程语言中的编译器令牌,不是宏

typedef std::pair<int,int> myStateType; // don't put # before
std:queue<myStateType> q;

'myStateType' 未在此范围内声明:表示您的程序不知道类型:myStateType

【讨论】:

  • 我发现调用 preprocessing directives 宏有点误导。
  • François Andrieux 是对的。通过删除“#”,程序可以运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2016-08-09
  • 2019-02-17
  • 2021-01-07
  • 2016-10-27
相关资源
最近更新 更多