简体   繁体   English

需要帮助将字符串插入函数

[英]Need help inserting a string into a function

I am looking to make repeated programming a little bit easier going forward.我希望以后可以更轻松地进行重复编程。 The program tells a motor to spin based on the manufacturers program guidelines.该程序根据制造商的程序指南告诉电机旋转。 The current code will state:当前代码将说明:

motorname.spin(originallib::directionType::fwd, speed, originallib::velocityUnits::pct);

I want to be able to say:我希望能够说:

int main()
{
run(LeftFront,80);
run(RightFront,80);
}

void run(string motorname, double speed )
{
motorname.spin(originallib::directionType::fwd, speed, originallib::velocityUnits::pct);
}

LeftFront and RightFront have been declared in a previous header file as LeftFront 和 RightFront 已在之前的头文件中声明为

originallib::motor LeftFront=originallib::motor(originallib::PORT2,
                                                originallib::gearSetting::ratio18_1,
                                                true);

The issue I am running into is:我遇到的问题是:

"error: no member named 'spin' in 'std::basic_string' " “错误:'std::basic_string' 中没有名为 'spin' 的成员”

Because the motorname.spin ..... is part of the originallib因为motorname.spin ..... 是 originallib 的一部分

How can I go about achieving this?我怎样才能做到这一点?

void run(string motorname, double speed )

Tells the compiler that motorname is a std::string .告诉编译器motornamestd::string std::string has no spin method. std::string没有spin方法。 Based on基于

run(LeftFront,80);

where LeftFront is a originallib::motor , and assuming originallib::motor does indeed have a spin method, you really the function to look something like其中LeftFront是一个originallib::motor ,并且假设originallib::motor确实有一个spin方法,你的功能真的看起来像

void run(originallib::motor & motor, 
         double speed)
{
    motor.spin(originallib::directionType::fwd, 
               speed, 
               originallib::velocityUnits::pct);
}

so that a motor that can spin is provided instead of a string that can at best std::rotate这样就提供了一个可以spinmotor ,而不是一个最多只能std::rotatestring

An alternative using names would be to have a map of string -> motor key-value pairs that you can look up a motor name in the map and receive the mapped motor , on which you could invoke spin .另一种使用名称的方法是使用string -> motor键值对的映射,您可以在映射中查找电机名称并接收映射的motor ,您可以在其上调用spin This does not seem to be a desirable case here.这在这里似乎不是一个理想的情况。

Sidenote:边注:

You do not want to你不想

originallib::motor LeftFront=originallib::motor(originallib::PORT2,
                                                originallib::gearSetting::ratio18_1,
                                                true);

in a header.在标题中。 This plays havoc on the One Definition Rule if multiple Translation Units include the header as each including translation unit will have its own, equally valid, LeftFront .如果多个翻译单元包含标题,则这会对单一定义规则造成严重破坏,因为每个包含的翻译单元都有自己的、同样有效的LeftFront Include guards will not prevent this because an include guard can only prevent a header from being included multiple times in one translation unit.包含保护不会阻止这一点,因为包含保护只能防止一个标题在一个翻译单元中被多次包含。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM