开发者

iterator pattern - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' [duplicate]

This question already has an answer here: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) (1 answer) Closed 5 years ago.

I am trying to iterate and print using the iterator pattern but i get an erorr

here is the error:

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 *)'

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

iterator

#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

menu

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

};

} 
} 
} 

and the electric guitar menu

#ifndef _ELECTRIC_MENU_
#define _ELECTRIC_MENU_
#include "Menu.h"
#include "Men开发者_如何学编程uItem.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

please forgive me for the formatting


//#include <string>

You aren't including the <string> header. You've commented out the include directive.

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.

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.


This is a guess, but I would look at

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

try changing menuItem->getName() and menuItem->getDescription to charater arrays

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜