问题:重新定义类

时间:2015-06-27 02:54:45

标签: c++ xcode redefinition

当我尝试编译程序时,我遇到了麻烦。问题在于我有一个“Redefiniciónofclasses”。

.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的类中。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:-1)

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