C++/f.obj : error LNK2019
Expert: vijayan - 5/26/2009
QuestionVijayan, I'm working on a project for a C++ class and it will compile, but I'm getting linking errors. I'm pasting the .cpp and .h files below. Hopefully, you can enter them into your programming environment and see the errors for yourself. I think it's easier that way. Please don't concern yourself with the fact that the program layout makes no sense and doesn't really accomplish anything. I should let you know that the instructor of this C++ class is dangerously insane and enjoys assigning work that is completely pointless.
If you would, please just let me know what I can do to get it to run. Thanks, Jeffrey Kurtz
***********************************************************
#include <iostream>
#include <string>
using namespace std;
#include "f.h"
string s;
string s5="data";
double sd=500;
Integer d("string 1");
Integer dl(500.00);
Integer::Integer(string s1)
{
strdata=s1;
}
Integer::Integer(double d2)
{
dbldata=d2;
}
int main()
{
d.equals("string 2");
dl.equals(600.42);
dl=s5;//1st = overload test
dl=sd;//2nd = overload test
dl+=sd;// += overload test
cout << "enter a string for conversion to a double : ";
getline(cin,s);
if(d.isNAN(s)==0)
cout << "no conversion or value of double is 0 " << endl;
cout << "enter a double for conversion to a string : ";
sd=0;
cin >> sd;
if(sd!=0)
d.isNAN(sd);
else
cout << "no conversion" << endl;
}
void Integer::equals(string s2)
{
string t=s2;
}
void Integer::equals(double d2)
{
double t=d2;
}
string Integer::toString(double d) const// double to string
{
char st[50];
sprintf(st,"%.2f",d);
return st;
}
double Integer::toDouble(string st)// string to double
{
double dd;
dd = atof(st.c_str());
return dd;
}
bool Integer::isNAN(string st)
{
d.strdata=st;
bool r=1;
d.dbldata=d.toDouble(d.strdata);
if(d.dbldata!=0)
{
cout << d.dbldata << " is the double" << endl << endl;
return r;
}
else
{
r=0;
return r;
}
}
bool Integer::isNAN(double st)
{
d.dbldata=st;
bool r=1;
d.strdata=d.toString(d.dbldata);
cout << "\"" << d.strdata << "\" is the string" << endl << endl;
return r ;
}
string &Integer::operator=(string &r)
{
dl.strdata=r;
cout << "string s5: \"" << r << "\" assigned to string dl.strdata: \"" << dl.strdata << "\"" << endl <<endl;
return r;
}
double &Integer::operator=(double &r)
{
dl.dbldata=r;
cout << "double sd(" << r << ") assigned to double dl.dbldata(" << dl.dbldata << ")" << endl << endl;
return r;
}
double &Integer::operator+=(double &r)
{
dl.dbldata=r+r;
cout << "double dl.dbldata is now " << dl.dbldata << endl << endl;
return r;
}
***********************************************************
class Number: string{
public:
Number(string);
Number();
virtual string toString(double) const=0;
virtual void isNAN() const;
private:
};
class Integer: Number
{
public:
Integer(string);
Integer(double);
void equals(string);
void equals(double);
virtual string toString(double) const;
double toDouble(string);
bool isNAN(string);
bool isNAN(double);
string &operator=(string &);
double &operator=(double &);
double &operator+=(double &);
private:
double dbldata;
string strdata;
};
AnswerA linker error LNK2019 means that there is at least one unresolved external symbol; you have declared and are trying to use a variable or function, but it is not defined. Provide a definition for the symbol and the error would go away. see:
http://msdn.microsoft.com/en-us/library/799kze2z(VS.80).aspx
In your code, for class Number, Number::Number() and Number::isNAN() is not defined. Modify the class Number to
class Number: string{
public:
Number(string);
Number() {} // **** modified ****
virtual string toString(double) const=0;
virtual void isNAN() const {} // **** modified ****
private:
};
This will get it to compile, link and run.