Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error LNK2001 with inline keyword

70 views
Skip to first unread message

Jing Duan

unread,
May 9, 1999, 3:00:00 AM5/9/99
to
I have a problem using inline functions with multiple files in a project.
The compiler issues the following error.

try.obj : error LNK2001: unresolved external symbol "public: __thiscall
A::A(void)" (??0A@@QAE@XZ)
try.obj : error LNK2001: unresolved external symbol "public: __thiscall
A::~A(void)" (??0A@@QAE@XZ)
try.obj : error LNK2001: unresolved external symbol "public: __thiscall
A::OK(void)" (??0A@@QAE@XZ)

My test project contains 3 files: A.h, A.cpp and try.cpp.

After I delete the inline keyword, everything is fine. Can anyone explain
the reason?

Thanking you in advance!

Jing Duan
jing...@utoronto.ca


// A.h: interface for the A class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_A_H__7750C362_053A_11D3_B2EE_00A0C9CD953B__INCLUDED_)
#define AFX_A_H__7750C362_053A_11D3_B2EE_00A0C9CD953B__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class A
{
public:
A();
virtual ~A();
void OK();
};
#endif // !defined(AFX_A_H__7750C362_053A_11D3_B2EE_00A0C9CD953B__INCLUDED_)

// A.cpp: implementation of the A class.
//
//////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include "A.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
inline
A::A()
{
cout << "class A" <<endl;
}
inline
A::~A()
{
}

inline
void A::OK()
{
cout << "OK" <<endl;
}

//////////////////////////// try.cpp
#include "A.h"
void main()
{
A a;
a.OK();
}


Roger H. Joerg

unread,
May 9, 1999, 3:00:00 AM5/9/99
to
What does "inline" mean?

See the following example:

x.h:

inline int foo(int a, int b) { return a + b; }

y.cpp

int main()
{
int a = 0;

a = foo(3, 4)
}

Because of the "inline" keyword in the header file, the compiler
may replace the code of main to look like this:

int main()
{
int a = 0;

a = 3 + 4;
}

To be more precise:
The "inline" keyword may work like a macro, depending on the
complexity of the "inlined" source code fragment. But the compiler
may decide that the code is too complex and add its code to
y.obj and call it like a function (this is always true for _DEBUG
configurations).
If there is a other source code file (e.g. z.cpp) which also
uses the function foo, there may be a second "instantiation" of
foo's code, which may blow up the size of the final application.
In your code the "inline" is not seen by the "user" of the inlined
function (try.cpp) and therefore there is no instantiation of the
code (a.obj will not contain the code of A::A() because it is
never used there).

As a rule of thumb:
- Declare inline methods in your header files.
- Do not declare complex methods "inline"

The result will be:

A.h
... #pragma once etc. ...
class A
{
public:


A() { cout << "class A" <<endl; }

virtual ~A() { }
void OK() { cout << "OK" <<endl; } // should be "void OK() const"
};

A.cpp
.. empty ..

try.cpp
#include <iostream.h>
#include "A.h"
int main()
{
A a;
a.OK();
return 0;
}

Or (second possibility)

A.h


class A
{
public:
A();
virtual ~A();

void OK() const;
};

A.inl


inline A::A() { cout << "class A" <<endl; }
inline A::~A() {}

inline void A::OK() const { cout << "OK" <<endl; }

A.cpp
... empty, used for implementation of complex methods of class A

try.cpp
#include <iostream.h>
#include "A.h"
#include "A.inl"
int main()
{
A a;
a.OK();
return 0;
}

The second solutions allows you to separate the interface from the
implementation.

Regards, Roger

Jing Duan wrote in message <#YOFP8dm#GA.263@cppssbbsa03>...

James Curran

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
Actually, it may replace that with code that look like:
int main()
{
int a = 7;
}

or even
int main()
{}

As a is not referenced in the function.

--
Truth,
James [MVP]
http://www.NJTheater.Com -and-
http://www.NJTheater.Com/JamesCurran

Roger H. Joerg wrote in message ...

Roger H. Joerg

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
Your right.

It will not compile anyway because
it doesn't return a result.

Thanks and regards, Roger

James Curran

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
Which is a bug in VC! main()'s with no return statement, have an
implicit return 0; (A recent addition to the standard)

Roger H. Joerg wrote in message

<#ms1Nvym#GA....@cppssbbsa02.microsoft.com>...

0 new messages