简体   繁体   English

非标准语法; 使用 '&' 创建指向成员 C++ 的指针

[英]non-standard syntax; use '&' to create a pointer to member C++

I have a problem running my small piece of code.我在运行我的一小段代码时遇到问题。 I have a vector who reads in temperature from User Input.我有一个从用户输入读取温度的向量。 I want to process the data a bit.我想稍微处理一下数据。 Giving out which kind of day it was (hot day, summer day, etc...) and which day was the hottest.给出这是哪一天(炎热的一天,夏天的一天,等等......),哪一天是最热的。 But everytime I want to get the maximum temperature with the function maxNumber I get 2 errors which I dont understand:但是每次我想用 function maxNumber 获得最高温度时,我都会遇到 2 个我不明白的错误:

non-standard syntax; use '&' to create a pointer to member

"! =": Function overload cannot be resolved

Please help!请帮忙! Thx very much非常感谢

Code:代码:

#include <iostream>
#include <vector>

std::string klimaTag(float a) {
    if (a >= 25 and a < 30) {
        return "Sommertag";
    }
    else if (a >= 30 and a < 35) {
        return "Heißer Tag";
    }
    else if (a >= 35) {
        return "Wüstentag";
    }
    else {
        return "Normaltag";
    }
}

float maxNumber(std::vector<float> &a) {
    float current_max = 0;
    for (int i = a.begin; i != a.end; i++) {
        if (a.at(i) > current_max) {
            current_max = a.at(i);
        }
        return current_max;
    }
}


int main()
{
    std::vector<float> temperatures;
    float current_temp;
    
    for (int i = 0; i < 5; i++) {
        std::cout << "Hoechsttemp für Tag " << i << " eingeben: ";
        std::cin >> current_temp;
        temperatures.push_back(current_temp);
    }

    for (int i = 0; i < 5; i++) {
        std::cout << "Tag " << i +  1 << " ist ein " << klimaTag(temperatures.at(i)) << std::endl;
    }
    std::cout << maxNumber(temperatures);
}

First of all begin() and end() are method, so you're getting that error because you're trying to reference a function so it should require an address operator.首先begin()end()是方法,所以你得到这个错误是因为你试图引用一个 function 所以它应该需要一个地址运算符。

Second is begin() and end() return an iterator and not an index, and you're clearing trying to access like index.其次是begin()end()返回一个iterator而不是一个索引,并且您正在清除尝试访问类似索引。

Third thing is you're always returning after the first cycle as your return is inside it and should be outside.第三件事是你总是在第一个周期之后返回,因为你的返回在里面并且应该在外面。

To loop correctly your array is enough to use the for range loop要正确循环,您的数组足以使用 for range 循环

float maxNumber(std::vector<float> &a) {
    float current_max = 0;
    for(const auto& element : a) {
     if(element > current_max){
            current_max = element; 
        }
    }
    return current_max;
}

if you want to use the old way you can always do如果您想使用旧方式,您可以随时使用

float maxNumber(std::vector<float> &a) {
    float current_max = 0;
    for(int i=0; i<a.size(); ++a) {
     if(a.at(i) > current_max){
            current_max = a.at(i); 
        }
    }
    return current_max;
}

a.end refers to the function address, you need to invoke the function itself with a.end() hence the complaint about the non standard syntax, as if you meant to take the function address you could call &a.end . a.end指的是 function 地址,您需要使用a.end()调用 function 本身,因此抱怨非标准语法,就好像您打算使用 ZC1C425268E687.9444 可以调用的地址&a.end Same with a.begin .a.begin相同。

To use the for loop as you described, you need int i = 0; i < a.size()要按照您的描述使用 for 循环,您需要int i = 0; i < a.size() int i = 0; i < a.size() . int i = 0; i < a.size() Moreover the return statement is inside the for loop in maxNumber.此外, return语句位于 maxNumber 中的 for 循环内。 That's not what you want (it exits at the first loop cycle), put it outside.这不是你想要的(它在第一个循环周期退出),把它放在外面。

Begin and end are actually methods for working with iterators.开始和结束实际上是使用迭代器的方法。 To use them you should change a bit the loop.要使用它们,您应该稍微改变一下循环。

begin and end are functions which need to be called like functions: begin() and end() . beginend是需要像函数一样调用的函数: begin()end() However, you can use range-based for loop like:但是,您可以使用基于范围的 for 循环,例如:

float maxNumber(std::vector<float> &a) {
    float current_max = 0;
    for (auto const i : a) {
        if (i > current_max) {
            current_max = i;
        }
    }
    return current_max;
}

Please, note where I put your return statement.请注意我把你的退货声明放在哪里。 It is outside of the for loop.它在 for 循环之外。

暂无
暂无

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

相关问题 c ++非标准语法; 使用“&”创建具有虚拟功能的成员的指针 - c++ non-standard syntax; use '&' to create a pointer to member with virtual function C ++ Visual Studio非标准语法使用&#39;&&#39;创建指向成员的指针 - C++ Visual Studio non-standard syntax use '&' to create a pointer to member C ++ Visual Studio 2015“非标准语法; 使用“&”创建指向成员的指针” - C++ Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member” C ++ Visual Studio 2015:非标准语法; 使用“&”创建指向成员的指针 - C++ Visual Studio 2015: non-standard syntax; use '&' to create a pointer to member 错误信息:非标准语法; 使用“&amp;”创建指向成员的指针 - Error Message: non-standard syntax; use '&' to create a pointer to member 非标准语法; 使用“&”创建指向成员的指针 - non-standard syntax; use '&' to create a pointer to member 非标准语法,使用 &amp; 创建指向成员的指针 - Non-standard syntax, use & to create a pointer to member c3867&#39;sf::Time::as seconds&#39;:非标准语法;使用&#39;&amp;&#39;创建指向成员的指针 - c3867'sf::Time::as seconds':non-standard syntax ;use '&'to create a pointer to a member C3867:&#39;_com_error :: Description&#39;:非标准语法 使用“&”创建指向成员的指针 - C3867 : '_com_error::Description': non-standard syntax; use '&' to create a pointer to member 非标准语法 - 使用 &amp; 创建指向成员的指针:C++ function 指向成员的指针 - Non standard syntax - Use & to create a pointer to a member : C++ function pointer to member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM