Solved

LNK2019 error working with header file creation.

Posted on 2006-06-28
10
220 Views
Last Modified: 2010-04-24
Hello,

 Please forgive my ignorance on this one, but I have been fighting with this for too long now.
 I have a program that I am writing that requires several classes to be designed and utilized. In the course of designing the
header files for the classes, I must break the class declarations (within the header files) apart from the class coding.
ie....

/* this is all in HEADERNAME.h */
#ifndef _HEADERNAME_H_
#define  _HEADERNAME_H_

someclass
{
 ......
 ......
}

#endif

/* this is all in HEADERNAME.cpp */
#include ".\headername.h"

someclass::someclass( void )
{........}

someclass::~someclass( void )
{........}

etc......


 My problem is that my compiler (Visual Studio.net 2003) runs fine if the entire class content is in the .h file, but gives me an error LNK2019 when I attempt to break the file into the .h file and the .cpp file.

 Any help would be awesome. Thank you in Advance!
 William Sean Grant
0
Comment
Question by:grantws
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 17001476
Please post the full error message.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17001686
Add cpp file to the project.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17002460
The information is not sufficient.
0
 

Author Comment

by:grantws
ID: 17003645
I have placed: #include "OrderedList.h"
in the main program .cpp file. Below is just one of the many LNK2019 errors I get, though they are all the same error and just related to the other functions of the class Node and class OrderedList. As far as I can tell, my .h and .cpp files are included in my project (checking the solution explorer on the right side of the screen). As I mentioned above, if I force all of the function definitions in the .h file and compile, everything works as advertised.

Polynomials error LNK2019: unresolved external symbol "public: __thiscall OrderedList<char>::OrderedList<char>(void)" (??0?$OrderedList@D@@QAE@XZ) referenced in function _main


This is my Header file:

#pragma once
#include "profile.h"

/*
Predefined OrderedList class to use with Node class. The OrderedList class will
be fully defined after the Node class.
*/
template <class T>
class OrderedList;


/*
Node Class is the primary data storage structure to be used with the OrderedList class.
Includes a friend declaration for the OrderedList class which allows the OrderedList class
to utilize the Node structure as if it were Node were a OrderedList class member.
*/
template <class T>
class Node
{
 friend class OrderedList<T>;
 private:
    T* data;                                                            // Template type data
      Node<T> *next;                                                      // Address of next Node in list
      Node<T> *prev;                                                      // Address of previous Node in list

      Node( void );                                                      // Default Node constructor declaration
      virtual ~Node( void );                                          // Default Node destructor declaration
      Node<T>* operator=( const Node<T>& nodeIn );      // Node operator overloaded "equals" type Node
      Node<T>* operator=( const T& nodeIn );                  // Node operator overloaded "equals" type data
      bool operator==( const Node<T>& nodeIn );            // Node operator overloaded "is equal to" type Node
      bool operator==( const T& nodeIn );                        // Node operator overloaded "is equal to" type data
      bool operator!=( const Node<T>& nodeIn );            // Node operator overloaded "is not equal to" type Node
      bool operator!=( const T& nodeIn );                        // Node operator overloaded "is not equal to" type data
      bool operator<( const Node<T>& nodeIn );            // Node operator overloaded "is less than" type Node
      bool operator<( const T& nodeIn );                        // Node operator overloaded "is less than" type data
      bool operator>( const Node<T>& nodeIn );            // Node operator overloaded "is greater than" type Node
      bool operator>( const T& nodeIn );                        // Node operator overloaded "is greater than" type data
      bool operator<=( const Node<T>& nodeIn );            // Node operator overloaded "is lt or equal to" type Node
      bool operator<=( const T& nodeIn );                        // Node operator overloaded "is lt or equal to" type data
      bool operator>=( const Node<T>& nodeIn );            // Node operator overloaded "is gt or equal to" type Node
      bool operator>=( const T& nodeIn );                        // Node operator overloaded "is gt or equal to" type data
};

/* OrderedList Class declaration. */
template <class T>
class OrderedList
{
 private:
    long unsigned int size;                                           // Size of the list ( the number of Nodes in list )
      long unsigned int cpos;                                           // Current position of the list
      Node<T> *theList;                                                 // Initial data container for the list
      Node<T> *currentNode;                                           // Current data container pointed to within the list

      bool advanceNode( void );                                     // Function to advance the current Node in the list by one
      bool reverseNode( void );                                     // Function to rewind the current Node in the list by one
      bool beginNode( const T& dataIn );                         // Function to initialize the list
      bool insertFirst( const T& dataIn );                   // Function to insert data into the first position in the list
      bool insertMid( const T& dataIn );                         // Function to insert data into the middle positions in the list
      bool insertLast( const T& dataIn );                         // Function to insert data into the last position in the list
      bool removeFirst( void );                                     // Function to remove data from the first position in the list
      bool removeMid( void );                                           // Function to remove data from the middle positions in the list
      bool removeLast( void );                                     // Function to remove data from the last position in the list

public:
      OrderedList( void );                                           // Default constructor for the OrderedList class
      virtual ~OrderedList( void );                               // Default destructor for the OrderedList class

      void printOut( void );                                           // Function to printout list contents for testing purposes only
      bool isEmpty( void );                                           // Function to indicate if the list is empty
      long unsigned int sizeOf( void );                         // Function to return size of the list
      bool clearList( void );                                           // Function to clear the list
      bool setFirst( void );                                           // Function to move to the front of the list
      bool setPosition( const unsigned long int& pos );// Function to set the position that the list is currently at
      bool setDataPosition( const T& dataIn );             // Function to find the poisition that contains the input data
      long unsigned int returnCurrentPosition( void ); // Function to return the current position within the list
      bool removeData( const long unsigned int& pos ); // Function to remove data from the list
      T retrieveData( const long unsigned int& pos );  // Function to retreive data from the list without removing it
      bool insertData( const T& dataIn );                         // Function to insert data into the list
};


This is a portion of my .cpp file:
#include ".\orderedlist.h"


/*
Class Node constructor to create Node class with no input arguments.
Creates template data variable, NULL pointer to next Node in list, and
NULL pointer to previous Node in list.
*/
template <class T>
Node<T>::Node( void )
{
    data = new T;
      next = NULL;
      prev = NULL;
}

/*
Class Node default constructor to delete Node class with no input arguments.
Deletes template data variable. Makes next pointer and previous pointer NULL
address locations.
*/
template <class T>
Node<T>::~Node( void )
{
      delete data;
      if( next ) delete next;
      if( prev ) delete prev;

      next = NULL;
      prev = NULL;
}

more code follows..............................

/*
Class OrderedList default constructor. Sets size of list to 0 and the current position to the first Node
in the list, which is also 0. The initial data storage position within the list is initialized as "theList."
"currentNode" is set to the address of theList as the only Node in the list.
*/
template <class T>
OrderedList<T>::OrderedList( void )
{
      size = 0;
      cpos = 0;
      theList = new Node<T>;
      currentNode = theList;
}

/*
Class OrderedList default destructor. This function sets the values of the class to the original default values,
while deleting the Nodes for data holding.
*/
template <class T>
OrderedList<T>::~OrderedList( void )
{
      size = 0;
      cpos = 0;
      delete theList;
      currentNode = NULL;
}



more code follows..............................



Again, thank you in advance.
William Sean Grant
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Accepted Solution

by:
e_tadeu earned 95 total points
ID: 17004063
You can't put templated CODE in .cpp. All your templated code must be in the header, because they must be included by whoever use them!
0
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 30 total points
ID: 17006689
Some more addition to e_tadeu's suggestion.

Templated class should have their definition in the header file. Why ?
Because they are included by the user of the class in the compile time.
Template stuff is not resolved by the linker.
0
 

Author Comment

by:grantws
ID: 17006945
Thank you e_tadeu and rajeev_devin, both, for your help. I truly appreciate the assist.
William Sean Grant
0
 
LVL 4

Expert Comment

by:e_tadeu
ID: 17008782
Grant,

  I think I deserved the Accepted Answer, rajeev's answer was just adding a little to my answer (he should have got a Good Assist)
0
 

Author Comment

by:grantws
ID: 17009569
I appologize, I am new to this site and am mucking through.
Thank you both, again.

William Grant
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now