我已经在我的自定义类上定义了 vector 的私有继承,并且我想在其上获得一个迭代器。但我收到转换错误。
这是我的解决方案.h
#include <vector>
#include <string>
#ifndef SOLUTIONS_H_
#define SOLUTIONS_H_
class Solutions : private std::vector<std::string>
{
public:
using std::vector<std::string>::begin;
using std::vector<std::string>::end;
using std::vector<std::string>::push_back;
void Show() const;
};
#endif
这是我的解决方案.cpp
#include <iostream>
#include "solutions.h"
using std::cout;
using std::endl;
using std::vector;
using std::string;
void Solutions::Show() const
{
cout << "Solutions : " << endl;
if ( ! vector<string>::empty() )
{
for (vector<string>::iterator it = begin(); it != end(); ++it)
{
cout << *it << endl;
}
}
}
这是我的 test_solutions.cpp
#include "solutions.h"
int main()
{
Solutions resultat;
resultat.Show();
resultat.push_back("10 + 7 = 17");
resultat.push_back("3 / 3 = 1");
resultat.push_back("17 - 1 = 16");
resultat.Show();
resultat.push_back("AA * BB = CC");
resultat.push_back("CC - DD = EE");
resultat.push_back("AA * EE = FF");
resultat.Show();
return 0;
}
这是我在终端上使用的命令(编译器 Ubuntu/Linaro 4.7.3-1ubuntu1):
g++ solutions.h solutions.cpp test_solutions.cpp -o test_solutions
这是编译器给出的错误:
solutions.cpp: In member function ‘void Solutions::Show() const’:
solutions.cpp:12:44: erreur: conversion from ‘std::vector<std::basic_string<char> >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ to non-scalar type ‘std::vector<std::basic_string<char> >::iterator {aka __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ requested
我该如何解决这个问题?
提前致谢。