【问题标题】:Undeclared identifier error, but variable correctly defined (?) [duplicate]未声明的标识符错误,但变量正确定义(?)[重复]
【发布时间】:2020-01-03 16:45:35
【问题描述】:

我一直在使用相同的文件有一段时间了,但是突然做了一些其他的更改,这个错误开始弹出

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_;



【问题讨论】:

    标签: c++ declaration undeclared-identifier


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。之前做过,修改的时候忘记再次声明了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多