gcc 错误:在“xxx”之前需要嵌套名称说明符

2j4z5cfb  于 2022-11-13  发布在  其他
关注(0)|答案(4)|浏览(186)

编译“error:”时出现错误”“之前应为嵌套名称说明符
代码为

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

我使用的是Ubuntu 12.04、GCC 4.6和代码块10.05

chhkpiq4

chhkpiq41#

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

#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++11program_name.cc***编译和运行时没有任何问题。

nhaq1z21

nhaq1z212#

您的意思可能如下:

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

请记住使用C11(否则您将收到警告:扩展初始化器列表仅在使用-std=c11或-std=gnu++11时可用)

xytpbqjk

xytpbqjk3#

试试看:

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

olhwl3o24#

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

相关问题