1

I'm new to C++ and want to create my first program. It decodes a number into binary. And it works fine!

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;


void decode_binary(int num) {
    vector<int> binary;

     for (int i = 0; num > 0; i++ ) {
        binary.push_back(num % 2);
        num = num/2;
    }

    //Reverse the Vector

    for (int i = binary.size()-1; i >= 0; i--) {
        cout << binary[i];
    }
}


int main () {

    int number_user;

    cout << "Type in the Number in Decimal: ";
    cin >> number_user;

    cout << "Your Number in Binary: ";
    decode_binary(number_user);

    cout << "\n";

    return 0;
}

But now I want to get the function decode_binary() to store in the binary.cpp with a header file binary.h but I get the error: undefined reference to `decode_binary(int)' collect2.exe: error: ld returned 1 exit status. Where is my mistake and how can I fix it?

Main.cpp:

#include <iostream>
#include <vector>
#include <cmath>
#include "binary.h"

using namespace std;

int main () {
    int num_user;
    cout << "Type in the Number in Decimal: ";
    cin >> num_user;
    cout << "Your code in binary: ";
    decode_binary(num_user);
    cout << "\n";    
    return 0;

}

Binary.cpp:

#include <iostream>
#include <vector>
#include "binary.h"

using namespace std;  

void decode_binary (int num) {
    vector<int> binary;

     for (int i = 0; num > 0; i++ ) {
        binary.push_back(num % 2);
        num = num/2;
    }

    for (int i = binary.size()-1; i >= 0; i--) {
        cout << binary[i];
    }
} 

binary.h:

#pragma once

void decode_binary(int num);
pzaenger
  • 11,381
  • 3
  • 45
  • 46
bgsbnny
  • 21
  • 1
  • 1
    How do you compile your program? – Quimby Sep 13 '20 at 12:53
  • @Quimby Without linking the compiled `Binary.cpp` obviously. – πάντα ῥεῖ Sep 13 '20 at 12:54
  • Can you show us the exact compilation command you are using so that we tell you where the problem is? Because your code is correct. – kyriakosSt Sep 13 '20 at 12:56
  • @kyriakosST cd "c:\Users\Daniil\Desktop\Programmieren\Binary Decoder\" ; if ($?) { g++ main3.cpp -o main3 } ; if ($?) { .\main3 } c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Daniil\AppData\Local\Temp\ccNmoC0f.o:main3.cpp:(.text+0x58): undefined reference to `decode_binary(int)' collect2.exe: error: ld returned 1 exit status – bgsbnny Sep 13 '20 at 12:59
  • 1
    Do you type this by hand every time?? Or are you using an IDE? – kyriakosSt Sep 13 '20 at 13:02
  • I'm using Visual Studio Code – bgsbnny Sep 13 '20 at 13:03
  • All right. We now have enough information to answer. Give me a moment – kyriakosSt Sep 13 '20 at 13:05
  • Your bug is likely in your `tasks.json` that is if you did not modify it according to this from the documentation: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Sep 13 '20 at 15:00

0 Answers0