By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. // HelloWorld.cpp : Defines the entry point for the console
  2. application.
  3. //
  4. #include "stdafx.h"
  5. #include "Class01.h"
  6.  
  7. using namespace std;
  8.  
  9. void list_CString (const char* oCStr) {
  10. size_t iSize = 0;
  11.  
  12. if (oCStr != NULL) {
  13. iSize =  _tcslen (oCStr);
  14. } else {
  15. iSize = 0;
  16. }
  17.  
  18. cout << "cInput has the following contents : " << endl;
  19. for (size_t i=0; i<iSize; i++) {
  20. cout << oCStr[i] << " ";
  21. }
  22. cout << endl;
  23. }
  24.  
  25. int _tmain(int argc, _TCHAR* argv[])
  26. {
  27. char* cInput = new char[10];
  28. memset (cInput, 0, sizeof (cInput));
  29. cout << "Hello World" << endl;
  30. cin >> cInput;
  31.  
  32. ::list_CString (cInput);
  33. cin >> cInput;
  34.  
  35. Class01* oClass01 = NULL;
  36. oClass01 = new Class01 (); // -- linking error!?
  37.  
  38. return 0;
  39. }
  40.  
  41.  
  42. // -- Class01.h
  43. #include "stdafx.h"
  44. #include <tchar.h>
  45.  
  46. #ifndef _CLASS01_
  47. #define _CLASS01_
  48. // -- cheat the compiler, Class01 is a managed class type !
  49. class Class01 {
  50. public:
  51. // Constructor(s)
  52. Class01 ();
  53.  
  54. // Getter and Setter(s)
  55. void setName (_TCHAR* otc_char);
  56.  
  57. private:
  58. _TCHAR* tc_Name;
  59. #endif
  60. };
  61.  
  62. // -- Class01.cpp
  63. #include "Class01.h"
  64.  
  65. void Class01::setName (_TCHAR* otc_char) {
  66. if (otc_char != NULL) {
  67. tc_Name = otc_char;
  68. } else {
  69. return;
  70. }
  71. }
  72.  
From Jason (Kusanagihk)
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)
Nov 17 '05 #2

P: n/a
Jason Jacob wrote:
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.

--
David Olsen
qg********@yahoo.com

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.