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:
/tmp/ccB4KNX2.o: In function `B::getB()':
B.cpp:(.text+0x d): undefined reference to `A::get()'
/tmp/ccB4KNX2.o: In function `B::B()':
B.cpp:(.text+0x 29): undefined reference to `A::set(int)'
/tmp/ccB4KNX2.o: In function `B::B()':
B.cpp:(.text+0x 45): 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:
ab.cpp ab.h main main.cpp
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
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
B.cpp:(.text+0x d): undefined reference to `A::get()'
/tmp/ccB4KNX2.o: In function `B::B()':
B.cpp:(.text+0x 29): undefined reference to `A::set(int)'
/tmp/ccB4KNX2.o: In function `B::B()':
B.cpp:(.text+0x 45): undefined reference to `A::set(int)'
collect2: ld returned 1 exit status
>
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
ls
./main
>
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
Comment