gpt4 book ai didi

c++ - 迭代器模式 - 错误 C2679 : binary '<<' : no operator found which takes a right-hand operand of type 'std::string'

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:43 26 4
gpt4 key购买 nike

我正在尝试使用迭代器模式进行迭代和打印,但出现错误

这里是错误:

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();

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



};

}
}
}

我将包含更多文件以防它在其他地方

#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

请原谅我的格式

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com