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

huangapple go评论79阅读模式
英文:

Undeclared identifier error, but variable correctly defined (?)

问题

I've been using the same files for a while now, but suddenly making some other changes, this error started popping up

constraints.cpp:105:5: error: use of undeclared identifier 'constraintViolated';
'constraintViolated' was not declared in scope

https://stackoverflow.com/questions/22197030/what-is-an-undeclared-identifier-error-and-how-do-i-fix-it

I followed quite a few answers in the above thread but didn't seem relevant in my case. I'm not sure if there's something else triggering this. Clearly I've declared the variables in the header but they don't seem to have been read in the cpp file. In fact, there are other variables that do the same, but I'll limit my example to this. The code worked perfectly before, but some minor modifications have made this error pop up.
Could it be a build issue?

I've tried to add all the relevant code. The code is below,

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_;
英文:

I've been using the same files for a while now, but suddenly making some other changes, this error started popping up

constraints.cpp:105:5: error: use of undeclared identifier 'constraintViolated';
'constraintViolated' was not declared in scope

https://stackoverflow.com/questions/22197030/what-is-an-undeclared-identifier-error-and-how-do-i-fix-it

I followed quite a few answers in the above thread but didn;t seem relevant in my case. I'm not sure if there's something else triggering this. Clearly I've declared the variables in the header but they dont seem to have been read in the cpp file. In fact, there are other variables that do the same,but i'll limit my example to this. The code worked perfectly before, but some minor modifications have made this error pop up.
Could it be a build issue?

I've tried to add all the relevant code. The code is below,

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

得分: 1

使用作用域分辨符来告诉编译器,你的 checkVehicleConstraints 实现属于 Constraint,因为你不在 constraints.cpp 中的类内部:

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

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

英文:

Use a scope resolution modifier to tell the compiler that your checkVehicleConstraints implementation is the one belonging to Constraint since you're not within the class in your constraints.cpp:

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

That way it'll have access to Constraint's private members.

huangapple
  • 本文由 发表于 2020年1月4日 00:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定