By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

problem with "inline"

P: n/a
Hi, I made the following test program:
//////////////////////// classes_1.cpp
#include <iostream>
#include "classes_1.h"
using namespace std;

A::A():i(0){cout <<"const A: i =" << i <<endl;}
A::~A(){cout <<"destr A"<<endl;}
inline void A::showA() {cout << "show A: i =" << i <<endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
class A{
int i;
public:
void set(int ii){i=ii;}
inline void showA();
A();
~A();};
#endif
////////////////////////
and a simple "main.cpp" file which makes an A object , sets an i and shows
it.

It compiles without problems, but I have the following link-error:
->error LNK2001: unresolved external symbol "public: void __thiscall
A::showA(void)" (?showA@A@@QAEXXZ)

when I remove both the inlines I don't have a link-error .

What am I doing wrong ??

Thanks in advance.

Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
John Rambo wrote:
//////////////////////// classes_1.cpp
#include <iostream>
#include "classes_1.h"
using namespace std;

A::A():i(0){cout <<"const A: i =" << i <<endl;}
A::~A(){cout <<"destr A"<<endl;}
inline void A::showA() {cout << "show A: i =" << i <<endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
class A{
int i;
public:
void set(int ii){i=ii;}
inline void showA();
A();
~A();};
#endif
////////////////////////
and a simple "main.cpp" file which makes an A object , sets an i and shows
it.

It compiles without problems, but I have the following link-error:

[...]

Move the implementation of the inline function from classes_1.cpp to
classes_1.h

HTH,

Niels Dekker
www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #2

P: n/a
Thanks, but isn't it possible to place the implementation in the cpp file
(otherwise I have problems with the "using namespace std" which may not be
placed in the header file...) ???

"Niels Dekker - no reply address" <un*****@this.is.invalid> wrote in message
news:41***************@this.is.invalid...
John Rambo wrote:
//////////////////////// classes_1.cpp
#include <iostream>
#include "classes_1.h"
using namespace std;

A::A():i(0){cout <<"const A: i =" << i <<endl;}
A::~A(){cout <<"destr A"<<endl;}
inline void A::showA() {cout << "show A: i =" << i <<endl;};
//////////////////////// classes_1.h
#ifndef A_H
#define A_H
class A{
int i;
public:
void set(int ii){i=ii;}
inline void showA();
A();
~A();};
#endif
////////////////////////
and a simple "main.cpp" file which makes an A object , sets an i and shows it.

It compiles without problems, but I have the following link-error:

[...]

Move the implementation of the inline function from classes_1.cpp to
classes_1.h

HTH,

Niels Dekker
www.xs4all.nl/~nd/dekkerware

Jul 22 '05 #3

P: n/a
John Rambo wrote:

#include <iostream>
#include "classes_1.h"
using namespace std;

inline void A::showA() {cout << "show A: i =" << i <<endl;};
I wrote: Move the implementation of the inline function from classes_1.cpp to
classes_1.h
John Rambo wrote: Thanks, but isn't it possible to place the implementation in the cpp file
Not if you want your member function to be "inline".
(otherwise I have problems with the "using namespace std" which may not be
placed in the header file...) ???


That's a good point. But you don't need the "using namespace std", if
you do "std::"! As follows:

#include <iostream>
inline void A::showA() {std::cout << "show A: i =" << i <<
std::endl;};
Niels Dekker
www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #4

P: n/a
Ok. Thanks a lot; I have still a question, however:
how can I tell the compiler that it is the std-version of << I want to use
?? "using std::<<;" doesn't works .

"Niels Dekker - no reply address" <un*****@this.is.invalid> wrote in message
news:41***************@this.is.invalid...
John Rambo wrote:

#include <iostream>
#include "classes_1.h"
using namespace std;

inline void A::showA() {cout << "show A: i =" << i <<endl;};
I wrote:
Move the implementation of the inline function from classes_1.cpp to
classes_1.h


John Rambo wrote:
Thanks, but isn't it possible to place the implementation in the cpp file
Not if you want your member function to be "inline".
(otherwise I have problems with the "using namespace std" which may not

be placed in the header file...) ???


That's a good point. But you don't need the "using namespace std", if
you do "std::"! As follows:

#include <iostream>
inline void A::showA() {std::cout << "show A: i =" << i <<
std::endl;};
Niels Dekker
www.xs4all.nl/~nd/dekkerware

Jul 22 '05 #5

P: n/a
Please don't top-post. Rearranged.

John Rambo wrote:
John Rambo wrote:
> Thanks, but isn't it possible to place the implementation in the
> cpp file


Not if you want your member function to be "inline".
> (otherwise I have problems with the "using namespace std" which
> may not be placed in the header file...) ???


That's a good point. But you don't need the "using namespace std",
if you do "std::"! As follows:

#include <iostream>
inline void A::showA() {std::cout << "show A: i =" << i <<
std::endl;};


Ok. Thanks a lot; I have still a question, however:
how can I tell the compiler that it is the std-version of << I want to
use ?? "using std::<<;" doesn't works .


Why would you need that?
Jul 22 '05 #6

P: n/a
found it: "std::operator<<"

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cf*************@news.t-online.com...
Please don't top-post. Rearranged.

John Rambo wrote:
John Rambo wrote:
> Thanks, but isn't it possible to place the implementation in the
> cpp file

Not if you want your member function to be "inline".

> (otherwise I have problems with the "using namespace std" which
> may not be placed in the header file...) ???

That's a good point. But you don't need the "using namespace std",
if you do "std::"! As follows:

#include <iostream>
inline void A::showA() {std::cout << "show A: i =" << i <<
std::endl;};


Ok. Thanks a lot; I have still a question, however:
how can I tell the compiler that it is the std-version of << I want to
use ?? "using std::<<;" doesn't works .


Why would you need that?

Jul 22 '05 #7

P: n/a
On Sat, 14 Aug 2004 18:45:16 GMT, John Rambo <Ia*****@hotmail.com> wrote:
found it: "std::operator<<"


It's very unlikely that you would need that.

std::cout << "hello\n";

That works perfectly well because C++ has a rule (sometimes called Koenig
lookup) which says that if the type of an argument to a particular
function or operator is in a particular namespace then that namespace is
searched automatically. So type type of cout is in the std namespace
(std::ostream), so you don't need to tell the compiler to look for
operator<< in the std namesapce.

john
Jul 22 '05 #8

P: n/a
On Sat, 14 Aug 2004 15:28:49 GMT, "John Rambo" <Ia*****@hotmail.com>
wrote:
Ok. Thanks a lot; I have still a question, however:
how can I tell the compiler that it is the std-version of << I want to use
?? "using std::<<;" doesn't works .

If you really need to do that then try:
using std::operator<<

rossum
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #9

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote:
On Sat, 14 Aug 2004 18:45:16 GMT, John Rambo <Ia*****@hotmail.com> wrote:
found it: "std::operator<<"


It's very unlikely that you would need that.

std::cout << "hello\n";

That works perfectly well because C++ has a rule (sometimes called Koenig
lookup) which says that if the type of an argument to a particular
function or operator is in a particular namespace then that namespace is
searched automatically.


I suppose he wanted to guard against something like:

// in header
namespace foo
{
std::ostream &operator<<(std::ostream &, char const *);
};

// in non-header
using namespace foo;
int bar()
{
std::cout << "hello\n";
}
};

(but I would expect the compiler to complain about ambiguous
function)
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.