错误:'xxx'之前应嵌套名称说明符

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

本文关键字:嵌套 说明符 xxx 错误      更新时间:2023-10-16

我在编译"错误:之前预期的嵌套名称说明符"时遇到错误

代码是

 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

我在文件中创建了这个:

 #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,它编译和运行没有任何问题。

也许你的意思是:

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

请记住使用 C++11(否则您将收到警告:扩展初始值设定项列表仅适用于 -std=c++11 或 -std=gnu++11)

试试这个:

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

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