小编Sak*_*mon的帖子

"使用带有二进制位运算符的带符号整数操作数" - 使用无符号短整型时

在下面的C片段中,检查是否设置了16位序列的前两位:

bool is_pointer(unsigned short int sequence) {
  return (sequence >> 14) == 3;
}
Run Code Online (Sandbox Code Playgroud)

CLion的Clang-Tidy给我一个"使用带有二进制位运算符的有符号整数操作数"警告,我无法理解为什么.是unsigned short不是没有签名?

c clion clang-tidy

34
推荐指数
3
解决办法
8721
查看次数

集合比较器的前向声明

我有一个使用节点(顶点)的图形结构,而节点又以std::pair<Node*, int>节点是边缘另一端的形式附加边缘,整数是边缘权重.我想std::multiset根据连接的节点索引和边缘权重将边缘排序为a .

enum color { white, grey, black };

struct EdgeComparator;

struct Node {
  int n;
  std::multiset<std::pair<Node *, int>, EdgeComparator> edges;
  enum color col;
  int d;  // distance to source node

  explicit Node(int n) : n(n), edges(), col(white), d() {};
};

struct EdgeComparator {
  bool operator()(const std::pair<Node *, int> &p1,
                  const std::pair<Node *, int> &p2) {
    if (p1.second == p2.second)
      return p1.first->n < p2.first->n;
    return p1.second < p2.second;
  }
};
Run Code Online (Sandbox Code Playgroud)

这种前向声明方法导致错误:invalid use of incomplete …

c++ graph cross-reference forward-declaration

4
推荐指数
1
解决办法
145
查看次数

根据子构造函数参数调用具有不同参数的父构造函数

有没有办法根据子构造函数的参数值调用具有不同参数的父构造函数?

我有以下父类:

class Rectangle
{
public:
    Rectangle(std::string name, glm::vec3 top_left_corner, float height, float width, glm::vec3 color, bool fill);
    ~Rectangle();
    //...
}
Run Code Online (Sandbox Code Playgroud)

和孩子班:

class Wall :
    public Rectangle
{
public:
    Wall(std::string name, Position position, float scene_height, float scene_width, float thickness, glm::vec3 color);
    ~Wall();
    //...
}
Run Code Online (Sandbox Code Playgroud)

哪里Positionenum应该决定什么样的参数的父类的构造应该被称为:

enum Position { UP, DOWN, LEFT, RIGHT };
Run Code Online (Sandbox Code Playgroud)

所以基本上,我想在子构造函数中有这样的东西:

Wall::Wall(std::string name, Position position, float window_height, float window_width, float thickness, glm::vec3 color) {
    switch(position) {
    case UP:
        Rectangle(name, glm::vec3(0, window_height, 0), thickness, …
Run Code Online (Sandbox Code Playgroud)

c++ oop inheritance

4
推荐指数
1
解决办法
118
查看次数

在另一个宏中使用字符串宏

如何在C中的字符串宏中使用另一个宏?

我有这个:

#define MAX_OPERATION_COUNT 10
#define MSG_TOO_MANY_OPERATIONS "Too many operations! Only the first 10 were applied."
Run Code Online (Sandbox Code Playgroud)

但我希望第二个宏使用第一个宏的值.例如,在Java中,我会有类似的东西:

public static final int MAX_OPERATION_COUNT = 10;
public static final String MSG_TOO_MANY_OPERATIONS = "Too many operations! Only the first " + MAX_OPERATION_COUNT + " were applied.";
Run Code Online (Sandbox Code Playgroud)

有没有办法在C中做类似的事情?

编辑:这个问题 的解决方案与这个问题的解决方案非常相似,但我认为问题本身是完全不同的,应该单独处理.

c string macros

1
推荐指数
1
解决办法
116
查看次数