未声明的标识符错误,但变量定义正确(?)

时间:2020-01-03 16:45:35

标签: c++ declaration undeclared-identifier

我已经使用相同的文件已有一段时间了,但是突然进行了一些其他更改,此错误开始弹出

constraints.cpp:105:5:错误:使用未声明的标识符'constraintViolated''constraintViolated'没有在范围中声明

What is an 'undeclared identifier' error and how do I fix it?

我在上述线程中遵循了很多答案,但在我看来并没有相关性。我不确定是否还有其他因素触发了这一点。显然,我已经在标头中声明了变量,但似乎未在cpp文件中读取它们。实际上,还有其他变量也可以执行相同的操作,但是我将示例限制于此。该代码之前运行良好,但是进行了一些小改动使此错误弹出。 可能是构建问题吗?

我尝试添加所有相关代码。代码如下,

constraints.cpp

namespace tudat{


Constraint::Constraint(std::string filenameForConstraintData){

    constraintViolated = 0;
    constraintLengthVehicle = 0.0;
    constraintDiameterVehicle = 2.44;

}

void checkVehicleConstraints(int launchVehicleType, int numberOfStages, double lengthVehicle, double diameterVehicle,  Eigen::Vector3d  diameterMotorCaseStage,
 Eigen::Vector3d diameterNozzleExitStage, Eigen::Vector3d lengthMotorCaseStage){

    constraintViolated = 0;
    constraintLengthVehicle = 25.0;
    constraintDiameterVehicle = 2.44;

if (totalLengthOfTheVehicle/diameterMotorCaseStage1 > maxLOverDRatio){
        constraintViolated = 1;
        if (printConstraints_ == 1){
           std::cout<< "Length is"<< totalLengthOfTheVehicle << std::endl;
                   }
    }
}
constraints.h

namespace tudat
{
class Constraint{
public:

    Constraint(std::string filenameForConstraintData);

    int getConstraintViolated(){ return constraintViolated; }

//    void checkTrajectoryConstraints(const std::vector< tudat::basic_astrodynamics::AccelerationModel3dPointer > listOfAccelerationsActingOnLaunchVehicle,
//                                    const boost::shared_ptr< tudat::RocketLaunchVehicle > launchVehicle,
//                                    const boost::shared_ptr< EnvironmentUpdater > environmentUpdater);


    // void checkVehicleConstraints(int launchVehicleType, int numberOfStages, double lengthVehicle, double diameterVehicle,
                                 // double diameterMotorCaseStage1, double diameterMotorCaseStage2, double diameterMotorCaseStage3,// double diameterMotorCaseStage4,
                                 // double diameterNozzleExitStage1,double diameterNozzleExitStage2, double diameterNozzleExitStage3, // double diameterNozzleExitStage4,
                                 /* double vacuumThrustStage1, double massStartStage1, double deltaVVehicle, double vacuumThrfustStage3, */
                                 // double lengthMotorCaseStage1, double lengthMotorCaseStage2, double lengthMotorCaseStage3 ); //, double lengthMotorCaseStage4);

    void checkVehicleConstraints(int launchVehicleType, int numberOfStages, double lengthVehicle, double diameterVehicle,
                                Eigen::Vector3d diameterMotorCaseStage,
                                Eigen::Vector3d diameterNozzleExitStage, 
                                Eigen::Vector3d lengthMotorCaseStage); 

private:
    int constraintViolated;
    int printConstraints_;



1 个答案:

答案 0 :(得分:1)

使用范围解析修饰符来告诉编译器您的checkVehicleConstraints实现是属于Constraint的实现,因为您不在constraints.cpp的类之内:

void Constraint::checkVehicleConstraints(int launchVehicleType, ...

这样,它就可以访问Constraint的私人成员。