0

My problem is not with the program but I put it in case it is necessary. I always used VS on the Windows version, now I'm using a Linux version. I'm having a problem so that a simple program recognizes the methods of a class, an error appears: A simpler program, without an .h header, compiles normally. What do I need to do more to fix this problem? In the Windows version, the program runs

The error:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: in the `_start 'function:
(.text + 0x20): undefined reference to `main '
collect2: error: ld returned 1 exit status

[main.cpp 2020-05-23 17: 28: 09.707]
,, / tmp / ccJJYd6a.o: in the `main 'function:
main.cpp :(. text + 0x1f): undefined reference to `Complex :: Complex () '
main.cpp :(. text + 0x2b): undefined reference to `Complex :: Complex () '
main.cpp :(. text + 0x37): undefined reference to `Complex :: Complex () '
main.cpp :(. text + 0x43): undefined reference to `Complex :: lerComplexo () '
main.cpp :(. text + 0x4f): undefined reference to `Complex :: print () '
main.cpp :(. text + 0x5b): undefined reference to `Complex :: lerComplexo () '
main.cpp :(. text + 0x67): undefined reference to `Complex :: print () '
collect2: error: ld returned 1 exit status

Main.cpp

    #include stdio.h
    #include"Complexo.h"

     int main()
    {
     Complexo a,b,c;

a.lerComplexo();
a.imprimir();

b.lerComplexo();
b.imprimir();



 }

Complexo.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "Complexo.h"
#include<stdio.h>
#include<math.h>


 Complexo::Complexo()
 {
   re = 0;
   im = 0;
 }

 Complexo::Complexo(float r, float i)
 {
   re = r;
   im = i;
 }

 void Complexo::atribuir(float r, float i)
 {
   float novo_re = r;
   float novo_im = i;
 }

 void Complexo::lerComplexo()
 {
   printf("Parte real:");
   scanf("%f", &re);
   printf("\n");
   printf("Parte imaginaria:");
   scanf("%f", &im);
   printf("\n");

 }

 void Complexo::imprimir()   // escrever complexo para o monitor  
 {
   printf("%.1f +%.1fi\t", re, im);
 }


 float Complexo::modulo()  // devolve módulo do número 
 {
   float m;
   m = float(sqrt(pow(re, 2) + pow(im, 2)));
   return m;
 }
 float Complexo::argumento() // devolve argumento do número 
 {
   float arg;
   arg = float(atan2(im, re));
   return arg;
 }

 Complexo Complexo::conjugado()  // devolve o conjugado do número  
 {
   Complexo c;
   c.re = re;
   c.im = -im;
   return c;
  }
 Complexo Complexo::simetrico()  // devolve o simétrico do numero
 {
   Complexo s;
   s.re = re * (-1);
   s.im = im * (-1);
   return s;
 }
 Complexo Complexo::potencia(float exp)  // devolve potência do número
 {
   Complexo p; 
   float mod;
   mod = pow(modulo(), exp);
   p.re = mod * cos(exp * argumento());
   p.im = mod * sin(exp * argumento());
   return p;
 }

       // operações entre 2 complexos  
 Complexo Complexo::operator +(const Complexo& c) // adição   
 {
   Complexo res;
   res.re = re + c.re;
   res.im = im + c.im;
   return res;

  }
  Complexo Complexo::operator -(const Complexo& c) // subtração  
  {
    Complexo res;
    res.re = re - c.re;
    res.im = im - c.im;
    return res;
   }
  Complexo Complexo::operator *(const Complexo& c) // multiplicação 
  {
    Complexo res;
    res.re = re * c.re - im * c.im;
    res.im = re * c.im + im * c.re;
    return res;
   }
  Complexo Complexo::operator /(const Complexo& c) // divisão    
 {
    Complexo num, den, res;
    num = Complexo(re, im);
    den = c;
    num = *this * den.conjugado();
    den = den * den.conjugado();
    return Complexo(num.re / den.re, num.im / den.re);



    }

    // operações entre um complexo (operador da esquerda) e um real (operador da  // 
        direita)  

  Complexo Complexo::operator *(float f)
  {
    Complexo res;
    res.re = re * f;
    res.im = im * f;
    return res;

   }
  Complexo Complexo::operator /(float f)
  {
    Complexo res;
    res.re = re / f;
    res.im = im / f;
    return res;

  }

Complexo.h

        #pragma once  
        class Complexo
        {
        private:
          float re, im;
        public:
          Complexo();   // Construtor cria um número nulo  
          Complexo(float r, float i);

    void atribuir(float r, float i);
    void lerComplexo();  // ler complexo do teclado  
    void imprimir();   // escrever complexo para o monitor  

    float real() { return re; } // devolve coeficiente real  
    float imaginario() { return im; } // devolve coeficiente imaginário  
    float modulo();  // devolve módulo do número  
    float argumento(); // devolve argumento do número  
    Complexo conjugado();  // devolve o conjugado do número  
    Complexo simetrico();  // devolve o simétrico do número 
    Complexo potencia(float exp);  // devolve potência do número 

    // operações entre 2 complexos  
    Complexo operator +(const Complexo& c); // adição   
    Complexo operator -(const Complexo& c); // subtração  
    Complexo operator *(const Complexo& c); // multiplicação  
    Complexo operator /(const Complexo& c); // divisão     

    // operações entre um complexo (operador da esquerda) e um real (operador da  // direita)  
    Complexo operator *(float f);
    Complexo operator /(float f);

};
Adam
  • 2,820
  • 1
  • 13
  • 33
Sol
  • 1
  • 1
  • 4
    How are you compiling your program? Can you provide us with your compile command? – Yuxuan Lu May 23 '20 at 18:24
  • 1
    Maybe try to use the usual guard rather than `#pragma once`. – Eraklon May 23 '20 at 18:28
  • This is C++, not C, so I updated the tag. – Nate Eldredge May 23 '20 at 20:06
  • where is the return staetement in main.cpp? – Adam May 23 '20 at 20:13
  • @Adam: Not required. https://stackoverflow.com/questions/18402853/must-the-int-main-function-return-a-value-in-all-compilers – Nate Eldredge May 23 '20 at 20:14
  • The program compiles and runs fine for me. – Nate Eldredge May 23 '20 at 20:15
  • why to include stdio.h in c++ in main.cpp? why to use c headers in c++? generaly speaking this is a bad habbit. instead you can use (just if this is must) cstdio.h and you did it wrong without <>. please fix it. and use iostream instead. – Adam May 23 '20 at 20:17
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 273K May 24 '20 at 06:39

1 Answers1

-1

Did you compile the main.cpp at all? for example, on linux/g++ compiler the compilation command line should looks at least like below: g++ main.cpp complexo.cpp -pedantic -Wall -Wconversion

remarks Including C headers in C++ is a bad thing to do in most cases. for example you have included <stdio.h>, and you dont need it, because c++ did all the work for you in <iostream> and you should use its functionality (like cin, cout objects and more)!

general tip: always compile with the flags above! and let the compiler helps you.

Adam
  • 2,820
  • 1
  • 13
  • 33