C++ 结构函数的多重定义

C++ multiple definition of struct functions

抱歉这个菜鸟问题..

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)'

我该如何解决这个问题?我想要头文件中 axay 函数的定义以及 .cpp

中的实现

尝试使用您的代码执行此操作:

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;
}

然后你这样问或这样说:

How can I solve this problem? I want the definitions of ax and ay functions in header file and the implementions in .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:你包括其他 header 文件吗?

OP 回答:

Yes, the header "devf.h" is included in other headers

我相信这就是 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

要解决此循环包含问题...

  • 您需要在header中使用classstruct prototypes-forward declarations;适用于 pointer & reference types.
  • 如果需要 definition 而不是 declaration,则仅在 header 文件中包含其他 header 文件。
  • 一旦你在 header 文件中有了 classstruct'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 on stack以获得更详细的解释。


一旦你完成了你的代码库的其余部分并修复了所有的循环包含你应该没有错误,因为上面的 类 集编译并且 运行 没有错误。