Errors got when declaring member functions to be inline

P

Peng Yu

Hi,

I'm trying to define inline_test::test() to be inline. But I always
got errors. I know that if I define inline_test::test() in
inline_test.h, there are no errors.

But I still would rather to put the member functions in *.cc files not
the *.h files. Do you know how to do that?

The following paragraphs are the compile error and the source codes.

Best wishes,
Peng

g++-3.3 -c -O -g -o inline_test.o inline_test.cc
g++-3.3 -c -O -g -o main.o main.cc
g++-3.3 -o main inline_test.o main.o
main.o(.text+0x1b): In function `main':
/usr/include/c++/3.3/ostream:193: undefined reference to
`inline_test::test()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

//inline_test.h
#ifndef INLINE_TEST_H
#define INLINE_TEST_H
using namespace std;
class inline_test{
public:
inline_test();
~inline_test();
int test();
private:
int x;
};
#endif // INLINE_TEST_H



//inline_test.cc
#include "inline_test.h"
using namespace std;
inline_test::inline_test(){
x = 10;
}
inline_test::~inline_test(){
}
inline int inline_test::test(){
return x;
}


//main.cc
#include <iostream>
#include "inline_test.h"
using namespace std;
int main(int argc, char *argv[])
{
inline_test A;
cout << A.test() << endl;
}
 
J

John Harrison

Peng Yu said:
Hi,

I'm trying to define inline_test::test() to be inline. But I always
got errors. I know that if I define inline_test::test() in
inline_test.h, there are no errors.

But I still would rather to put the member functions in *.cc files not
the *.h files. Do you know how to do that?

You must put inline functions in header files. If you don't want to but
function in header files then don't make then inline.

john
 
D

David Lindauer

John said:
You must put inline functions in header files. If you don't want to but
function in header files then don't make then inline.

the reason for this is that inline functions are very very sort of like
macros... a function may not get created for them as a separate entity if
the compiler wants to do that. If the full body of an inline function isn't
visible in the scope that it is used, it won't exist at all in the sense
that it can't be 'expanded inline'... if you *really* want your func to be
in a separate C++ source file from where it is used, you don't use the
inline keyword and then the compiler makes it a fully accessible function.

David
 
D

David White

John Harrison said:
You must put inline functions in header files. If you don't want to but
function in header files then don't make then inline.

That's assuming you want to call them from multiple translation units. I
often don't put private inline functions in header files.

DW
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top