迭代器模式 - 错误C2679:二进制'< :无操作符,找到一个右侧的操作数类型'std :: string' [英] iterator pattern - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'
问题描述
我想使用迭代器模式迭代和打印,但我得到一个错误
I am trying to iterate and print using the iterator pattern but i get an erorr
这里是错误:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1> could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>> (std::basic_ostream<_Elem,_Traits> &,const char *)'
错误发生在
std :: cout<< menuItem-> getName();
here is where the error originates at std::cout << menuItem->getName();
#ifndef _ROBOT1_
#define _ROBOT1_
namespace guitars {
namespace Composite {
namespace InventoryParts {
using namespace std;
#include <iostream>
//#include <string>
class Robot1 {
Menu* _partsMenu;
private:
public: Robot1( Menu* parts ) : _partsMenu( parts ) { assert( parts );
}
public: void printMenu() {
Iterator<MenuItem>* partsIterator = _partsMenu->createIterator();
std::cout << "Parts List" << std::endl;
printMenu( partsIterator );
}
private: void printMenu( Iterator<MenuItem>* iterator ) { assert( iterator );
while( iterator->hasNext() ) {
MenuItem* menuItem = dynamic_cast< MenuItem* >( iterator->next() );
std::cout << menuItem->getName();
std::cout << menuItem->getPrice() << " -- ";
std::cout << menuItem->getDescription() << std::endl;
}
}
};
}
}
}
包含更多文件包含在其他地方
i will include more files incase its somewhere else
#ifndef _ELECTRIC_MENU_ITERATOR_
#define _ELECTRIC_MENU_ITERATOR_
#include "Iterator.h"
namespace guitars {
namespace Composite {
namespace InventoryParts {
class ElectricMenuIterator : public Iterator<MenuItem> {
private: mutable std::vector< MenuItem* > _items;
private: mutable MenuItem* _position;
public: explicit ElectricMenuIterator( std::vector< MenuItem* > items ) :
_items( items ) {
_position = *items.begin();
}
public: MenuItem* next() const {
return _position;
}
public: bool hasNext() const {
for( std::vector< MenuItem* >::iterator iterator = _items.begin(); iterator != _items.end(); iterator++ ) {
if( *iterator == _position ) {
if( ++iterator != _items.end() ) {
_position = *iterator;
return true;
}
else
return false;
}
}
return false;
}
};
}
}
}
#endif
迭代器
#ifndef _ITERATOR_
#define _ITERATOR_
namespace guitars {
namespace Composite {
namespace InventoryParts {
template <class T>
class Iterator
{
public:
virtual bool hasNext() const = 0;
virtual T* next() const = 0;
virtual ~Iterator() = 0 {
}
};
}
}
}
#endif
#ifndef _MENU_
#define _MENU_
#include "MenuComponent.h"
#include "InventoryItem.h"
#include "Iterator.h"
#include <assert.h>
#include <vector>
#include "MenuItem.h"
namespace guitars {
namespace Composite {
namespace InventoryParts {
class Menu : public MenuComponent {
private:
public:
virtual Iterator<MenuItem>* createIterator() const = 0;
virtual ~Menu() = 0 {
}
};
}
}
}
电吉他菜单
#ifndef _ELECTRIC_MENU_
#define _ELECTRIC_MENU_
#include "Menu.h"
#include "MenuItem.h"
#include "ElectricMenuIterator.h"
namespace guitars {
namespace Composite {
namespace InventoryParts {
class ElectricMenu : public Menu {
private:
std::vector< MenuItem* > _menuItems;
public: ElectricMenu() {
addItem( "Electric Guitar","comes with an assortment of goodies",542.99);
//addItem( "Regular acoustic","standard style",false,245.99);
}
public: void addItem( std::string name, std::string description, double price ) {
MenuItem* menuItem = new MenuItem( name, description, price );
_menuItems.push_back( menuItem );
}
public: std::vector< MenuItem* > getMenuItems() const {
return _menuItems;
}
public: Iterator<MenuItem>* createIterator() const {
return dynamic_cast<Iterator<MenuItem>* > ( new ElectricMenuIterator( _menuItems) );
}
};
}
}
}
#endif
请原谅格式
推荐答案
//#include <string>
您不包括< string>
头。您已注释掉了include指令。
You aren't including the <string>
header. You've commented out the include directive.
在Visual C ++标准库实现中, std :: string
当您包括< iostream>
,但是运算符<<
重载时允许插入<$只有包括实际的< c $ c> std :: ostream
才会包含
In the Visual C++ Standard Library implementation, std::string
can be used when you include <iostream>
, but the operator<<
overload that allows insertion of an std::string
into an std::ostream
is only included if you include the actual <string>
header.
如果您希望代码可移植,您必须包括< string>
才能使用 std :: string
;其中标准库头文件包含在其他头文件中是实现定义的。
If you want your code to be portable, you must include <string>
to use std::string
; which Standard Library headers are included by other headers is implementation-defined.
这篇关于迭代器模式 - 错误C2679:二进制'< :无操作符,找到一个右侧的操作数类型'std :: string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!