Problem with STL's std::set container parameter to the operator << ()


 
Thread Tools Search this Thread
Top Forums Programming Problem with STL's std::set container parameter to the operator << ()
# 1  
Old 04-11-2012
Problem with STL's std::set container parameter to the operator << ()

Hi,

I have this following code which gives me error when compiling. The problem is happening at the point where I create a const_iterator inside the overloaded insertion operator (i.e) operator << () function. The template argument version of set is not correct I guess. Could anyone please pitch in and make this code working?

Code:
#include <iostream>
#include <set>

using namespace std;

class A {
public:
    int _i;
    A(int i) : _i(i) {}
    bool operator < (A a) const { return _i < a._i; }
};

class compA {
public:
    bool operator()(A a1, A a2) const { return (a1._i > a2._i); }
};

template<class T, class C>
std::ostream& operator << (std::ostream& os, const set<T, C>& s) {
    std::set<T, C>::const_iterator itr = s.begin();

    for (int i = 0; i < s.size(); i++) {
        os << *itr++ << " ";
    }

    return os;
}

int main() {
    set<A, less<A> > s1;
    set<A, compA> s2;

    s1.insert(A(9)); s1.insert(A(3)); s1.insert(A(-5)); s1.insert(A(3));
    s2.insert(A(2)); s2.insert(A(1)); s2.insert(A(7));

    cout << s1;
    cout << s2;
}


Last edited by royalibrahim; 04-16-2012 at 04:11 AM..
# 2  
Old 04-12-2012
g++ tells you exactly what to do.

Code:
$ g++ iter.cpp
iter.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::set<T, C, std::allocator<_CharT> >&) [with T = A, C = std::less<A>]':
iter.cpp:40:   instantiated from here
iter.cpp:21: error: dependent-name 'std::set::const_iterator' is parsed as a non-type, but instantiation yields a type
iter.cpp:21: note: say 'typename std::set::const_iterator' if a type is meant
iter.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::set<T, C, std::allocator<_CharT> >&) [with T = A, C = compA]':
iter.cpp:41:   instantiated from here
iter.cpp:21: error: dependent-name 'std::set::const_iterator' is parsed as a non-type, but instantiation yields a type
iter.cpp:21: note: say 'typename std::set::const_iterator' if a type is meant

$

Code:
template<class T, class C>
std::ostream& operator << (std::ostream& os, const set<T, C>& s)
{
    typename std::set<T,C>::const_iterator itr=s.begin();

    for (int i = 0; i < s.size(); i++) {
  //      os << *itr++ << " ";
    }

    return os;
}


Last edited by Corona688; 04-12-2012 at 01:48 PM..
# 3  
Old 04-12-2012
I'd also refactor your loop a bit:

Code:
while(itr != s.end())
{
        os << (*itr) << " ";
        itr++;
}

# 4  
Old 04-16-2012
Quote:
Originally Posted by Corona688
g++ tells you exactly what to do.
Code:
template<class T, class C>
std::ostream& operator << (std::ostream& os, const set<T, C>& s)
{
    typename std::set<T,C>::const_iterator itr=s.begin();

    for (int i = 0; i < s.size(); i++) {
  //      os << *itr++ << " ";
    }

    return os;
}

Awesome Corona688!! the code is now compiling Smilie


Quote:
Originally Posted by Corona688
I'd also refactor your loop a bit:
Code:
while(itr != s.end())
{
        os << (*itr) << " ";
        itr++;
}

But this statement, printing the iterator value is giving lot of compilation errors to me. Is that compiling fine for you?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can't set parameter to lynx properly

My intention is to go through list of addresses and call google geocode api for each of them. I am using lynx for this, but somehow I can't supply the parameters to it in a proper way. To show that my parameters are OK I just hardcoded one address in my script and put it in my input file, and... (2 Replies)
Discussion started by: migurus
2 Replies

2. Shell Programming and Scripting

Dot operator and space, Parameter not set

Hi, i have this script setenv.sh if then echo "is empty" fi echo "done" The following is the result when i run the script from command without and with a dot and space operator $ setenv.sh is empty done $ . setenv.sh sh: VAR_1: Parameter not set. $ It's our standard to run... (5 Replies)
Discussion started by: ysrini
5 Replies

3. Programming

how to check the value in stl container during debug

Hi, guys. I am working on a project right now. But when I debug my code, I can't see the values in stl container, e.g vector, map. Can anyone help me, please. I am really frustrated:wall:. I am using code::blocks IDE, by the way. Thanks in advance...... (0 Replies)
Discussion started by: tefino
0 Replies

4. Solaris

Problem creating sol8 container using seperate / /usr and /var ufsdumps

Hello experts New to solaris 8 containers and zones in general and I'm having some problems creating a Solaris 8 container using ufsdumps of /, /usr and /var. I have created the zone which worked fine and a running the following command. zoneadm -z zone_s8 install -v -p -a /root.ufs ... (2 Replies)
Discussion started by: BrokeIt
2 Replies

5. UNIX and Linux Applications

set mysql password with host parameter

hi, linux gurus... i'm trying to write a script in ksh called ResetPass that allows a user to change mysql passwords. the script accepts user, password and host like this: ResetPass <user> <password> <host>. here's the code: ***************************************************** mysql... (1 Reply)
Discussion started by: ankimo
1 Replies

6. Shell Programming and Scripting

reading from within the loop --std i/p problem

i have a teat file having data like one 12/3 two 23/09 three 12/12 now from another script i want to read one line at a time ,cut field one and two in two separate variable ,compare field1 with another variable but outside the loop .If i found a match i want to take from user the value... (2 Replies)
Discussion started by: mobydick
2 Replies

7. Programming

Sun Studio C++ - Getting error in linking std::ostream &std::ostream::operator<<(std:

Hello all Im using CC: Sun C++ 5.6 2004/07/15 and using the -library=stlport4 when linkning im getting The fallowing error : Undefined first referenced symbol in file std::ostream &std::ostream::operator<<(std::ios_base&(*)(std::ios_base&))... (0 Replies)
Discussion started by: umen
0 Replies

8. Shell Programming and Scripting

How to redirect std out and std err to same file

Hi I want both standard output and standard error of my command cmd to go to the same file log.txt. please let me know the best commandline to do this. Thanks (2 Replies)
Discussion started by: 0ktalmagik
2 Replies

9. Shell Programming and Scripting

ksh: dbfFILe: parameter not set

Having the following message returned: FIND REDLOG FILES..... ksh: dbfFILe: parameter not set When I attempt to perform the script below....#!/bin/ksh . $HOME/.profile # Initial Script Prerequisites ORACLE_SID=MDirect ; export ORACLE_SID REDOLOGDIR=$ARCLOGDEST ; export REDOLOGDIR... (2 Replies)
Discussion started by: Cameron
2 Replies

10. UNIX for Dummies Questions & Answers

DEBUG_PROG and opts: parameter not set

I am using a Sun Ultra 30 with 250MG of RAM and 9GIG of hard drive. I connect to the machine via Exceed 6 and have the Oracle8i for Solaris in the cdrom drive. I have read and complied with Oracle's instruction in preparation to installing Oracle. Since I don't have enough space to cp the content... (1 Reply)
Discussion started by: Alexxx14
1 Replies
Login or Register to Ask a Question

Featured Tech Videos