What's wrong ?? LNK2019 error (Unresolved externals)

  • Thread starter Thread starter Jason Jacob
  • Start date Start date
J

Jason Jacob

To all

I am new to MS's VC++ and got a question.
I've tried to write a simple console program "HelloWorld.cpp", then
I've added a simple class (Class01.h and cpp)

Class01 compile well, but when I want to create an instance inside
HelloWorld.cpp, a LNK2019 error pops up (Unresolved Externals). I
guess somehow the HelloWorld.cpp can't find the Class01 !?

I've added the directory of the Class01.h and .cpp to the options
"Additional Include Directories", but still linking problem.

Code:
// HelloWorld.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include "Class01.h"

using namespace std;

void list_CString (const char* oCStr) {
size_t iSize = 0;

if (oCStr != NULL) {
iSize =  _tcslen (oCStr);
} else {
iSize = 0;
}

cout << "cInput has the following contents : " << endl;
for (size_t i=0; i<iSize; i++) {
cout << oCStr[i] << " ";
}
cout << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
char* cInput = new char[10];
memset (cInput, 0, sizeof (cInput));
cout << "Hello World" << endl;
cin >> cInput;

::list_CString (cInput);
cin >> cInput;

Class01* oClass01 = NULL;
oClass01 = new Class01 (); // -- linking error!?

return 0;
}


// -- Class01.h
#include "stdafx.h"
#include <tchar.h>

#ifndef _CLASS01_
#define _CLASS01_
// -- cheat the compiler, Class01 is a managed class type !
class Class01 {
public:
// Constructor(s)
Class01 ();

// Getter and Setter(s)
void setName (_TCHAR* otc_char);

private:
_TCHAR* tc_Name;
#endif
};

// -- Class01.cpp
#include "Class01.h"

void Class01::setName (_TCHAR* otc_char) {
if (otc_char != NULL) {
tc_Name = otc_char;
} else {
return;
}
}

From Jason (Kusanagihk)
 
Thanks pal, after adding the constructor code, it worked!

But another question, as from my code, I have set the cInput (char
array) to only a length of 10, but how come I can enter a string of
more than 20 or even 40 characters ?? (It looks quite illogical, but
somehow it works fine till now)

From Jason (Kusanagihk)
 
Jason said:
But another question, as from my code, I have set the cInput (char
array) to only a length of 10, but how come I can enter a string of
more than 20 or even 40 characters ?? (It looks quite illogical, but
somehow it works fine till now)

You are overflowing the buffer and stomping on memory you don't own.
Don't do that!!! Really bad things can happen. You are just unlucky
that it seems to have worked so far.

In today's C++, you should never use char arrays to manipulate strings.
Doing so can be really tedious, and it's just too easy to forget how big
your buffer is and write past the end of it. Always use a string class
instead. I prefer std::string, but any class, even CString, is better
than using char arrays.
 
Back
Top