views:

645

answers:

3

I've try to compile this code:

#include <iostream>
#include <cstdlib>

using namespace std;

#define ARRAY_TAM 2

typedef int (*operacion)(int, int);
typedef const char* (*Pfchar)();

int suma(int, int);
int resta(int, int);

const char* descrSuma();
const char* descrResta();
const char* simbSuma();
const char* simbResta();

class OP
{
    private:

    public:
     operacion op;
     Pfchar descr;
     Pfchar simb;

};

int main (int argv, char *argc[])
{
    OP ArrayOP[ARRAY_TAM];

    ArrayOP[0].op = suma;
    ArrayOP[0].descr = descrSuma;
    ArrayOP[1].op = resta;
    ArrayOP[1].descr = descrResta;

    int op1, op2;
    unsigned int i;
    char opcion;
    bool fin = false;

    while (fin != true)
    {
     cout << "CALCULADORA" << "\n";
     cout << "===========" << "\n";

     for (i = 0; (i < ARRAY_TAM); i++)
     {
      cout << i+1;
      cout << ".- ";
      cout << ArrayOP[i].descr() << "\n";
     }

     cout << i+1 << ".- " << "Salir" << endl;

     cout << "Opcion: ";

     cin >> opcion;
     opcion = atoi(&opcion);
     opcion--;
     cout << (int)opcion << endl;

     if ((opcion >= 0) && (opcion < ARRAY_TAM))
     {
      cout << "Operando 1: ";
      cin >> op1;
      cout << "Operando 2: ";
      cin >> op2;
      cout << "Resultado: op1 " << ArrayOP[opcion].simb()
                         << " op2 = " << ArrayOP[opcion].op(op1, op2);
     } 
     else if (opcion == ARRAY_TAM)
     {
      fin = true;
     }

    }

    return 0;

}


int suma (int op1, int op2)
{return op1 + op2;}

int resta (int op1, int op2)
{return op1 - op2;}

const char* descrSuma()    
{return "Suma";}

const char* descrResta() 
{return "Resta";}

const char* simbSuma()
{return "+";}

const char* simbResta()
{return "-";}

An it works, but I have a lot of problems linking with gcc with debbugging symbols and it doesn't link :-(

Need Help!

Large linker error:

facon@facon-laptop:~/Windows - Mis documentos/Prog/C/Ejercicios/pedirentero$ g++ -o main main.o main.o: In function `_start':

/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65: multiple definition of `_start'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65:

first defined here main.o:(.rodata+0x0): multiple definition of `_fp_hw'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata+0x0): first defined here main.o: In function _fini': (.fini+0x0): multiple definition of _fini'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.fini+0x0): first defined here main.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here main.o: In function __data_start': (.data+0x0): multiple definition of __data_start'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.data+0x0): first defined here main.o: In function __data_start': (.data+0x4): multiple definition of __dso_handle'

/usr/lib/gcc/i486-linux-gnu/4.4.1/crtbegin.o:(.data+0x0): first defined here main.o: In function _init': (.init+0x0): multiple definition of _init'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.init+0x0): first defined here

/usr/lib/gcc/i486-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): multiple definition of `DTOR_END' main.o:(.dtors+0x4): first defined here

/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored. /usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created.

collect2: ld returned 1 exit status

PD: Edited.

+3  A: 

Did you use gcc instead of g++?

If gcc is used with C++ code it will give weird linking errors. C++ code must be compiled with g++.


EDIT: Based on the new information you provided I see that you are running g++ -o main main.o main.o.

You should instead run: g++ -o main main.cpp

Andreas Bonini
It do the same with gcc a g++.
Facon
What is the "same"? You never explained exactly what the error is
Andreas Bonini
I have the same error:http://www.subirimagenes.com/imagen-pantalla-3747487.html
Facon
In that screenshot you ran main.o again instead of main.cpp
Andreas Bonini
Yeah that works but I need to debug it because I have an error in Pointer to function.I did:g++ -g main.cpp main.og++ -o main main.oWith this I have the error that I posted.
Facon
please do not use: g++ -g main.cpp main.o. That's wrong. use: g++ -g main.cpp -o main
Yeah!, NOW works ;) , thanks for all!!!
Facon
+2  A: 

You write "... it works", but then you write "... problems with linking".

I am little bit confused with this question, because:

  • If there are problems with linking then it doesn't work ...
  • But if it works, then you don't have problems with linking...

So I guess that you mean: "it compiles, but there are linking errors" ?

If that's the case, then you could try

g++ -g main.cpp -o main

instead of

gcc -g main.cpp -o main

EDIT: ... and do not mention main.o on the command line =;)

EDIT: if that all doesn't help - maybe there is something wrong with your g++/gcc installation?

on ubuntu please try

sudo aptitude install build-essential
It do the same at linking, I think, I haven't got the debugging library or something like that.
Facon
what kind of development environment is this? linux? cygwin? mingw?
Ubuntu => gedit + console + gcc/gcc + gdb
Facon
which version of Ubuntu? karmic? Is it a 32bit or a 64bit Ubuntu?
... and how did you install gcc/g++ ? I would recommend that you use **sudo aptitude install build-essential**
Karmic Koala 9.10, 32 Bits.I did that when I started programming in C/C++
Facon
I am using that too. But it works perfectly well here. Have you tried the "sudo aptitude install build-essential' ?
A: 

Thanks to Vokuhila-Oliba, now, it works!

Facon
you're welcome :-)