Categories
C++ Visual Studio

warning C4244: ‘argument’ : conversion from ‘double’ to ‘int’, possible loss of data

Full error message: warning C4244: ‘argument’ : conversion from ‘double’ to ‘int’, possible loss of data

Error occured on Visual Studio 2003 when compiling a C++ project.

The warning occurred because i was calling the abs function and i did not have included yet the math.h file. The reason that this warning appeared was due to the abs function that was already loaded with other libraries (stdlib.h) and it had only one defined as int abs(int) but I needed the double abs(double) overload. This way the compiler needed to let me know about the implicit cast from double to int.

Example:

double y = 1.2454;
double x = abs((double)y);

Fix:

#include <math.h>

The fix in my case is the following code added to the header of the file #include <math.h>

Categories
C++ Visual Studio

error C2724: ‘ClassName::FunctionName’ : ‘static’ should not be used on member functions defined at file scope

Full error message: error C2724: ‘ClassName::FunctionName’ : ‘static’ should not be used on member functions defined at file scope

Error occurred in Visual Studio 2003 while declaring a static function.

Header file:

class MyClass {
public:
static void FunctionName();
};

Source file:

static void MyClass::FunctionName() {
return 0;
}

The error occurs because i used the ‘static’ modifier in the source file as well as the header file.

The fix is: Remove the ‘static’ modifier from the source file

So the code in the source file will become:

void MyClass::FunctionName() {
return 0;
}
Categories
C++ Visual Studio

error LNK2001: unresolved external symbol private: static …

Full error message: error LNK2001: unresolved external symbol “private: static <type> * <Class>::<memberVariable>” (?<memberVariable>@<C@@0PADA)

Error occured on Visual Studio 2003 when compiling a C++ project.

The error occurred because memberVariable was a static variable and it wasn’t initialized.

Example:

class A {
static char * filename;
static void F(void);
} ;

Fix:

char * A::filename = NULL;

The fix in my case is the following code added to the implementation of the <Class>:
<type> <Class>::<memberVariable> = <initValue>;