0

The problem is simple: I have 3 files to work with (main.cpp, complex.cpp, complex.h) which I am going to attach here:

main.cpp:

#include "complex.h"


int main(){
    Complesso c1, c2(3, 4), somma, prodotto;

    c1.set_Re(5);
    c1.set_Imm(5);

    cout << c1.get_Re() << " " << c1.get_Imm() << endl;
    cout << c2.get_Re() << " " << c2.get_Imm() << endl;

    somma = c1+c2; //c1.operator+(c2)
    prodotto = c1*c2;
    if (c1==c2)
        cout << "Uguale" << endl;

    return 0;
}

complex.cpp:

#include "complex.h"


Complesso Complesso::operator+(const Complesso C) const{
    Complesso somma;
    somma.Re = Re+C.Re;
    somma.Imm = Imm+C.Imm;
    return somma;
}

Complesso Complesso::operator*(const Complesso C) const{
    Complesso prodotto;
    prodotto.Re = Re*C.Re - Imm*C.Imm;
    prodotto.Imm = Imm*C.Re + C.Imm*Re;
    return prodotto;
}

bool Complesso::operator==(const Complesso C) const{
    return (Re==C.Re && Imm==C.Imm);
}

and finally complex.h:

#ifndef _COMPLEX_H
#define _COMPLEX_H

#include <iostream>
#include <cmath>
using namespace std;

class Complesso{
    private:
        double Re;
        double Imm;
    public:
        // Constructor
        Complesso(){};
        Complesso(const double R, const double I){Re=R; Imm=I;}; // With arguments

        // Setter
        void set_Re(const double R) {Re=R;};
        void set_Imm(const double I) {Imm=I;};
        //Getter
        double get_Re() const {return Re;};
        double get_Imm() const {return Imm;};

        // Operator functions
        Complesso operator+(const Complesso) const;
        Complesso operator*(const Complesso) const;
        bool operator==(const Complesso) const;
};

#endif

When I run the main.cpp file in vscode I get this error:

c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Simon\AppData\Local\Temp\ccGgcciK.o:tempCodeRunnerFile.cpp:(.text+0x15b): 
undefined reference to `Complesso::operator+(Complesso) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Simon\AppData\Local\Temp\ccGgcciK.o:tempCodeRunnerFile.cpp:(.text+0x192):
undefined reference to `Complesso::operator*(Complesso) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Simon\AppData\Local\Temp\ccGgcciK.o:tempCodeRunnerFile.cpp:(.text+0x1c2):
undefined reference to `Complesso::operator==(Complesso) const'
collect2.exe: error: ld returned 1 exit status

So it's like vscode does not recognise the functions. The thing is that if I run the code using DEV-C++ It runs without errors.

Simon
  • 23
  • 2
  • Look closer at your error, it talks about some ```tempCodeRunnerFile.cpp```, so actually your project setup is wrong and you don't use that 3 files you want. – Karen Melikyan Apr 26 '21 at 12:42
  • Also this looks useful: [gcc - Compiling C++ in VSCode gives me an undefined reference - Stack Overflow](https://stackoverflow.com/questions/59370455/compiling-c-in-vscode-gives-me-an-undefined-reference) – MikeCAT Apr 26 '21 at 12:43
  • VSCode by default attempts to build only the active file into the executable. You need to edit `tasks.json` to get it to build all the files in the folder. The official VSCode documentation explains how to do that here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Apr 26 '21 at 13:09
  • ok actually by manually compiling using `g++ main.cpp complex.cpp` seems to do the trick. – Simon Apr 26 '21 at 13:15
  • I am going to answer the post. But I still don't have a clue why this heppens @MikeCAT – Simon Apr 26 '21 at 13:16
  • Read my comment. This is the default behavior of VSCode and is in the documentation. ***"You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}\\*.cpp" instead of ${file}."*** With the default `tasks.json` you build only `${file}` into your executable. `${file}` is a variable containing the name of file that is active. – drescherjm Apr 26 '21 at 15:18

0 Answers0