CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    135

    Unresolved Symbol Error

    I've had this error before, but what I changed then does not seem to be the cause for this instance of the error in a different program:

    1>------ Build started: Project: LearningCalculator, Configuration: Debug Win32 ------
    1>Compiling...
    1>calculator.cpp
    1>Linking...
    1>main.obj : error LNK2019: unresolved external symbol "int __cdecl CalculateTrials(class std::vector<double,class std::allocator<double> > &,double,double,double &)" (?CalculateTrials@@YAHAAV?$vector@NV?$allocator@N@std@@@std@@NNAAN@Z) referenced in function _main
    1>LearningCalculator.exe : fatal error LNK1120: 1 unresolved externals
    1>LearningCalculator - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Here's the code:

    main.cpp
    Code:
    #include <iostream>
    #include <vector>
    #include "learningIO.h"
    #include "calculator.h"
    
    int main()
    {
    	double CS_salience = 0.1, US_salience = 1, CurrentLearning = 0;
    	std::vector<double> Learning;
    	int num_trials;
    
    	GetLearningVars(Learning, CS_salience, US_salience, CurrentLearning);
    	num_trials = CalculateTrials(Learning, CS_salience, US_salience, CurrentLearning);
    
    	system("PAUSE");
    
    	return 0;
    }
    learningIO.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    #include "learningIO.h"
    
    using namespace std;
    
    bool ParseInt( const string& input, double& retval )
    {
        istringstream parsed_string( input );
        if( parsed_string >> retval )
            return true;
        else
            return false;
    }
    
    // GetDoubleInput() by Russco, modified by BleaS
    double GetDoubleInput( const string& message, string error = "\nYour input is invalid, please try again.\n\n", double min_accepted = INT_MIN, double max_accepted = INT_MAX)
    {
        std::string input;
        double retval;
        int not_first = 0;
       do
       {
          if (not_first == 1)
          {
              cout << error;
          }
          std::cout << message;
          std::getline( cin, input );
          not_first = 1;
       } while ( !ParseInt( input, retval ) || retval < min_accepted || retval > max_accepted );
       return retval;
    }
    
    void GetLearningVars(vector<double>& Learning, double& CS_salience, 
    					 double& US_salience, double& CurrentLearning)
    {
    	//cout << "Please Input the salience of the CS (generally less than 1"
    	//	 << endl << "CS > ";
    	CS_salience = GetDoubleInput("Please Input the salience of the CS (generally less than 1)\nCS > ");
    
    	//cout << "Please Input the salience of the US (generally 1)"
    	//	 << endl << "US > ";
    	US_salience = GetDoubleInput("Please Input the salience of the US (generally 1)\nUS > ");
    
    	//cout << "Please enter the current level of learning between CS-US (generally 0)"
    	//	 << endl << "Learning > ";
    	CurrentLearning = GetDoubleInput("Please Input the current level of learning (generally 0 to start with)\nL > ");
    
    	// Add current learning to the learning index
    	Learning.push_back(CurrentLearning);
    
    }
    calculator.cpp
    Code:
    #include <iostream>
    #include <vector>
    #include "calculator.h"
    
    using namespace std;
    
    int CaluclateTrials(vector<double>& Learning, double CS_salience, double US_salience, double& CurrentLearning)
    {
    	int num_trials = 1;
    
    	while (Learning[num_trials - 1] != US_salience)
    	{
    		cout << "Trial #" << num_trials << ". "
    			 << CS_salience << "(" << US_salience << " - "
    			 << CurrentLearning << ")= ";
    		double SurpriseLevel = US_salience - CurrentLearning;
    		double LearningChange = SurpriseLevel * CS_salience;
    
    		cout << LearningChange << " | Current Learning is now: "
    			 << " (" << LearningChange << ") + (" << CurrentLearning
    			 << ") = ";
    		 
    		CurrentLearning+= LearningChange;
    
    		cout << CurrentLearning;
    
    		Learning.push_back(CurrentLearning);
    		CurrentLearning;
    		++num_trials;
    		cout << endl;
    	}
    
    	cout << endl << "It took " << num_trials << " to fully learn.";
    	return num_trials;
    }
    calculator.h
    Code:
    #ifndef GUARD_calculator_h
    #define GUARD_calculator_h
    
    #include <vector>
    
    int CalculateTrials(std::vector<double>&, double, double, double&);
    
    #endif // GUARD_calculator_h
    [learningIO.h
    Code:
    #ifndef GUARD_learningIO_h
    #define GUARD_learningIO_h
    
    #include <vector>
    
    bool ParseInt(const std::string&, double&);
    double GetDoubleInput( const std::string&, std::string, double, double);
    void GetLearningVars(std::vector<double>&, double&, double&, double&);
    
    #endif // GUARD_learning_h
    I know there's a library out there to translate errors to something more understandable. But I'd like to learn how to identify this error and know what to look for when I get this error without anything translating it. I don't just want someone to point out what code is wrong, but why it produces this sort of error, so that I can point it out next time, without having to ask.

    Thank you.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Unresolved Symbol Error

    You misspelled the function name in the implementation file.

  3. #3
    Join Date
    Feb 2009
    Posts
    135

    Re: Unresolved Symbol Error

    I simply added it to the msvc project and compiled the project. I've never had an issue before.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Unresolved Symbol Error

    That error is a linking error, not a compiler error. The linker is saying that it cannot find the function CalculateTrials. Yes, you mispelled it, that's why the linker can't find it.
    Code:
    CaluclateTrials
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 16th, 2009 at 08:21 PM.

  5. #5
    Join Date
    Feb 2009
    Posts
    135

    Re: Unresolved Symbol Error

    Ah ha. I seem to remember such link errors occurring when it cannot find something. It's happened when I forgot to put a reference symbol in before. I suppose spelling should be checked too, though, to be honest, I -did- look it over and simply saw it spelled correctly (hooray for perception). Thank your catching that embarrassing blunder.

  6. #6
    Join Date
    Feb 2009
    Posts
    135

    Re: Unresolved Symbol Error

    So, I'm getting this error again and I'm not sure why. I've checked for spelling and reference symbols. I'd love to figure it out for myself, hence why I haven't posted it yet, but I'm completely stuck. I can't find a good reference source for taking apart this error and fully taking a look at it in that method.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Unresolved Symbol Error

    Common things to check when dealing with linker errors.....

    1) Is the file containing the definition being compiled once and only once, eg, not #included in multiple source files?
    2) Is the signature consistent between the header declaration and definition? A misspelling, a difference in the argument list, even a single missing const qualifier can cause this. I suggest copying the definition signature and pasting it over the header declaration to be sure.
    3) Are you trying to link a function in a .c file to a C++ program without using extern "C"?

  8. #8
    Join Date
    Feb 2009
    Posts
    135

    Re: Unresolved Symbol Error

    Thanks, I eventually realized it was a missing argument. It was difficult to catch because it was the long hand of a sizetype (std::string::size_type).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured