简体   繁体   中英

error: expected nested-name-specifier before 'xxx'

I'm getting a error when compiling 'error: expected nested-name-specifier before"

The code is

 using range = std::pair<float,float> ;
 range make_range( float a, float b ) { return { std::min(a,b), std::max(a,b) } ; }
 bool intersects( range a, range b )
 {
    if( a > b ) std::swap(a,b) ;
    return a.second >= b.first ;
 }

I'm using Ubuntun 12.04, GCC 4.6, and CodeBlocks 10.05

I created this in a file:

 #include <utility>
 #include <algorithm>
 #include <iostream>

 using range = std::pair<float,float> ; 

 range make_range( float a, float b ) { return { std::min(a,b), std::max(a,b) } ; }

 bool intersects( range a, range b )
 {
    if( a > b ) std::swap(a,b) ;
    return a.second >= b.first ;
 }

 int main()
 {
   float x =1.0;
   float y =10.0;
   range pair_1 = make_range( x, y);
   range pair_2 = make_range(-2, 6);

   bool brs = intersects( pair_1, pair_2 );
   std::cout<<std::get<0>(pair_1)<<"  "<<std::get<1>(pair_1)<<std::endl;
   std::cout<<std::get<0>(pair_2)<<"  "<<std::get<1>(pair_2)<<std::endl;
   std::cout<<brs<<std::endl
   return 0;
 }  

and using g++ -std=c++11 program_name.cc it compiled and ran without any problem.

Try this:

#include<tuple>
#include<algorithm>

using range = std::pair<float, float>;
range make_range(float a, float b) { return{ std::min(a, b), std::max(a, b) }; }
bool intersects(range a, range b)
{
    if (a > b) std::swap(a, b);
    return a.second >= b.first;
}

Perhaps you meant the following:

typedef std::pair<float,float> range;

Remember to use C++11 (or you will get the warning: extended initializer lists only available with -std=c++11 or -std=gnu++11)

不是因为头文件,可能是因为c++版本不是c++11(尝试设置编译选项为-std=c++11)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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