C++ 下面的代码仍然没有';在Fatih在SO问题中建议的更改后,t编译

C++ 下面的代码仍然没有';在Fatih在SO问题中建议的更改后,t编译,c++,C++,下面的代码是从“GNUC库”复制的 在@Fatih中,我建议对要编译的代码进行修改,正如我在下面的注释中所示 #include <signal.h> #include <stdio.h> #include <unistd.h> // This was inserted by Fatih volatile struct two_words { int a, b; } memory; void handler(int signum) { prin

下面的代码是从“GNUC库”复制的

在@Fatih中,我建议对要编译的代码进行修改,正如我在下面的注释中所示

#include <signal.h>
#include <stdio.h>
#include <unistd.h>    // This was inserted by Fatih 

volatile struct two_words { int a, b; } memory;

void
handler(int signum)
{
   printf ("%d,%d\n", memory.a, memory.b);
   alarm (1);
}

int
main (void)
{
   static struct two_words zeros = { 0, 0 }, ones = { 1, 1 };
   signal (SIGALRM, handler);
   memory = zeros;
   alarm (1);
   while (1)
     {
       memory = zeros;
       memory = ones;
     }
}

我现在缺少什么?

您添加了一个
volatile
,这使得代码与原始示例不同。@πάνταῥεῖ
volatile
位于GNU中的原始代码中。正如我所说,代码仍然无法编译。有什么建议吗?正如您所注意到的,这段代码来自GNUC库(重点是C)。您也被标记为
C
而不是
C++
的问题,并且代码确实在编译时编译。并且应该阐明为什么这在C++中不起作用,这两个伟大的链接回答了我的问题。非常感谢。
main.cpp: In function 'int main()':
main.cpp:19:13: error: passing 'volatile two_words' as 'this' argument discards qualifiers [-fpermissive]
   19 |    memory = zeros;
      |             ^~~~~
main.cpp:5:17: note:   in call to 'constexpr two_words& two_words::operator=(const two_words&)'
    5 | volatile struct two_words { int a, b; } memory;
      |                 ^~~~~~~~~
main.cpp:23:17: error: passing 'volatile two_words' as 'this' argument discards qualifiers [-fpermissive]
   23 |        memory = zeros;
      |                 ^~~~~
main.cpp:5:17: note:   in call to 'constexpr two_words& two_words::operator=(const two_words&)'
    5 | volatile struct two_words { int a, b; } memory;
      |                 ^~~~~~~~~
main.cpp:24:17: error: passing 'volatile two_words' as 'this' argument discards qualifiers [-fpermissive]
   24 |        memory = ones;
      |                 ^~~~
main.cpp:5:17: note:   in call to 'constexpr two_words& two_words::operator=(const two_words&)'
    5 | volatile struct two_words { int a, b; } memory;
      |                 ^~~~~~~~~