迭代器模式 - 错误C2679:二进制'<<' :没有找到哪个运算符采用'std :: string'类型的右手操作数

时间:2011-02-12 00:21:47

标签: c++ iterator

我正在尝试使用迭代器模式进行迭代和打印,但我得到了一个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&lt;&lt; menuItem-&GT;的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

请原谅我的格式

2 个答案:

答案 0 :(得分:9)

//#include <string>

您未加入<string>标题。你已经注释掉了include指令。

在Visual C ++标准库实现中,std::string可以包含<iostream>,但operator<<重载允许std::string插入std::ostream仅当您包含实际的<string>标头时才会包含1}}。

如果您希望代码可移植,则必须包含<string>才能使用std::string;其他标头包含哪些标准库标头是实现定义的。

答案 1 :(得分:0)

这是一个猜测,但我会看看

while( iterator->hasNext() ) {
    MenuItem* menuItem = dynamic_cast< MenuItem* >( iterator->next() );
    std::cout << menuItem->getName();
    std::cout << menuItem->getPrice() << " -- ";
    std::cout << menuItem->getDescription() << std::endl;
}

尝试将menuItem-&gt; getName()和menuItem-&gt; getDescription更改为charater数组