Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
ostream & operator <<(ostream & os, LongNumber & along)
{

    for(std::vector<LongNumber>:: iterator i = (along.container).begin(); i != (along.container).end(); ++i) //this line give me errors
    {
        os << *i;
    }
      
    return os;
}

class LongNumber
{
    private:
        vector<int> number;

};

error: conversion from to non-scalar type iterator requested

error: no match for operator !=


i have no idea what's wrong.
Posted
v2

1 solution

Declaring a container member in a class does not make the class a container, nor does it create a container for the class.

Your LongNumber class does not have a member called container. You have to either write an accessor function:
C++
class LongNumber
{
    private:
        vector<int> number;
	public:
		vector<int>& container();
};

ostream & operator <<(ostream & os, LongNumber & along)
{
    for(std::vector<int>::iterator i = along.container().begin(); i != along.container().end(); ++i) 

or make it a public member:
C++
class LongNumber
{
	public:
        vector<int> number;
};

ostream & operator <<(ostream & os, LongNumber & along)
{
    for(std::vector<int>::iterator i = along.number.begin(); i != along.number.end(); ++i) 

or provide access to the beginning and end iterators:
C++
class LongNumber
{
    private:
        vector<int> number;
	public:
		vector<int>::iterator begin()
		{    
		    return number.begin(); 
		}	
		vector<int>::const_iterator begin() const
		{    
			return number.begin(); 
		}	
		vector<int>::iterator end()
		{    
		    return number.end(); 
		}	
		vector<int>::const_iterator end() const
		{    
			return number.end(); 
		}	
};

ostream & operator <<(ostream & os, LongNumber & along)
{
    for(std::vector<int>::iterator i = along.begin(); i != along.end(); ++i) 

Note that I've changed the type in the loop from std::vector<LongNumber> to std::vector<int> here. The code in the examples above will iterate over the elements of the member container of your along variable.
 
Share this answer
 
Comments
Member 10338805 28-Feb-14 12:23pm    
aren't friend functions supposed to be able to access private class variables without accessors?
Orjan Westin 28-Feb-14 17:14pm    
Yes, they can, but you didn't show that the streaming operator was a friend. If it is a friend, you still need to use the name of the actual member variable, as in the second example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900