0

The object of this assignment is to convert decimal to binary. I must follow the pseudo code for the algorithm to do the process of conversion but the rest is up to us in regards how to structure the program. However, we must use classes and have at least a single inheritance. I have the code mostly finished, but I can't seem to test it more than likely due to something super simple that I simply just can't see. I've been trying various things but it seems I'm just chopping at the code with no real idea of what is actually wrong. Here's my source code, any help is appreciated.

When you run the code, i get a LNK2019: unresolved external symbol error.

#include <iostream>
#include <vector>

using namespace std;


class binaryConverter
{
public:
    void print();
    binaryConverter();
    binaryConverter(int);
    void addBit(int);

private:
    vector<int> binary;
};


class decimalToBinary : public binaryConverter {

public:
    void print();
    void process(int);
    decimalToBinary();


private:

};

void binaryConverter::addBit(int d){
    binary.push_back(d);
}
void decimalToBinary::process(int num)
{
    if (num >= 128){
        num = 128 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 64 && num < 128){
        num = 64 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 32 && num < 64){
        num = 32 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 16 && num < 32){
        num = 16 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 8 && num < 16){
        num = 8 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 4 && num < 8){
        num = 4 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 2 && num < 4){
        num = 2 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
    if (num >= 1 && num < 2){
        num = 1 - num;
        decimalToBinary::addBit(1);
    }
    else{
        decimalToBinary::addBit(0);
    }
}

void binaryConverter::print(){
    for (vector<int>::iterator it = binary.begin(); it != binary.end(); it++){
        cout << *it << endl;
    }
}


int main(){

    decimalToBinary test;

    test.process(150);
    test.print();


    return 0;
}
kensai01
  • 65
  • 1
  • 3
  • 9

2 Answers2

0

By reading your code I see :

binaryConverter();
binaryConverter(int);

and

decimalToBinary();

You declared constructors but did not defined them.

And your decimalToBinary::print() method is also not defined.

blakelead
  • 1,780
  • 2
  • 17
  • 28
0

When you create object to the class, if a class is not equipped with a constructor, the compiler will generate code for one, call the implicit default constructor.

decimalToBinary test; calls decimalToBinary:: decimalToBinary();

You do not need to declare and define constructors to create objects. If you declare constructors then you have to define them. Otherwise you will get linking error for constructors.

If you would like to use print method defined in the binaryConverter class you need not to declare print method in the decimalToBinary class.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50