I wonder if anyone can point out the source of my error.

I am using the SDK for commercial software which _requires_ that I build etc in Visual Studio 2008. I confess that my C++ is rusty at best and that I don't understand a lot about using Visual Studio. However, I have previously built code without this problem, so I'm a bit stumped about why it is happening now.

When I first built my code, I got an error C3861 (identifier not found). Therefore I #included the appropriate file. However, that file has been #included in another part of my code and so resulted in a error LNK2005 (**** already defined in main.obj). The #included file, as far as i can tell, has the appropriate #ifndef and #define statements.

In my project, the only directly included files are main.cpp and testFunction.cpp (I don't really understand what difference it makes to Visual Studio if I explicitly state the source files or #include files).

I've massively pared down the code to the following. Can anyone sport how I get my code compiling without an error C3861 or a error LNK2005, please?

GeneralFunctions.h:

#ifndef GENERALFUNCTIONS_H
#define GENERALFUNCTIONS_H

bool IsFiniteNumber(double x);

bool IsFiniteNumber(double x) {
     // thanks to http://www.johndcook.com/IEEE_exceptions_in_cpp.html
        return (x <= DBL_MAX && x >= -DBL_MAX); 
} 
    
#endif /* GENERALFUNCTIONS_H */

main.h:

#ifndef MAIN_H
#define MAIN_H

// #includes from SDK
#include <system_includes.h>

// #includes from IMJS
#include "./testFunction.h"
#include "./GeneralFunctions.h"

#endif /* MAIN_H */

main.cpp:

#include "./main.h"
void main()
{
	char				errorStr[1024];

	strcpy(errorStr,testFunction());
	exit(0);
}

testFunction.h

#ifndef TESTFUNCTION_H
#define TESTFUNCTION_H

#include <system_includes.h>

// if the following line is not included: error C3861: 'IsFiniteNumber': identifier not found
// if it is included: error LNK2005: "bool __cdecl IsFiniteNumber(double)" (?IsFiniteNumber@@YA_NN@Z) already defined in main.obj
#include "./GeneralFunctions.h"

char * testFunction();

#endif

testFunction.cpp:

#include "./testFunction.h"
char * testFunction()
{    double            testDouble=1.0;
    char            errorStr[256];

    if (IsFiniteNumber(testDouble)){
        strcpy(errorStr,"Tis finite!\n");
    } else {
        strcpy(errorStr,"Uh oh!\n");
    }
    return errorStr;
}

I think even though you have include guards, you can't declare a function in the header, you can only define it. That is, you need to change the header to:

#ifndef GENERALFUNCTIONS_H
#define GENERALFUNCTIONS_H

bool IsFiniteNumber(double x);
    
#endif /* GENERALFUNCTIONS_H */

And then in the implementation (.cpp file), put:

#include "GeneralFunctions.h"

bool IsFiniteNumber(double x) {
     // thanks to http://www.johndcook.com/IEEE_exceptions_in_cpp.html
        return (x <= DBL_MAX && x >= -DBL_MAX); 
}

David

Thank you. This now builds its also made me re-asses how I build my projects. I think I misunderstood something I was taught years ago...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.