Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linking problem in VC++

Posted on 1998-09-01
2
Medium Priority
?
202 Views
Last Modified: 2010-04-02
hi,
   I have the following class:

   #include <iostream.h>
   #include <string.h>
   #include <stdio.h>
     
   #ifndef STR
   #define STR
     
   class TPString
   {
   private:
   char *buf; // to contain the data
   int len; // length of this buffer
     
   public:
        // Constructors
   TPString();
   TPString(  char *aBuf );
   TPString(  TPString& rhs );
     
   // Destructors
   ~TPString();
     
           // Concatenations
     TPString  operator+(  TPString& rhs );
   friend TPString  operator+ (  char* lhs,  TPString& rhs );
            // Assignments
   TPString& operator=(  TPString& rhs );
   TPString& operator=(  char* rhs );
   };
     
   When I compile and build my main function, I have the following link errors:

   main.obj : error LNK2001: unresolved external symbol "public: __thiscall TPString::~TPString(void)"
   (??1TPString@@QAE@XZ)
   main.obj : error LNK2001: unresolved external symbol "public: __thiscall TPString::TPString(class TPString &)"
   (??0TPString@@QAE@AAV0@@Z)
   main.obj : error LNK2001: unresolved external symbol "public: class TPString & __thiscall
   TPString::operator=(char *)" (??4TPString@@QAEAAV0@PAD@Z)
   main.obj : error LNK2001: unresolved external symbol "public: __thiscall TPString::TPString(void)"
   (??0TPString@@QAE@XZ)
   main.obj : error LNK2001: unresolved external symbol "class ostream & __cdecl operator<<(class ostream
   &,class TPString &)" (??6@YAAAVostream@@AAV0@AAVTPString@@@Z)
   main.obj : error LNK2001: unresolved external symbol "public: class TPString & __thiscall
   TPString::operator=(class TPString &)" (??4TPString@@QAEAAV0@AAV0@@Z)

   Can any expert please advise what happens and how can i solve/prevent the same errors.

   Thanks very much.
   Andrea
0
Comment
Question by:tanmh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 320 total points
ID: 1171806
WHY
The class definition above defines the structure of the class, however you have not defined the actual code that implements the class anywhere.  This is why.  When you call one of the functions in the class, there is no code anywhere to actually implement the functon.

FUNNY NAMES
In case you are wondering the reason the names are listed strangly is because the C++ compiler "mangles" this names with extra characters indicate the type of parameters that the function takes.  For example you have multiple constructors (each of the 3 functions called TPString after public) however each takes different parameters, so they are really different functions.  The linker needs a unique name for each unique function, so the C++ compiler adds the extra characters to the names to make these names unique.

HOW TO FIX
1. You need to define the implementation code for each function that you have in the class,  

i) you can either do this in the body of the class
example (please note the absence of a semi-colon (;) after the function definition)

class TPString
{
   ...etc...
     
   public:
        // Constructors
   TPString()
   {
     // <--- add your code here
   }

   ...etc...

OR

ii) add the code in a separate function body outside the { } enclosing the class

Example

   class TPString
   {
     ...etc...
   } ;

TPString::TPString(  char *aBuf )
{
  // whatever code you want goes here
}
     
ONE FINAL THING
The line beginning "friend" in the class definition defines a global function (called operator+, which can be called by writing a + with the parameters stated) defines a global function which has access to the private data in a TPString.  This global function should also be defined somewhere, something like:

TPString  operator+ (  char* lhs,  TPString& rhs )
{
  TPString str(lhs) ;
  TPString str2 = str + rhs ;
  return str2 ;
}

0
 

Author Comment

by:tanmh
ID: 1171807
Thanks very much for the clear and concise answer.
I've done the recommended ammendments and it works!!!!
Thanks a million.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question