setw not working?

It may be a really stupid question but it confuses me so much....

so following is a friend function of my class, R.name returns a string


and it just doesn't work, I replaced R.name with any other string it works perfectly fine and I have no idea why ...:S


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ostream& operator<<(ostream& outs,const Resistor& R)
{
    outs << ' ';
    outs << setw(MAX_RESISTOR_NAME_LEN) <<left << R.name << ' ';
    outs.width(7);
    outs.setf(ios::right);
    outs << R.getResistance() << " Ohms " <<  R.endpointNodeIDs[0] 
            << " -> " << R.endpointNodeIDs[1] << endl;
    outs.unsetf(ios::right);
    return outs;


class Resistor
{
private:
   double resistance; // resistance (in Ohms)
   string name; // C++ string holding the label
   int endpointNodeIDs[2]; // IDs of nodes it attaches to
   Resistor *next;
}


}
Last edited on
You need to use the #include<iomanip> library in your code.
I did ... I did it in my head file...
help....

if I write

string a = "abcde"
outs << setw(MAX_RESISTOR_NAME_LEN) <<left << a << ' ';


the space is set to MAX_RESISTOR_NAME_LEN, but just with R.name it has no effect...whyyyy
The code fragment you posted doesn't make much sense. It looks like you are declaring the Resistor class within the operator<<() function.

Rather, the friend function should be declared within the Resistor class,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Resistor
{
private:
   double resistance;      // resistance (in Ohms)
   string name;            // C++ string holding the label
   int endpointNodeIDs[2]; // IDs of nodes it attaches to
   Resistor *next;

public:
    Resistor::Resistor() { }
    double getResistance() const { return resistance; }

    friend ostream& operator<<(ostream& outs,const Resistor& R);
};


Last edited on
Topic archived. No new replies allowed.