I'm getting an unresolved symbols linking error with this code, the cpp files compile successfully separately, I just can't build the solution and run it. any ideas?

header.h

#include <iostream>
#include <string>
using std::string;
using namespace std;

class Double
{
private:
	double dub; 
	char str[50];
public:
	Double();
	Double(char s[50]);
	Double(double a);
	void toDouble(char str[50]);
	void toString();
	void printClass();
	void equal(char str[50]);
	void equal(double dub);
	bool isNAN(char s[50]);




};

double.cpp

#include <math.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <string.h>
#include <conio.h>
#include "header.h"
using namespace std;

Double::Double()
{
}

Double::Double(char s[50])
{
	strcpy(str, s);
}

Double::Double(double a)
{
	dub=a;
}

void Double::toDouble(char str[50])
{
	if(!isNAN(str))
		dub = atof(str);
}

void Double::toString()
{
	sprintf(str,"%f",dub);
}

void Double::equal(char s[])
{
	strcpy(str, s);
}

void Double::equal(double d)
{
	dub=d;
}

bool isNAN(char s[])
{
	string str=s;
	cout<<s<<endl;
	cout<<str<<endl;
	for(int i=0; i<str.size(); i++)
	{
		if(!isdigit(s[i]))
			return true;
	}
	return false;
}



void Double::printClass()
{
	cout<<dub<<endl;
	cout<<str<<endl;
}

main.cpp

#include <iostream>
#include <string>
#include <math.h>
#include <cstdlib>
#include <string.h>
#include <conio.h>
#include "header.h"
using namespace std;
int printMenu();
void waitKey();

const int ADDSTRING=1;
const int ADDDOUBLE=2;
const int EXIT=3;

int main()
{
	char s[50]="123.223h";
	Double d, str(*s), dubble(123.233);
	d.toDouble("123.222h");
}

Dani AI

Generated

A quick expert note: the linker complaint came from a missing class-qualified definition for the method declared in the header. As discovered, the symbol the linker was looking for was the member function Double::isNAN — the implementation in double.cpp had been written as a free function instead of a class member. ’s suggestion to add header guards is still good practice (prevents other include-related headaches) but wasn’t the immediate cause of this particular LNK2019.

A short checklist for hunting similar unresolved externals: make sure the implementation signature matches the declaration exactly (parameter types, reference/const qualifiers and any trailing const on methods); ensure the definition is qualified with the class name when it’s a member; confirm every .cpp that contains needed definitions is actually part of the project/build; check for mismatched linkage (extern "C") or different runtimes; and do a clean rebuild if object files are stale.

For more robust numeric parsing (the original isNAN using isdigit rejects dots, signs, and exponents), prefer a parsing routine that validates the whole string. Example using strtod to both parse and detect non-numeric trailing characters:

#include <cstdlib>
#include <cerrno>

bool is_numeric(const char* s, double &out) {
    char *end = nullptr;
    errno = 0;
    out = strtod(s, &end);
    if (end == s || *end != '\0' || errno == ERANGE) return false;
    return true;
}

Final practical tips: avoid using namespace std; in headers; prefer std::string over fixed char buffers; initialize members in initializer lists; use snprintf/streams instead of sprintf; enable high warning levels and treat warnings as errors during debugging; if a symbol still won’t link, inspect object files (dumpbin/nm) to compare mangled names.

Recommended Answers

All 3 Replies

Try adding header guards in header.h

#ifndef HEADER_H
#define HEADER_H

#include <iostream> // put your includes in the header file so you don't need to include them in every file that uses this header
#include <...>

class blah_blah
{
};
...

#endif

That didn't fix it, heres the error im getting by the way, I guess I shoudl have included it the first time around

1>double.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Double::isNAN(char * const)" (?isNAN@Double@@QAE_NQAD@Z) referenced in function "public: void __thiscall Double::toDouble(char * const)" (?toDouble@Double@@QAEXQAD@Z)
1>E:\C++ Level 2\Midterm\Debug\Midterm.exe : fatal error LNK1120: 1 unresolved externals

ohh duh, nevermind, when I defined the isNAN() function i forgot to name it as part of a class

bool Double::isNAN(char str[])
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.