重新定义c中的类型,但是包括保护?

时间:2015-09-12 02:02:52

标签: c header-files

我有一些代码,我无法在一台计算机上编译。它可以在我的电脑上运行,但在另一个电脑上它不起作用。错误是"重新定义typdef cplx"即使我对每个头文件都有所保护,并且我对typdef的每个定义都有所保护:

          maintabLayout.setOnTabSelectedListener(new       
        TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int numTab = tab.getPosition();
            switch (numTab) {
                case 0://Product
                    ViewPager pviewPager = (ViewPager)            

                    findViewById(R.id.viewpager);
                    String[] pname = {"ProCat1","ProCat2","ProCat3"};
                    pviewPager.setAdapter(new  
                    FragmentPageAdapter(getSupportFragmentManager(),
                            MainActivity.this));
                    return;
                case 1://Store
        ViewPager sviewPager = (ViewPager) findViewById(R.id.viewpager);
                    String[] pname = {"Store1","Store2","Store3"};
                    sviewPager.setAdapter(new 
                  FragmentPageAdapter(getSupportFragmentManager(),
                            MainActivity.this));

                    return;
                case 2://Company
        ViewPager cviewPager = (ViewPager) findViewById(R.id.viewpager);
                    String[] pname = {"Comp1","Comp2","Comp3"};
                    cviewPager.setAdapter(new 
               FragmentPageAdapter(getSupportFragmentManager(),
                            MainActivity.this));

                    return;
                case 3://Service
        ViewPager serviewPager = (ViewPager) findViewById(R.id.viewpager);
                    String[] pname = {"Serv1","Serv2","Serv3"};
                    serviewPager.setAdapter(new 
                 FragmentPageAdapter(getSupportFragmentManager(),
                            MainActivity.this));

                    return;
                case 4://User
                    ViewPager uviewPager = (ViewPager)   
               findViewById(R.id.viewpager);
                    String[] uname = {"Sample1", "Sample2", "Sample3"};
           uviewPager.setAdapter(new 
          FragmentPageAdapter(getSupportFragmentManager(),
                            MainActivity.this));
                    return;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

为什么会出现这种问题? 这是两个头文件。 blas.h:

#ifdef __cplusplus
#include <complex>
#include <cmath>
typedef std::complex<double> cplx;
#else
#include <tgmath.h>
typedef double complex cplx;
#endif

和lapack.h:

#ifndef BLAS_H
#define BLAS_H
#ifdef __cplusplus
#include <complex>
#include <cmath>
typedef std::complex<double> cplx;
#else
#include <tgmath.h>
typedef double complex cplx;
#endif
//declaration of functions
#endif

问题是当我同时包含lapack.h和blas.h时,我收到此错误?

1 个答案:

答案 0 :(得分:3)

您的警卫可以防止两次包含相同的包含文件,但您有两个不同的包含两个不同警卫的包含文件,并且您在每个文件中定义cplx

在每个包含文件中需要为该类型单独保护,如下所示:

#ifndef CPLX
#define CPLX
#ifdef __cplusplus
#include <complex>
#include <cmath>
typedef std::complex<double> cplx;
#else
#include <tgmath.h>
typedef double complex cplx;
#endif
//declarations of functions
#endif