F
firegun9
I am not a newbie C++, but I haven't coded multiple files project under
g++.
Here is my quesiont:
b.h
////////////////////
#ifndef b_h
#define b_h
class b{
int b1;
public:
b(){}
void set(int b1_);
};
void b::set(int b1_)
{
b1=b1_;
}
#endif
a.h
===========================
#include "b.h"
class a{
b* b_ptr;
public:
void set(b & b_obj);
};
void a::set(b & b_obj)
{
b_ptr=&b_obj;
}
main.cpp
////////////////////////////
#include "a.h"
#include "b.h"
int main()
{
a a_obj;
b b_obj;
a_obj.set(b_obj);
return 0;
}
For these 3 files, I can successfully compile and link by using "g++
main.cpp".
However, if I separate a.h into a.h and a.cpp, like here:
a.h
===========================
#include "b.h"
class a{
b* b_ptr;
public:
void set(b & b_obj);
};
a.cpp
/////////////////////////
#include "a.h"
void a::set(b & b_obj)
{
b_ptr=&b_obj;
}
then I use the command "g++ a.cpp main.cpp", I got a link error
"ld: fatal: symbol 'b::set(int)' is multiply-defined:
(file /var/tmp/ccUoZBm0.o type=FUNC; file /var/tmp/ccG0aQup.o
type=FUNC)"
and if I also separate b.h into b.h and b.cpp in the same way, the
error is gone.
What's going on here?
I just want to separate one of the files because in my real project,
there's only one .h file is so long.
g++.
Here is my quesiont:
b.h
////////////////////
#ifndef b_h
#define b_h
class b{
int b1;
public:
b(){}
void set(int b1_);
};
void b::set(int b1_)
{
b1=b1_;
}
#endif
a.h
===========================
#include "b.h"
class a{
b* b_ptr;
public:
void set(b & b_obj);
};
void a::set(b & b_obj)
{
b_ptr=&b_obj;
}
main.cpp
////////////////////////////
#include "a.h"
#include "b.h"
int main()
{
a a_obj;
b b_obj;
a_obj.set(b_obj);
return 0;
}
For these 3 files, I can successfully compile and link by using "g++
main.cpp".
However, if I separate a.h into a.h and a.cpp, like here:
a.h
===========================
#include "b.h"
class a{
b* b_ptr;
public:
void set(b & b_obj);
};
a.cpp
/////////////////////////
#include "a.h"
void a::set(b & b_obj)
{
b_ptr=&b_obj;
}
then I use the command "g++ a.cpp main.cpp", I got a link error
"ld: fatal: symbol 'b::set(int)' is multiply-defined:
(file /var/tmp/ccUoZBm0.o type=FUNC; file /var/tmp/ccG0aQup.o
type=FUNC)"
and if I also separate b.h into b.h and b.cpp in the same way, the
error is gone.
What's going on here?
I just want to separate one of the files because in my real project,
there's only one .h file is so long.