Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Constructors -- LNK2019 error in Visual C++

mcarthum@gmail.com
Guest
 
Posts: n/a
#1: Aug 30 '08
I have a class declared in a header file as follows:

class Vector3{
public:
int x,y,z;
Vector3(int inx, int iny, int inz);
};

And I define the class in a source file:

class Vector3{
public:
int x,y,z;
Vector3(int inx, int iny, int inz){
x = inx;
y = iny;
z = inz;
}
};

When I #include the header in another file and try to construct a
Vector 3 object using "Vector3 vect(x,y,z);" I get an "unresolved
external symbol" linker error. What might be causing this?

Paavo Helde
Guest
 
Posts: n/a
#2: Aug 30 '08

re: Using Constructors -- LNK2019 error in Visual C++


mcarthum@gmail.com kirjutas:
Quote:
I have a class declared in a header file as follows:
>
class Vector3{
public:
int x,y,z;
Vector3(int inx, int iny, int inz);
};
>
And I define the class in a source file:
>
class Vector3{
public:
int x,y,z;
Vector3(int inx, int iny, int inz){
x = inx;
y = iny;
z = inz;
}
};
>
When I #include the header in another file and try to construct a
Vector 3 object using "Vector3 vect(x,y,z);" I get an "unresolved
external symbol" linker error. What might be causing this?
>
The linker does not find implementation of a function or a definition for
a variable (you do not sat which). Probably you did not link all you
source files together. If the linker does not see any file containing the
Vector3 constructor implementation it is quite natural that it complains.

How to call linker etc. is a bit off-topic here. Follow the guidelines
for your particular implementation of C++.

hth
Paavo
Pete Becker
Guest
 
Posts: n/a
#3: Aug 31 '08

re: Using Constructors -- LNK2019 error in Visual C++


On 2008-08-30 16:49:56 -0400, mcarthum@gmail.com said:
Quote:
I have a class declared in a header file as follows:
>
class Vector3{
public:
int x,y,z;
Vector3(int inx, int iny, int inz);
};
>
And I define the class in a source file:
>
class Vector3{
public:
int x,y,z;
Vector3(int inx, int iny, int inz){
x = inx;
y = iny;
z = inz;
}
};
>
This definition says that the constructor is inline, so it's not
visible outside the source file where this definition occurs.
Ordinarily, an inline constructor is defined in a header file, and an
out of line constructor is defined out of line in a source file, like
this:

Vector3::Vector3(int inx, int iny, int inz){
x = inx;
y = iny;
z = inz;
}

Or, slightly better in this case but in general much better:

Vector3::Vector3(int inx, int iny, int inz)
: x(inx), y(iny), z(inz)
{
}


--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Closed Thread