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);