【问题标题】:Issue: Redefinition of Class问题:重新定义类
【发布时间】:2015-06-27 02:54:45
【问题描述】:

我在尝试编译我的程序时遇到了麻烦。问题是我有一个“类的重新定义”。

.cpp 代码

#include "atmosfericConditions.h"
#include <iostream>

using namespace std;

class clsPressure{     //Redefinition of "clsPressure"
    float pressure;
public:
    clsPressure(){}
    clsPressure(float presion){
        pressure = presion;
    }
    friend istream& operator >>(istream &i, clsPressure &e);
    friend ostream& operator <<(ostream &o, const clsPressure &s);
};

istream& operator >>(istream &i, clsPressure &e){
    char sign;
    i >> e.pressure >> sign >> sign;
    return i;
}

ostream& operator <<(ostream &o, const clsPressure &s){
    o << s.pressure << " Pa";
    return o;
}


class clsDensity{    //Redefinition of "clsDensity"
    float density;
public:
    clsDensity(){}
    clsDensity(float densdad){
        density = densdad;
    }
    friend istream& operator >>(istream &i, clsDensity &e);
    friend ostream& operator <<(ostream &o, const clsDensity &s);
};

istream& operator >>(istream &i, clsDensity &e){
    char sign;
    i >> e.density >> sign >> sign >> sign >> sign >> sign >> sign;
    return i;
}

ostream& operator <<(ostream &o, const clsDensity &s){
    o << s.density << " Kg/m^3";
    return o;
}


class clsSoundVelocity{    //Redefinition of "clsSoundVelocity"
    float soundVelocity;
public:
    clsSoundVelocity(){}
    clsSoundVelocity(float velocidadDelSonido){
        soundVelocity = velocidadDelSonido;
    }
    friend istream& operator >>(istream &i, clsSoundVelocity &e);
    friend ostream& operator <<(ostream &o, const clsSoundVelocity &s);
};

istream& operator >>(istream &i, clsSoundVelocity &e){
    char sign;
    i >> e.soundVelocity >> sign >> sign >> sign;
    return i;
}

ostream& operator <<(ostream &o, const clsSoundVelocity &s){
    o << s.soundVelocity << " m/s";
    return o;
}


class clsDynamicViscocity{    //Redefinition of "clsDynamicViscocity"
    double dynamicViscocity;
public:
    clsDynamicViscocity(){}
    clsDynamicViscocity(double viscocidadDinamica){
        dynamicViscocity = viscocidadDinamica;
    }
    friend istream& operator >>(istream &i, clsDynamicViscocity &e);
    friend ostream& operator <<(ostream &o, const clsDynamicViscocity &s);
};

istream& operator >>(istream &i, clsDynamicViscocity &e){
    char sign;
    i >> e.dynamicViscocity >> sign >> sign >> sign >> sign >> sign;
    return i;
}

ostream& operator <<(ostream &o, const clsDynamicViscocity &s){
    o << s.dynamicViscocity << " N/m^2";
    return o;
}

前一个.cpp的.h代码

//
//  atmosfericConditions.h
//  IASS Project
//
//  Created by Oscar Espinosa on 6/26/15.
//  Copyright (c) 2015 IPN ESIME Ticoman. All rights reserved.
//

#ifndef atmosfericConditions_h
#define ATMOSFERICCONDITIONS_H_

#include <iostream>
using namespace std;

class clsPressure{
    float pressure;
public:
    clsPressure(){}
    clsPressure(float presion){
        pressure = presion;
    }
    friend istream& operator >>(istream &i, clsPressure &e);
    friend ostream& operator <<(ostream &o, const clsPressure &s);
};
class clsDensity{
    float density;
public:
    clsDensity(){}
    clsDensity(float densdad){
        density = densdad;
    }
    friend istream& operator >>(istream &i, clsDensity &e);
    friend ostream& operator <<(ostream &o, const clsDensity &s);
};
class clsSoundVelocity{
    float soundVelocity;
public:
    clsSoundVelocity(){}
    clsSoundVelocity(float velocidadDelSonido){
        soundVelocity = velocidadDelSonido;
    }
    friend istream& operator >>(istream &i, clsSoundVelocity &e);
    friend ostream& operator <<(ostream &o, const clsSoundVelocity &s);
};
class clsDynamicViscocity{
    double dynamicViscocity;
public:
    clsDynamicViscocity(){}
    clsDynamicViscocity(double viscocidadDinamica){
        dynamicViscocity = viscocidadDinamica;
    }
    friend istream& operator >>(istream &i, clsDynamicViscocity &e);
    friend ostream& operator <<(ostream &o, const clsDynamicViscocity &s);
};

#endif

我注意到,它在 .cpp 和 .h 中的代码相同。我一直在寻找视频中的一些答案,但我找不到任何可以帮助我的东西。我尝试删除 .cpp 并仅保留 .h,但它不起作用。

如您所见,Xcode 通知我问题出在 .cpp 的类中。

【问题讨论】:

    标签: c++ xcode redefinition


    【解决方案1】:

    这是在抱怨,因为相同的类被定义了两次。

    您不需要在 .h 和 .cpp 文件中都有类。将它们放在 .h 中并从 .cpp 文件中删除它们,因为 .cpp 包含 .h 文件。

    在 .CPP 文件中,仅删除类定义,保留其余部分。您的 .cpp 文件应如下所示:

    #include "atmosfericConditions.h"
    
    istream& operator >>(istream &i, clsPressure &e){
        char sign;
        i >> e.pressure >> sign >> sign;
        return i;
    }
    
    ostream& operator <<(ostream &o, const clsPressure &s){
        o << s.pressure << " Pa";
        return o;
    }
    
    istream& operator >>(istream &i, clsDensity &e){
        char sign;
        i >> e.density >> sign >> sign >> sign >> sign >> sign >> sign;
        return i;
    }
    
    ostream& operator <<(ostream &o, const clsDensity &s){
        o << s.density << " Kg/m^3";
        return o;
    }
    
    istream& operator >>(istream &i, clsSoundVelocity &e){
        char sign;
        i >> e.soundVelocity >> sign >> sign >> sign;
        return i;
    }
    
    ostream& operator <<(ostream &o, const clsSoundVelocity &s){
        o << s.soundVelocity << " m/s";
        return o;
    }
    
    istream& operator >>(istream &i, clsDynamicViscocity &e){
        char sign;
        i >> e.dynamicViscocity >> sign >> sign >> sign >> sign >> sign;
        return i;
    }
    
    ostream& operator <<(ostream &o, const clsDynamicViscocity &s){
        o << s.dynamicViscocity << " N/m^2";
        return o;
    }
    

    【讨论】:

    • 行得通!您是否建议以这种方式使用阅读器,还是尽可能保留 .cpp 文件?
    • 建议标头应仅包含声明,.cpp 文件包含详细信息。
    • 这只是个人喜好,但我尝试将 .cpp 用于我希望增长的类,因为您并不总是希望所有代码都内联(意思是,在类定义中定义) .
    【解决方案2】:

    如果类声明 .cpp 文件重复,您可以删除它。如果他们服务 不同的功能,那么您可以使用不同的命名空间来解决编译错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多