C++ 常量引用返回和STL向量的问题

C++ 常量引用返回和STL向量的问题,c++,stl,vector,reference,constants,C++,Stl,Vector,Reference,Constants,我对以下代码片段有问题 string const& symbol::at(int index) const { assert(index<symbol_data.vector::size()); return symbol_data.vector::at(index); } 有人知道如何让STL向量与常量引用一起正常工作吗?在我写这篇文章时,您的代码: string const& symbol::at(int index) const {

我对以下代码片段有问题

    string const& symbol::at(int index) const {
    assert(index<symbol_data.vector::size());
    return symbol_data.vector::at(index);
    }

有人知道如何让STL向量与常量引用一起正常工作吗?

在我写这篇文章时,您的代码:

string const& symbol::at(int index) const {
    assert(index<symbol_data.vector::size());
    return symbol_data.vector::at(index);
    }
而不是符号\数据。向量::只写符号\数据


干杯&hth.,

当我写这篇文章时,您的代码:

string const& symbol::at(int index) const {
    assert(index<symbol_data.vector::size());
    return symbol_data.vector::at(index);
    }
而不是符号\数据。向量::只写符号\数据


干杯,我不能重现你的问题;以下内容在VS2010 express上编译并运行良好

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

class symbol 
{
  std::vector<std::string> symbol_data;

public:
  symbol()
  {
    symbol_data.push_back( "Str1" );
    symbol_data.push_back( "Str2" );
  }

  std::string const& at( int index ) const 
  {
    assert( index < symbol_data.vector::size() );

    return symbol_data.vector::at( index );
  }
};

int main()
{
  symbol s;

  std::cout << s.at( 0 ) << std::endl;
  std::cin.get();

  return 0;
}

我无法重现你的问题;以下内容在VS2010 express上编译并运行良好

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

class symbol 
{
  std::vector<std::string> symbol_data;

public:
  symbol()
  {
    symbol_data.push_back( "Str1" );
    symbol_data.push_back( "Str2" );
  }

  std::string const& at( int index ) const 
  {
    assert( index < symbol_data.vector::size() );

    return symbol_data.vector::at( index );
  }
};

int main()
{
  symbol s;

  std::cout << s.at( 0 ) << std::endl;
  std::cin.get();

  return 0;
}

这就是我的想法,但是我的lappy上的所有编译器都接受symbol\u data.vector::atindex@詹姆斯:嗯。你能发布一个完整的例子吗?int main {STD::vector:vector::开始;} Clang 3.0/133044,Visual C++ 2010 SP1,和G+4.5.1都接受了,包括,自然。我很惊讶!这就达到了目的,并使代码得以编译。你能解释一下这背后的原因吗?因为我在自定义类中重新使用::at和::size,所以我不需要vector::进行范围解析吗?哦。它还与Comeau Online一起编译。其实这并不奇怪,因为在vector类模板定义和它的成员函数定义中,您可以引用vector。我认为名称查找是相同的。但这使得OP的问题更加神秘。我想我们必须等待更完整的代码…这就是我的想法,但是我的lappy上的所有编译器都接受symbol_data.vector::atindex@詹姆斯:嗯。你能发布一个完整的例子吗?int main {STD::vector:vector::开始;} Clang 3.0/133044,Visual C++ 2010 SP1,和G+4.5.1都接受了,包括,自然。我很惊讶!这就达到了目的,并使代码得以编译。你能解释一下这背后的原因吗?因为我在自定义类中重新使用::at和::size,所以我不需要vector::进行范围解析吗?哦。它还与Comeau Online一起编译。其实这并不奇怪,因为在vector类模板定义和它的成员函数定义中,您可以引用vector。我认为名称查找是相同的。但这使得OP的问题更加神秘。我想我们必须等待更完整的代码…你能发布一个可以独立编译的完整示例吗?最好是一个尽可能小的例子你能发布一个可以独立编译的完整例子吗?最好是尽可能小的一个例子,这或多或少是我的完整代码的样子。删除vector::后,它可以很好地编译,但不会像您编写的那样编译。是的,这或多或少就是我的完整代码的样子。删除vector::后,它可以很好地编译,但不会按照您编写的方式编译。