错误:在&x; xxx'之前预期的嵌套名称说明符

时间:2014-12-25 15:10:20

标签: c++ gcc codeblocks

我在编译错误时遇到错误:在"

之前预期的嵌套名称说明符

代码是

 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 ;
 }

我正在使用Ubuntun 12.04,GCC 4.6和CodeBlocks 10.05

3 个答案:

答案 0 :(得分:2)

我在一个文件中创建了这个:

 #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;
 }  

并使用 g ++ -std = c ++ 11 program_name.cc 编译并运行没有任何问题。

答案 1 :(得分:1)

试试这个:

#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;
}

答案 2 :(得分:1)

也许你的意思如下:

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

请记住使用C ++ 11(或者您将收到警告:扩展初始化程序列表仅适用于-std = c ++ 11或-std = gnu ++ 11)