C++ C++;共享内存中的二维数组(基于命令行参数)

C++ C++;共享内存中的二维数组(基于命令行参数),c++,ipc,shared-memory,C++,Ipc,Shared Memory,我试图将存在于共享内存段中的二维(2D)数组附加到C中的进程。我在编译时遇到以下错误: main.cpp:52:22: error: invalid conversion from ‘void*’ to ‘int (*)[2]’ [-fpermissive] shm_assemble = shmat(shmid_assemble,0,0); 编译命令是: g++ main.cpp -o myworkshop 我的代码是: #include <iostream> #includ

我试图将存在于共享内存段中的二维(2D)数组附加到C中的进程。我在编译时遇到以下错误:

main.cpp:52:22: error: invalid conversion from ‘void*’ to ‘int (*)[2]’ [-fpermissive]
  shm_assemble = shmat(shmid_assemble,0,0);
编译命令是:

g++ main.cpp -o myworkshop
我的代码是:

#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <ctype.h>

#include "myheader.h"

void perror_exit(char *message);
bool init(int argc, char * argv[], int *Y);

int main (int argc , char* argv[]){

    if(argc<3) {
        perror_exit("Usage: ./myworkshop -Y <value>\n");
        return(-1);
    }
    int i=0,n=0,child_pid=0,Y=0,shmid_paint=0,shmid_assemble=0,shmid_check=0,shmid_y=0,painter_empty=0,painter_full=0,checker_full=0,checker_empty=0,assembler_full=0,assembler_empty=0,ex=0;
    int *pids = new int[8];

    kill_message *shm_y;
    message *shm_paint,*shm_check;
    int (*shm_assemble)[2];

    if (!init(argc,argv,&Y)){
        std::cout << "Something is wrong with the arguments, try again\n";
        exit(-2);
    }

    if(Y==0) return 0; // if no products are to be created the program should exit

    const int SHMSIZE = sizeof(message);                // Shared Memory size = the size of a message
    const int assembler_SHMSIZE = sizeof(int[3*Y][2]);
    printf("assembler_SHMSIZE is: %d\n", assembler_SHMSIZE);
    const int kill_SHMSIZE = sizeof(kill_message);
    // srand(time(NULL));

    /* --- Shared memory creation --- */
    shmid_paint = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
    shmid_assemble = shmget(IPC_PRIVATE, assembler_SHMSIZE, IPC_CREAT | 0666);
    shmid_check = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
    shmid_y = shmget(IPC_PRIVATE, kill_SHMSIZE, IPC_CREAT | 0666);

    shm_paint = (message*)shmat(shmid_paint,NULL,0);
    shm_assemble = (int **)shmat(shmid_assemble,0,0);
    shm_check = (message*)shmat(shmid_check,NULL,0);
    shm_y = (kill_message*)shmat(shmid
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“myheader.h”
无效或退出(字符*消息);
bool init(int-argc,char*argv[],int*Y);
int main(int argc,char*argv[]){

如果(argcOk),答案很简单。 我必须声明SmithRead Eng装配为int **。我的初始int(SmithRead Engulver)[2 ]在C中工作,就像我用的一个相关问题,而不是C++。改变这使得我的代码工作得像个符咒。< /P> 以下是更正后的代码:

#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <ctype.h>

#include "myheader.h"

void perror_exit(char *message);
bool init(int argc, char * argv[], int *Y);

int main (int argc , char* argv[]){

    if(argc<3) {
        perror_exit("Usage: ./myworkshop -Y <value>\n");
        return(-1);
    }
    int i=0,n=0,child_pid=0,Y=0,shmid_paint=0,shmid_assemble=0,shmid_check=0,shmid_y=0,painter_empty=0,painter_full=0,checker_full=0,checker_empty=0,assembler_full=0,assembler_empty=0,ex=0;
    int *pids = new int[8];

    kill_message *shm_y;
    message *shm_paint,*shm_check;
    int **shm_assemble;

    if (!init(argc,argv,&Y)){
        std::cout << "Something is wrong with the arguments, try again\n";
        exit(-2);
    }

    if(Y==0) {
        delete[] pids;
        return 0; // if no products are to be created the program should exit
    }

    const int SHMSIZE = sizeof(message);                // Shared Memory size = the size of a message
    const int assembler_SHMSIZE = sizeof(int[3*Y][2]);
    printf("assembler_SHMSIZE is: %d\n", assembler_SHMSIZE);
    const int kill_SHMSIZE = sizeof(kill_message);
    // srand(time(NULL));

    /* --- Shared memory creation --- */
    shmid_paint = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
    shmid_assemble = shmget(IPC_PRIVATE, assembler_SHMSIZE, IPC_CREAT | 0666);
    shmid_check = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
    shmid_y = shmget(IPC_PRIVATE, kill_SHMSIZE, IPC_CREAT | 0666);

    shm_paint = (message*)shmat(shmid_paint,NULL,0);
    shm_assemble = (int**)shmat(shmid_assemble,NULL,0);
    shm_check = (message*)shmat(shmid_check,NULL,0);
    shm_y = (kill_message*)shmat(shmid_y,NULL,0);

    /* --- Semaphore creation --- */
    painter_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
    painter_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);

    checker_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
    checker_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);

    assembler_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
    assembler_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);

    delete[] pids;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“myheader.h”
无效或退出(字符*消息);
bool init(int-argc,char*argv[],int*Y);
int main(int argc,char*argv[]){

如果(AgCI不认为需要从代码< SMAT < /C>)中返回返回值。代码和编译器是C++(虽然使用了一些C库和成语)你想在C++中这样做吗?或者你想在C++中这样做吗?后缀<代码> .CPP < /C>告诉编译器你有C++源,所以它将C++规则应用到那个源。不要告诉它不真实的东西。编译器不喜欢在你做的时候撒谎和找到自己的方法。是的,它们是非常不同的语言,尤其是在C是C++的,C风格的C++仍然是C++,即使它是用C++的方式来进行的。它有一个很不好的值来混淆它。不要假装它不重要,因为它是。嗯,你一定会困惑。我写C++,使用C库,用g++编译,我的文件是.CPP,但是这里我提到的这个问题是与我使用的语言无关,所以为了简单起见,我编写并标记了C。现在清楚了吗?@AnttiHaapala