problem with const reference return and STL vectors
I am having issues with the following snippet of code
string const& symbol::at(int index) const {
assert(index<symbol_data.vector::size());
return symbol_data.vector::at(index);
}
Here, symbol_data is a private member of the class and is a vector
::at is a member function in the symbol class that I have defined.
When I try compiling this code, I get the following error messsage:
error: ‘template<class _Tp, class _Alloc> class std::vector’ used without template parameters
However, there is no error if I change the function prototype to
string symbol::at(int index) {...}
Does anybody know how I can get STL vectors to work properly 开发者_Go百科with const references?
Your code as I'm writing this:
string const& symbol::at(int index) const {
assert(index<symbol_data.vector::size());
return symbol_data.vector::at(index);
}
Instead of symbol_data.vector::
write just symbol_data.
.
Cheers & hth.,
I can't reproduce your problem; the following compiles and works fine on 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;
}
精彩评论