Returning a const reference brokes dependency among header files?

K

Kermit Mei

Hello all, I wrote four simple c++ source files for this test. Look
please:
/*************1***************/
/// file A.h
#ifndef A_H
#define A_H

class A {
public:
void set(int i);
const int & get();
private:
int a;
};

#endif

/**************2****************/
///file A.cpp
#include "A.h"


inline void A::set(int i) {
a = i;
}

inline const int & A::get() {
return a;
}


/*****************3*******************/
///fileB.h
#ifndef B_H
#define B_H
#include "A.h"

class A;

class B
{
public:
B();

const int & getB();
private:
A ab;
};

#endif

/*******************4****************/
/// file B.cpp
#include "B.h"

B::B()
{ ab.set(19); }

const int & B::getB()
{ return ab.get(); }


/*******************main.cpp*********/
#include "B.h"
#include <iostream>

using namespace std;

int main()
{
B ab;
cout << ab.getB() << endl;
}

/*************************************/
Now, compile them and the errors occurs:
c++ -Wall -o main main.cpp A.cpp B.cpp
/tmp/ccB4KNX2.o: In function `B::getB()':
B.cpp:(.text+0xd): undefined reference to `A::get()'
/tmp/ccB4KNX2.o: In function `B::B()':
B.cpp:(.text+0x29): undefined reference to `A::set(int)'
/tmp/ccB4KNX2.o: In function `B::B()':
B.cpp:(.text+0x45): undefined reference to `A::set(int)'
collect2: ld returned 1 exit status
It's funny that if you merge them into one head file, then
you can run it:

/************* ab.h ************/
#ifndef T_H
#define T_H

class A {
public:
void set(int i);
const int & get();
private:
int a;
};

class B
{
public:
B();
const int & getB();
private:
A ab;
};

#endif


/*********** ab.cpp *****************/
#include "t.h"

inline void A::set(int i) {
a = i;
}

inline const int & A::get() {
return a;
}

B::B()
{ ab.set(19); }

const int & B::getB()
{ return ab.get(); }

/***********main.cpp***************/
#include <iostream>
using namespace std;

#include "ab.h"

int main()
{
B ab;
cout << ab.getB() << endl;;
}

To compile and run them like this:
> c++ -Wall -o main main.cpp ab.cpp
> ls ab.cpp ab.h main main.cpp
> ./main 19
>

/************End******************/

I hope anybody would like to tell me what cause it.
Dose returning a const reference broke the
dependency among header files?

Now, I need your help, if you would like to tell me why,
please cc your e-mail to me.
Thank you,very much!

Kermit
 
K

Kermit Mei

Remove the inline keyword.

application_pgp-signature_part
< 1KViewDownload

Sam, it works well after remove them.

But, sir, would you like to tell me why it can not be defined as
"inline"?
"inline" is just a suggestion for the compiler, isn't it? Then why
there're
something wrong with its arising? Why the compiler can NOT ignore it
here?

What's more, why it can be compiled with "inline" if A and B are
defined and
declared in a same file together?

Thank you very much!
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top