link error 1 unresolved external

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//demonstration of bit manipulation
#include "printBinary.h"
#include <iostream>
using namespace std;

#define PR(STR, EXPR) \
	cout << STR; printBinary(EXPR); cout << endl;

int main()
{
	unsigned int getval;
	unsigned char a, b;
	cout << "Enter a number between 0 and 255: ";
	cin >> getval; a = getval;
	PR("a in binary: ", a);
	cout << "Enter a number between 0 and 255: ";
	cin >> getval; b = getval;
	PR("b in binary: ", b);
	PR("a | b = ", a | b);
	PR("a & b = ", a & b);
	PR("a ^ b = ", a ^ b);
	PR("~a = ", ~a);
	PR("~b = ", ~b);

	//an interesting bit pattern
	unsigned char c = 0x5A;
	PR("c in binary: ", c);
	a |= c;
	PR("a |= c; a = ", a);
	b &= c;
	PR("b &= c; b = ", b);
	b ^= a;
	PR("b ^= a; b= ", b);
}




1
2
3
//display a byte in binary
//printBinary.h
void printBinary(const unsigned char val);


the code above produces Error 1 error LNK2019: unresolved external symbol "void __cdecl printBinary(unsigned char)" (?printBinary@@YAXE@Z) referenced in function _main main.obj Bitwise

your help would be appreciated.
thanks,
did you use #ifndef in printBinary.h ?
1
2
3
4
5
#ifndef PRINTBINARY_H
#define PRINTBINARY_H
//declaration here...
void printBinary(const unsigned char val);
#endif 
that produces the same error.
Last edited on
the code works for me ...
then your on crack because I haven't written the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//demonstration of bit manipulation
#include "printBinary.h"
#include <iostream>
using namespace std;

#define PR(STR, EXPR) \
	cout << STR; printBinary(EXPR); cout << endl;

int main()
{
	unsigned int getval;
	unsigned char a, b;
	cout << "Enter a number between 0 and 255: ";
	cin >> getval; a = getval;
	PR("a in binary: ", a);
	cout << "Enter a number between 0 and 255: ";
	cin >> getval; b = getval;
	PR("b in binary: ", b);
	PR("a | b = ", a | b);
	PR("a & b = ", a & b);
	PR("a ^ b = ", a ^ b);
	PR("~a = ", ~a);
	PR("~b = ", ~b);

	//an interesting bit pattern
	unsigned char c = 0x5A;
	PR("c in binary: ", c);
	a |= c;
	PR("a |= c; a = ", a);
	b &= c;
	PR("b &= c; b = ", b);
	b ^= a;
	PR("b ^= a; b= ", b);
}

void printBinary(const unsigned char val)
{
	for(int i = 7; i >= 0; i--)
		if(val & ( 1 << i))
			cout << "1";
		else
			cout << "0";
}


now it works

without

1
2
3
4
#ifndef PRINTBINARY_H
#define PRINTBINARY_H
//declaration here...
#endif  
Last edited on
Your issue was that you don't have the actual code of the function available, so when the linker tried to find where that function is it failed.
thanks,
stay sharp
Last edited on
Topic archived. No new replies allowed.