Persistent error "unresolved externals"

I am an experienced Java programmer and now I've been asked to do some C++ which has not been so bad in Linux. However, I have to move now to Visual Studio 2010. I am just trying to create a simple console program, and add a couple of classes. I do that with all the default settings, just pick a Win32 Console application at first, I have tried with both the pre-compiled header and an empty project. I add a simple class that does this:

Header:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
class TestClass
{
public:
	TestClass(void);
	~TestClass(void);	

	static double mapZeroToMax(double toMap, double max);

	static double linearScale(double x, double a, double b);

};


Source file (cpp):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "TestClass.h"

TestClass::TestClass(void)
{
}

TestClass::~TestClass(void)
{
}

static double mapZeroToMax(double toMap, double max){        
  return (0.000001 * (toMap) * (toMap) * (max));
}
    
static double linearScale(double x, double a, double b){                
  return ((x)*((b)-(a))/1000.0+(a));
}


Than in my main class I do this:

1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include "TestClass.h"


int _tmain(int argc, _TCHAR* argv[])
{
	TestClass::mapZeroToMax(358,0.01);
	return 0;
}


And it never compiles and gives me the following errors:

Error 1 error LNK2019: unresolved external symbol "public: static double __cdecl TestClass::mapZeroToMax(double,double)" (?mapZeroToMax@TestClass@@SANNN@Z) referenced in function _main...

Error 2 error LNK1120: 1 unresolved externals

I did several google searches and found things like "change the linker to Windows app instead of a Console App"... and so on. None of that has worked. Any ideas how to pass this error?
Thanks!
Why are there braces around the variables 'ToMap' and 'max' in your member functions?

That isn't how to call member functions in C++. This is a common mistake made by Java programmers moving to C++.

You'd be better off just making functions.
LNK2019 errors were the bane of my existence during the initial part of my internship, so I can sympathize with you.

I think the linker error is being caused by your methods mapZeroToMax(...) and linearScale(...) methods not being within TestClass's scope.

You should change the definitions in your cpp file to prefixed with TestClass:: as you have done for your default constructor and destructor. For example:

1
2
3
4
5
6
7
static double TestClass::mapZeroToMax(double toMap, double max){        
  return (0.000001 * (toMap) * (toMap) * (max));
}
    
static double TestClass::linearScale(double x, double a, double b){                
  return ((x)*((b)-(a))/1000.0+(a));
}


Computergeek01 wrote:
That isn't how to call member functions in C++.

I agree with Computergeek01 comment, if your going to call the method without creating an instance of the class, you are better off with functions.

I hope this helps!
Topic archived. No new replies allowed.