LNK2019 fatal error LNK 1120

Good day, everyone.
I'm writing a String class. It's major purpose is to create and modify strings of characters. Everything went well until I overloaded the operator +. After doing that, compiler gave out LNK 2019 error.

1> Driver.cpp
1>Driver.obj : error LNK2019: unresolved external symbol "public: __thiscall String::String(class String &)" (??0String@@QAE@AAV0@@Z) referenced in function "public: class String __thiscall String::operator+(class String const &)" (??HString@@QAE?AV0@ABV0@@Z)
1>E:\Dmbrldor4\VS_2010express\Projects\Driver\Debug\Driver.exe : fatal error LNK1120: 1 unresolved externals


After thoroughly reading the error I concluded it was connected with the copy-constructor. And, having defined the copy-constructor, the problem was solved. The question is WHY ???. I would be very grateful for any thoughts and link concerning this issue. The string class is posted below.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;

short countLength ( char * );

class String
{
public:
	String ();
	~String () { cout << "String destructor" << endl;}

	String (String&);
	String ( int );
	String ( char * );

	int getLength() const {return itsLen;}
	const char * getString() const {return itsString;}
	String operator + ( const String& );
	char & operator [] ( int );
	char operator [] ( int theCharacter ) const { return itsString [theCharacter]; }
	void operator = ( String & );
private:
	char *itsString;
	int itsLen;
};

String::String()
{
	itsString = new char;
	itsString = 0;
	itsLen = 0;
}

String::String ( String & rhs )
{
	itsLen = rhs.getLength();
	itsString = new char [itsLen + 1];
	for (short i = 0; i < itsLen; ++i)
		itsString [i] = rhs.itsString [i];
	itsString [itsLen] = '\0';
}

String::String (int Length)
{
	itsString = new char [Length + 1];
	itsLen = Length;
	for ( short i = 0; i <= itsLen; ++i)
		itsString [i] = '\0';
}

String::String (char * theString)
{
	//delete [] itsString;
	itsLen = countLength ( theString );
	itsString = new char [itsLen + 1];
	for (short i = 0; i < itsLen ; ++i) itsString [i] = theString [i];
	itsString [itsLen] = '\0';
}

char& String::operator [] ( int rhs )
{
	return itsString [rhs];
}

String String::operator + ( const String &theString )
{
	int i, j, TotalLength;
	TotalLength = this -> itsLen + theString.getLength();
	String Temp ( TotalLength );
	for ( i = 0; i < itsLen; ++i )
		Temp [i] = itsString [i];
	for ( j = 0; j < theString.getLength(); ++j, ++i )
		Temp [i] = theString [j];
	Temp [TotalLength] = '\0';
	return Temp;
}

void String::operator = (String & rhs)
{
	itsLen = rhs.getLength();
	itsString = new char [itsLen + 1];
	for ( short i = 0; i < itsLen; ++i)
		itsString [i] = rhs [i];
	itsString [itsLen] = '\0';

}

short countLength ( char *theString )
{
	short i;
	for ( i = 0; theString [i] != '\0'; ++i );
	return i;
}
J have not understood whether it is this code when the error occured or it is the code after your updating?
Last edited on
Just a quick note: I think your ~String() destructor should delete[] itsString. And, also that you remove line 29.

The question is WHY ???. I would be very grateful for any thoughts and link concerning this issue.

The answer lies in the error message itself. The operator+ uses a copy constructor implicitly, which isn't generated anymore for some reason... although it should, by default?
http://en.wikipedia.org/wiki/Copy_constructor
Last edited on
imagine what would happen wen u call say:
 
string3=string1+string2;

hoping everything is right ur function will be invoked
 
operator + ( const String &theString )

now who will copy the string 2 reference(its an object not a simple variable) to const String &theString. The copy constructor does it.

J have not understood whether it is this code when the error occured or it is the code after your updating?

This is the code that works.
thank for shedding some light on this @Catfish2
You should show the code that was not compiled. Because it is not clear what parts of your demonstrated code were present or absent in the original code. And Catfish2 did not shed some light as you think because the compiler shall generate copy constructer itself if you did not declare it.
Last edited on
J have not understood whether it is this code when the error occured or it is the code after your updating?


Posted code is the working one.
vlad from moscow (932) May 29, 2012 at 7:37pm
You should show the code that was not compiled. Because it is not clear what parts of your demonstrated code were present or absent in the original code. And Catfish2 did not shed some light as you think because the compiler shall generate copy constructer itself if you did not declare it.

Here is the code, that does not work. As you can see the user-defined copy-constructor is not defined (I commented it out)

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;

short countLength ( char * );

class String
{
public:
	String ();
	~String () { cout << "String destructor" << endl; delete [] itsString;}

	String ( const String& );
	String ( int );
	String ( char * );

	int getLength() const {return itsLen;}
	const char * getString() const {return itsString;}
	String operator + ( const String& );
	char & operator [] ( int );
	char operator [] ( int theCharacter ) const { return itsString [theCharacter]; }
	void operator = ( String & );
private:
	char *itsString;
	int itsLen;
};

String::String()
{
	itsString = 0;
	itsLen = 0;
}

/*String::String ( const String & rhs )
{
	itsLen = rhs.getLength();
	itsString = new char [itsLen + 1];
	for (short i = 0; i < itsLen; ++i)
		itsString [i] = rhs.itsString [i];
	itsString [itsLen] = '\0';
}*/

String::String (int Length)
{
	itsString = new char [Length + 1];
	itsLen = Length;
	for ( short i = 0; i <= itsLen; ++i)
		itsString [i] = '\0';
}

String::String (char * theString)
{
	//delete [] itsString;
	itsLen = countLength ( theString );
	itsString = new char [itsLen + 1];
	for (short i = 0; i < itsLen ; ++i) itsString [i] = theString [i];
	itsString [itsLen] = '\0';
}

char& String::operator [] ( int rhs )
{
	return itsString [rhs];
}

String String::operator + ( const String &theString )
{
	int i, j, TotalLength;
	TotalLength = this -> itsLen + theString.getLength();
	String Temp ( TotalLength );
	for ( i = 0; i < itsLen; ++i )
		Temp [i] = itsString [i];
	for ( j = 0; j < theString.getLength(); ++j, ++i )
		Temp [i] = theString [j];
	Temp [TotalLength] = '\0';
	return Temp;
}

void String::operator = (String & rhs)
{
	itsLen = rhs.getLength();
	itsString = new char [itsLen + 1];
	for ( short i = 0; i < itsLen; ++i)
		itsString [i] = rhs [i];
	itsString [itsLen] = '\0';

}

short countLength ( char *theString )
{
	short i;
	for ( i = 0; theString [i] != '\0'; ++i );
	return i;
}


I see the mistake now. I declared a copy-constructor, but have not defined it.
Last edited on
Topic archived. No new replies allowed.