错误C2011:'成员':上移动结构到一个新的头文件'结构'重新定义类型 [英] error C2011: 'member' : 'struct' type redefinition on moving a struct to a new header file

查看:154
本文介绍了错误C2011:'成员':上移动结构到一个新的头文件'结构'重新定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类code .h文件 - overlay.h

I had a .h file with some class code - overlay.h

    #include<iostream>
    #include<boost/thread.hpp> 
    #include<vector>
    #include<boost/asio.hpp> 
    #include <string>
    #include <boost/serialization/vector.hpp>
    #include <boost/archive/text_oarchive.hpp> 
    #include <boost/archive/text_iarchive.hpp> 
    #include <sstream> 
    #include <boost/tuple/tuple.hpp>
    #include<member.h>
    using boost::asio::ip::tcp;

    class overlay_server{...};

struct member{
    std::string ip_address;
    short int port;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & ip_address;
        ar & port;
    }
};

现在我搬到这个结构到另一个名为member.h新文件
,其中包括此文件,所以我班overlay_server可以使用它。
现在,当我建立程序出现错误。

Now I moved the struct to another new file called member.h and included this file so my class overlay_server could use it. Now when I build the program I get the error.

我要做出什么样的变化,使这项工作?
我读到的SO头后卫,但想不出真正了解如何在这里实现它要解决的问题。

What changes should I make to make this work? I read about header guards on SO, but couldnt really understand how to implement it here to solve the problem.

---- ----修改

----edit----

member.h

struct member{
    std::string ip_address;
    short int port;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & ip_address;
        ar & port;
    }
};

run.cpp

run.cpp

    #include<overlay_server.h>
#include<overlay_client.h>

int main(){

    overlay_server overlay_server_(8002);
    boost::thread thread_(boost::bind(&overlay_server::member_list_server, overlay_server_));


    overlay_client overlay_client_("127.0.0.1",8002);
    overlay_client_.member_list_client();

    thread_.join();
}

我没有结构的重新定义的任何地方。
我有一个称为另一个类overlay_client它也使用struct成员。

I dont have redefinition of struct anywhere. I have another class called overlay_client it also uses struct member.

在我的主要功能,我同时创建overlay_server和overlay_client的对象。
现在,只有当member.h包括在overlay_server.h(虽然在这两个overlay_server和overly_client code要求的话)我的程序运行
如果包含在这两个那么它我得到重新定义错误

In my main function, i create objects of both overlay_server and overlay_client. Now my program runs only if member.h is included in overlay_server.h (though code in both overlay_server and overly_client requires it) if its included in both then i get the redefinition error

为什么?

---- ----修改

----edit----

在我member.h这个code解决问题
<一href=\"http://stackoverflow.com/questions/5825084/compile-error-struct-type-redefinition-although-its-the-first-definition-fo/5825130#5825130\">Compile错误&QUOT;'结构'重新定义类型和QUOT;虽然它是第一个定义它

this code in my member.h solves the issue Compile error "'struct' type redefinition" although it's the first definition for it

推荐答案

这是发生了什么。
你有

This is whats happening. you have

member.h

包含在 overlay_server.h overlay_client.h

现在,当你有这两个在main.cpp中

now when you include these two in main.cpp

这就像你在main.cpp中做这个(实际上是preprocessor展开如下图所示)

it is like you are doing this in main.cpp(actually preprocessor expands like below)

#include"member.h"
#include"member.h"

所以它通常是这样完成后膨胀

so it typically will be like this after complete expansion

struct member{...};
struct member{...};    //redifinition!!

所以编译器解析它作为结构成员(因为它会参观member.h两次读取成员结构的DEF)。

so the compiler parses it as being two definition of struct member(because it will visit member.h twice and read the def of member struct).

怎样做才能避免这种

在member.h添加此

in member.h add this

#ifndef MEMBER_DECL    //initially not defined
#define MEMBER_DECL    //include guard(now first time you enter this MEMBER_DECL will get defined. so second time compiler comes here it skips this.)
struct member
{
 //rest here
};

#endif

现在主你也会有这种

#include"member.h" //when this happens MEMBER_DECL is defined

所以

//#include"member.h" member will not be expanded again hence resolving your redfinition 

这篇关于错误C2011:'成员':上移动结构到一个新的头文件'结构'重新定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆