2 Replies - 690 Views - Last Post: 05 February 2013 - 01:45 PM Rate Topic: -----

#1 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 235
  • View blog
  • Posts: 1,069
  • Joined: 25-June 12

Unresolved external LNK errors

Posted 05 February 2013 - 12:27 PM

Hello Everyone. I am trying to work through an example cpp program that uses exception handling to detect non-number user entries, but am stuck on a few LNK errors.

The errors are as followed (tied together I believe):

  • error LNK2001: unresolved external symbol "public: virtual int__thiscall NonNumber::What(void)" ([email protected]@@UAEHXZ)
  • error LNK1120: 1 unresolved externals.


Can anyone tell me what is going on? My file is coded below:

numberverifier.cpp
// numberverifier.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <stdexcept>
using namespace std; 

// class NonNumber definition
class NonNumber : public runtime_error
{
public:

   // constructor
   NonNumber()
      : runtime_error( "non-integer detected" ) 
   {
      // empty
   } // end class NonNumber definition

   virtual int What();

private: 
   string message;

}; // end class NonNumber

// function castInput definition
int castInput( string input )
{
   int result = 0;
   int negative = 1;

   // check for minus sign
   if ( input[ 0 ] == '-' )
     negative = -1;

   for ( int i = input.length() - 1, j = 0; i >= 0; i--, j++ )
   {
      if ( negative == -1 && i == 0 )
         continue;

      if ( input[ i ] >= '0' && input[ i ] <= '9' )
         result += static_cast< int >( input[ i ] - '0' ) * pow( 10.0, j );   
      else 
		 throw NonNumber();
   } // end for

   return result * negative;

} // end function castInput

int main()
{
   string input;
   int convert; 

   cout << "Please enter a number (end-of-file to terminate): ";

   while ( cin >> input )
   {
	   try
	   {
		   convert = castInput(input);
		   cout << "The number entered was: " << convert << endl;
	   } // end try
		catch (NonNumber &e)
	   {
		   cout << "INVALID INPUT: " << e.What() << endl;
	   } // end catch

      cout << "\n\nPlease enter a number (end-of-file to terminate): ";
   } // end while

   cout << endl;
} // end main



Any help as always is much appreciated! ^^ I'm still stumbling through the basics of C++ :/ I professionally develop in .NET so C++ is not being all that cooperative with me.

Is This A Good Question/Topic? 0
  • +

Replies To: Unresolved external LNK errors

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: Unresolved external LNK errors

Posted 05 February 2013 - 12:37 PM

You have defined a member function with the name of What(), where did you implement this function?

Jim
Was This Post Helpful? 1
  • +
  • -

#3 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 235
  • View blog
  • Posts: 1,069
  • Joined: 25-June 12

Re: Unresolved external LNK errors

Posted 05 February 2013 - 01:45 PM

Thanks for the reply Jim. Apparently I was over-complicating the design idea. I'll have to check with the individual who gave me the practice program to see if 'What()' is being used correctly, but for all rights and purposes, my output is as it should be.

numberverifier.cpp
// numberverifier.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <stdexcept>
using namespace std; 

// class NonNumber definition
class NonNumber : public runtime_error
{
public:

   // constructor
   NonNumber()
      : runtime_error( "non-integer detected" ) 
   {
      // empty
   } // end class NonNumber definition

   virtual string What()
   { 
	   return "non-integer detected"; 
   };

private: 
   string message;

}; // end class NonNumber

// function castInput definition
int castInput( string input )
{
   int result = 0;
   int negative = 1;

   // check for minus sign
   if ( input[ 0 ] == '-' )
     negative = -1;

   for ( int i = input.length() - 1, j = 0; i >= 0; i--, j++ )
   {
      if ( negative == -1 && i == 0 )
         continue;

      if ( input[ i ] >= '0' && input[ i ] <= '9' )
         result += static_cast< int >( input[ i ] - '0' ) * pow( 10.0, j );   
      else 
		 throw NonNumber();
   } // end for

   return result * negative;

} // end function castInput

int main()
{
   string input;
   int convert; 

   cout << "Please enter a number (end-of-file to terminate): ";

   while ( cin >> input )
   {
	   // Inserted for an actual end-of-file break statement
	   if (input == "eof") {
		   break;
	   }

	   try
	   {
		   convert = castInput(input);
		   cout << "The number entered was: " << convert << endl;
	   } // end try
		catch (NonNumber e)
	   {
		   cout << "INVALID INPUT: " << e.What() << endl;
	   } // end catch

      cout << "\n\nPlease enter a number (end-of-file 'eof' to terminate): ";
   } // end while

   cout << endl;
} // end main


This post has been edited by AnalyticLunatic: 05 February 2013 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1