Richard Rublev
Richard Rublev

Reputation: 8162

error: ‘result’ does not name a type

I want to find minimum value in my data set.

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> 
#include <iterator>  


int main(){
std::vector<double> v;
std::ifstream inputFile1("263_V01_C02_R099_THx_BL_128H.dat");

if (inputFile1) {        
    double value;
    while ( inputFile1 >> value ) {
        v.push_back(value);
    }
}

auto result = std::min_element(std::begin(v), std::end(v));

}

I have seen previous responses,people point to iterator not being included.How to solve this?

Upvotes: 0

Views: 843

Answers (2)

Simon Kraemer
Simon Kraemer

Reputation: 5680

As sergej already pointed out auto is a C++11 specifier/keyword.

If you don't have access to a C++11 compiler you could still use this to make your code work.

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> 
#include <iterator>  


int main() {
    std::vector<double> v;
    std::ifstream inputFile1("263_V01_C02_R099_THx_BL_128H.dat");

    if (inputFile1) {
        double value;
        while (inputFile1 >> value) {
            v.push_back(value);
        }
    }

    double result = 0; //Or other default value
    std::vector<double>::iterator minIter = std::min_element(v.begin(), v.end());
    if (minIter != v.end())
    {
        result = (*minIter);
    }
    std::cout << "Result is " << result << std::endl;

}

Additional hint:

auto in your code is the same as std::vector<double>::iterator in this case. So you will have an iterator as result.

You should always check an return value iterator against .end() before using it.

EDIT: Using v.begin() and v.end() instead of std::begin(v) and std::end(v) as pointed out by sergej and NathanOliver.

Upvotes: 2

sergej
sergej

Reputation: 18009

auto is a C++11 specifier. You need a C++11 compiler.

With gcc you can enable the C++11 features using -std=c++11:

gcc exaple:

g++ -std=c++11 main.cpp

From cppreference.com

auto specifier (since C++11) Specifies that the type of the variable that is being declared will be automatically deduced from its initializer.

If you don't have a C++11 compiler, then you need to define the concrete type. Eg:

std::vector<double>::iterator result = ...

BTW: std::begin() and std::end() are also C++11 features.

Upvotes: 3

Related Questions