0

I need to execute some examples founded in this page: http://www.chuidiang.com/clinux/ipcs/colas.php

The code for one of them is this one:

#include <iostream.h>
#include <sys/msg.h>
#include <errno.h>

typedef struct Mi_Tipo_Mensaje
{
long Id_Mensaje;
int Dato_Numerico;
char Mensaje[10];
};

main()
{
key_t Clave1;
int Id_Cola_Mensajes;
Mi_Tipo_Mensaje Un_Mensaje;

Clave1 = ftok ("/bin/ls", 33);
if (Clave1 == (key_t)-1)
{
    cout << "Error al obtener clave para cola mensajes" << endl;
    exit(-1);
}

Id_Cola_Mensajes = msgget (Clave1, 0600 | IPC_CREAT);
if (Id_Cola_Mensajes == -1)
{
    cout << "Error al obtener identificador para cola mensajes" << endl;
    exit (-1);
}

Un_Mensaje.Id_Mensaje = 1;
Un_Mensaje.Dato_Numerico = 29;
strcpy (Un_Mensaje.Mensaje, "Hola");

msgsnd (Id_Cola_Mensajes, (struct msgbuf *)&Un_Mensaje, 
        sizeof(Un_Mensaje.Dato_Numerico)+sizeof(Un_Mensaje.Mensaje), 
        IPC_NOWAIT);

msgrcv (Id_Cola_Mensajes, (struct msgbuf *)&Un_Mensaje,
        sizeof(Un_Mensaje.Dato_Numerico) + sizeof(Un_Mensaje.Mensaje), 
        2, 0);

cout << "Recibido mensaje tipo 2" << endl;
cout << "Dato_Numerico = " << Un_Mensaje.Dato_Numerico << endl;
cout << "Mensaje = " << Un_Mensaje.Mensaje << endl;

msgctl (Id_Cola_Mensajes, IPC_RMID, (struct msqid_ds *)NULL);
    }

I'm trying to compile this as suggested in the page I'd mentioned ("make" or "g++ cola1.c -o cola1"), but I get this message:

fatal error: iostream.h: No such file or directory

I know that this problem has been asked quite a lot, but I can't solve it yet, could you please give me some insights? Thank you for your time and disposition,

Sebastián Pavez

Giel
  • 2,787
  • 2
  • 20
  • 25
  • 3
    Your first problem is whatever books or tutorials you use, they are **severely** outdated! There has been no `` header file in C++ for many years. I recommend you look at a [good reference](http://en.cppreference.com/w/cpp). – Some programmer dude Apr 30 '13 at 20:12
  • try #include rather then #include – AlexLordThorsen Apr 30 '13 at 20:13
  • 1
    If you're using `iostream` then you don't have a `C` program, you have a `C++` program. – Nik Bougalis Apr 30 '13 at 20:22
  • 1
    I wish I could downvote that *website*, it’s horrible. – Konrad Rudolph Apr 30 '13 at 20:33
  • `typedef struct ...` is also an anachronism in C++, though in C it still makes a difference. `main()` without return type, defaulting to int, is also something that even C doesn't do any more, I believe. Take a look at http://www.icce.rug.nl/documents/cplusplus/, which should give you a starter. – Ulrich Eckhardt Apr 30 '13 at 21:00

2 Answers2

3

You have a bunch of problems in your code.

  1. iostream.h is not part of the C++ standard and you should instead import iostream.
  2. You never add the std namespace to the appropriate functions. You can simply add the line using namespace std; at the beginning of your program (although this is discouraged, look at your favorite reference what functions live in the std namespace and prepend them with std::).
  3. Plain main() is wrong, it has to return an int. Change it to int main() and add at the end of the main function the line return 0. As suggested by Joachim Pileborg in the comments you can omit the return statement because the compiler will add it if it's missing.
  4. You use functions that are declared in the headerfiles cstring (strcpy) and cstdlib (exit), so include them:

    #include <cstring>
    #include <cstdlib>
    
  5. The typedef in the line typedef struct Mi_Tipo_Mensaje is redundant and not necessary. Remove it.

BTW: A lot of things in your code suggest that you are writing a C++ program instead of a C one.

halex
  • 16,253
  • 5
  • 58
  • 67
  • Using `using namespace std;` is actually discouraged. It's okay to import certain classes or objects (most usually `std::cout`/std::cin`) from `std`, but not the complete namespace. Also, `return 0;` isn't needed at the end of the `main` function, if it's missing the compiler adds it for you. – Some programmer dude Apr 30 '13 at 20:32
  • 1
    @JoachimPileborg You are right, and writing *it is not recommended* is to weak. *Discouraged* is the better word :). Just wanted to make my job easy that OP can compile and run his code with as little changes as necessary. Regarding the optional `return`. I thought it's only true for C but you are right. – halex Apr 30 '13 at 20:35
0

C++ standard header filenames don't end in .h. Just use #include <iostream>.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469