Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,116 people online right now. Registration is fast and FREE... Join Now!




Class Method - Print 2D Vector

 
Reply to this topicStart new topic

Class Method - Print 2D Vector

janotte
23 Oct, 2006 - 01:33 AM
Post #1

D.I.C Head
**

Joined: 28 Sep, 2006
Posts: 242



Thanked: 21 times
My Contributions
I am really confused. I believe that the code below is a set of fairly simple conversion from examples of slightly different usages in my reference book but it will not compile and I have run out of ideas. I have tried a few different versions and got different errors but this is fairly typical code and error.

CODE


//  BlobGrid.h
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

typedef vector<char> GridRow;
typedef vector<GridRow> Grid;

class BlobGrid
{
    public:
    BlobGrid::BlobGrid();
    
    void print(ostream& out, const Grid& myGrid);

    inline Grid getMyGrid();
    
    private:
        GridRow myGridRow;
        Grid myGrid;
};

//-----Default-value constructor----
inline BlobGrid::BlobGrid()
{
    // default 2x2 grid filled with +s
    Grid myGrid(2, GridRow(2, '+'));
}

//------Get myGrid-----
inline Grid BlobGrid::getMyGrid()
{
    return myGrid;
}

//------Ouput Method---------
void BlobGrid::print(ostream& out, const Grid& myGrid)
{
    for (int r=0; r < myGrid.size(); r++)
    {
        for (int c=0; c < myGrid[r].size(); c++)
            out << myGrid[r][c] << '/t';
        out << endl;
    }
}

//---------BlobGrid ostream output-------
inline ostream& operator<<(ostream& out, const BlobGrid& theGrid)
{
    print(cout, theGrid);
    return out;
}


The compiler gives me this warning (fine who cares - or am I missing something critical?)
* warning: multi-character character constant
for this line
out << myGrid[r][c] << '/t';

and, the bad bit
the compiler throws this error
* error: 'print' was not declared in this scope
for this line
print(cout, theGrid);

I am sure it is me being as stupid as stupid but I have read everything I can find and nothing helps.

Any hints at all most welcome?
User is online!Profile CardPM
+Quote Post

gregoryH
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 02:52 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(janotte @ 23 Oct, 2006 - 02:33 AM) *

I am really confused. I believe that the code below is a set of fairly simple conversion from examples of slightly different usages in my reference book but it will not compile and I have run out of ideas. I have tried a few different versions and got different errors but this is fairly typical code and error.

,snip>
print(cout, theGrid);

I am sure it is me being as stupid as stupid but I have read everything I can find and nothing helps.

Any hints at all most welcome?


I think the problem may be here..... ostream & out
inline ostream& operator<<(ostream& out, const BlobGrid& theGrid)

becomes cout here print(cout, theGrid);
User is offlineProfile CardPM
+Quote Post

janotte
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 03:18 AM
Post #3

D.I.C Head
**

Joined: 28 Sep, 2006
Posts: 242



Thanked: 21 times
My Contributions
QUOTE


I think the problem may be here..... ostream & out
inline ostream& operator<<(ostream& out, const BlobGrid& theGrid)

becomes cout here print(cout, theGrid);


Hi gregoryH,
Thanks for taking the time to answer.
Sadly that's not it.
Changed that line to print(out, theGrid); but no change in the warning or error produced.
It's driving me slightly mad!!
Cheers
jan
User is online!Profile CardPM
+Quote Post

gregoryH
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 03:20 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(janotte @ 23 Oct, 2006 - 04:18 AM) *

QUOTE


I think the problem may be here..... ostream & out
inline ostream& operator<<(ostream& out, const BlobGrid& theGrid)

becomes cout here print(cout, theGrid);


Hi gregoryH,
Thanks for taking the time to answer.
Sadly that's not it.
Changed that line to print(out, theGrid); but no change in the warning or error produced.
It's driving me slightly mad!!
Cheers
jan

post the new code please.. i'll put that into a compiler smile.gif
User is offlineProfile CardPM
+Quote Post

janotte
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 03:30 AM
Post #5

D.I.C Head
**

Joined: 28 Sep, 2006
Posts: 242



Thanked: 21 times
My Contributions
Thank you.
Here's the new code.

CODE

//  BlobGrid.h
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

typedef vector<char> GridRow;
typedef vector<GridRow> Grid;

class BlobGrid
{
    public:
    BlobGrid::BlobGrid();
    
    void print(ostream& out, const Grid& myGrid);

    inline Grid getMyGrid();
    
    private:
        GridRow myGridRow;
        Grid myGrid;
};

//-----Default-value constructor----
inline BlobGrid::BlobGrid()
{
    // default 2x2 grid filled with +s
    Grid myGrid(2, GridRow(2, '+'));
}

//------Get myGrid-----
inline Grid BlobGrid::getMyGrid()
{
    return myGrid;
}

//------Ouput Method---------
void BlobGrid::print(ostream& out, const Grid& myGrid)
{
    for (int r=0; r < myGrid.size(); r++)
    {
        for (int c=0; c < myGrid[r].size(); c++)
            out << myGrid[r][c] << '/t';
        out << endl;
    }
}

//---------BlobGrid ostream output-------
inline ostream& operator<<(ostream& out, const BlobGrid& theGrid)
{
    print(out, theGrid);
    return out;
}


User is online!Profile CardPM
+Quote Post

gregoryH
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 03:53 AM
Post #6

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
The problem is elementary - the compiler complained that '/t' is too wide... it should be '\t'

CODE

//------Ouput Method---------
void BlobGrid::print(ostream& out, const Grid& myGrid)
{
    for (int r=0; r < myGrid.size(); r++)
    {
        for (int c=0; c < myGrid[r].size(); c++)
            out << myGrid[r][c] << '/t';
        out << endl;
    }
}


I have reason to believe that this should be a member or a friend of the class to work correctly.

However, if you make something a class member or friend, you don't normally need to create a special "print" function. All the print function can be "chained" with the correct use of operator overloading
CODE

//---------BlobGrid ostream output-------
inline ostream& operator<<(ostream& out, const BlobGrid& theGrid)
{
    print(out, theGrid);
    return out;
}



This post has been edited by gregoryH: 23 Oct, 2006 - 04:03 AM
User is offlineProfile CardPM
+Quote Post

janotte
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 04:15 AM
Post #7

D.I.C Head
**

Joined: 28 Sep, 2006
Posts: 242



Thanked: 21 times
My Contributions
Hi gregoryH,
omg all this time and I couldn't see the difference between /t and \t. Too silly for words.

I don't quite get the other point just now but I think that's a lot to do with too long staring at the screen. (see above \ vs / stupidity <haha>)

I definitely want the print method to be a member of the class (I don't really 'get' friends but that's another subject to read up on). That's because that's what I understood my text to be telling me to do, but, as you identify, the rotten thing doesn't seem to be 'joining' the class so I must be missing something basic. Perhaps I'm on the wrong road there entirely.

I was feeling quite good with C++ until the last couple of weeks and now I feel I am a complete dunce. Very disappointing.

Thanks heaps.
I don't know (or at least understand) the answer yet but I suspect you have given me some useful paths to follow.
Cheers
jan
User is online!Profile CardPM
+Quote Post

gregoryH
RE: Class Method - Print 2D Vector
23 Oct, 2006 - 04:22 AM
Post #8

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(janotte @ 23 Oct, 2006 - 05:15 AM) *

I definitely want the print method to be a member of the class (I don't really 'get' friends but that's another subject to read up on). That's because that's what I understood my text to be telling me to do, but, as you identify, the rotten thing doesn't seem to be 'joining' the class so I must be missing something basic. Perhaps I'm on the wrong road there entirely.

I was feeling quite good with C++ until the last couple of weeks and now I feel I am a complete dunce. Very disappointing.


Jan,

Don't worry, in the early days I didn't get 'friend' either. (I still don't the show Freinds either LOL).

Basically, operator overloading can be a little painful, one of the better explanations I have read is Bruce Eckel's Thinking in C++ Vol 1, you can download a free e-book here

regards


Greg
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:09AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month