-
March 16th, 2009, 07:59 PM
#1
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.
-
March 16th, 2009, 08:14 PM
#2
Re: Unresolved Symbol Error
You misspelled the function name in the implementation file.
-
March 16th, 2009, 08:16 PM
#3
Re: Unresolved Symbol Error
I simply added it to the msvc project and compiled the project. I've never had an issue before.
-
March 16th, 2009, 08:18 PM
#4
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.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 16th, 2009 at 08:21 PM.
-
March 16th, 2009, 08:29 PM
#5
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.
-
March 21st, 2009, 08:50 AM
#6
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.
-
March 21st, 2009, 12:07 PM
#7
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"?
-
March 21st, 2009, 12:55 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|