problem with "inline"

J

John Rambo

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.
 
N

Niels Dekker - no reply address

John said:
//////////////////////// 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
 
J

John Rambo

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 said:
John said:
//////////////////////// 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
 
N

Niels Dekker - no reply address

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

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

John said:
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
 
J

John Rambo

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 .
 
R

Rolf Magnus

Please don't top-post. Rearranged.

John said:
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?
 
J

John Harrison

found it: "std::eek:perator<<"

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::eek:stream), so you don't need to tell the compiler to look for
operator<< in the std namesapce.

john
 
R

rossum

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::eek:perator<<

rossum
 
O

Old Wolf

John Harrison said:
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::eek:stream &operator<<(std::eek:stream &, 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top