对于noob问题抱歉..
devf.h
#ifndef DEVF_H
#define DEVF_H
#include <string>
struct A {
int ax();
std::string ay(std::string jj);
};
struct B {
struct A* f;
int bx() {
return f->ax();
}
std::string by(std::string jj){
return f->ay(jj);
}
};
#endif // DEVF_H
devf.cpp
#include "devf.h"
int A::ax() {
return 5;
}
std::string A::ay(std::string jj){
return jj;
}
我收到此错误:
multiple definition of `A::ax()'
multiple definition of `A::ay(std::string)'
我该如何解决这个问题?我希望头文件中ax
和ay
函数的定义以及.cpp
答案 0 :(得分:1)
尝试使用您的代码执行此操作:
<强> devf.h 强>
#ifndef DEVF_H // Add this line
#define DEVF_H // Add this line
// #include <iostream> // removed this as not needed from what is shown
#include <string> // added this as is needed
struct A {
int ax() const; // added const as it doesn't change
std::string ay( const std::string& jj ) const; // changed string to const ref & added const
};
struct B {
/*struct*/ A* f; // keyword struct not needed here.
int bx() const { // added const
return f->ax();
}
std::string by( const std::string& jj ) const { // changed to const ref & added const
return f->ay( jj );
}
};
#endif // !DEVF_H // Add this line
<强> devf.cpp 强>
#include "devf.h"
int A::ax() const {
return 5;
}
std::string A::ay( const std::string& jj ) const {
return jj;
}
然后你问这个或发表了这样的声明:
我该如何解决这个问题?我想要头文件中的ax和ay函数的定义以及.cpp
中的实现
<强> devf.h 强>
#ifndef DEVF_H
#define DEVF_H
#include <string>
struct A {
int ax() const;
std::string ay( const std::string& jj ) const;
};
struct B {
A* f;
int bx() const;
std::string by( const std::string& jj ) const;
};
#endif // !DEVF_H
<强> devf.cpp 强>
#include "devf.h"
int A::ax() const {
return 5;
}
std::string A::ay( const std::string& jj ) const {
return jj;
}
int B::bx() const {
return f->ax();
}
std::string B::by( const std::string& ) const {
return f->ay( jj );
}
这应该对你所展示的内容有所帮助。
我已经尝试过这个:
<强>的main.cpp 强>
#include <iostream>
#include "devf.h"
int main() {
B b;
std::cout << b.bx() << std::endl;
std::cout << b.by( "hello world" ) << std::endl;
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return 0;
}
输出
5
hello world
Press any key and enter to quit.
我问OP:您是否包含其他头文件?
OP回答说:是的,标题&#34; devf.h&#34;包含在其他标题中
我相信这是OP问题所在。我认为OP是Circular Includes
的受害者,这将导致多个定义。
示例:
<强> A.H 强>
#ifndef A_H
#define A_H
#include "B.h" // circular include
struct A {
int a;
B b;
};
#endif // !A_H
<强> B.h 强>
#ifndef B_H
#define B_H
#include "A.h" // circular include
struct B {
int b;
};
#endif // !B_H
要解决此循环问题...
class
或struct prototypes
- forward declarations
;适用于pointer & reference types
。definition
不是declaration
,则只在头文件中包含其他头文件。class
或struct's forward declaration
后
删除class's
include directive
并将其放在其cpp文件修复上述示例:
<强> A.H 强>
#ifndef A_H
#define A_H
// Since A has a pointer to B we remove the include from here
// and replace it with a forward declaration or class prototype...
// #include "b.h" // cicular include
class B;
struct A {
int a;
B b;
};
#endif // !A_H
<强> A.cpp 强>
#include "A.h"
#include "B.h" // moved the include to here - prevents circular includes
<强> B.h 强>
#ifndef B_H
#define B_H
// Since B does not require A we can omit this all together.
//#include "A.h" // circular include
struct B {
int b;
};
#endif // !B_H
<强> B.cpp 强>
#include "B.h"
您可以在堆栈上引用此Q & A
Resolve build errors due to circular dependency amongst classes以获取更详细的说明。
一旦完成了其余的代码库并修复了所有的循环包含,你应该没有错误,因为上面的类集编译运行没有错误。