http://s8.postimg.org/zbfc0h92t/New_Bitmap_Image.png

'contents' can't be initialized whatever I did, thus I can't declare the type in this class

class Window_mgr {
public:
    // location ID for each screen on the window
    typedef std::vector<Screen>::size_type ScreenIndex;
    // reset the Screen at the given position to all blanks
    void clear(ScreenIndex);
private:
    std::vector<Screen> screens{ Screen (' ', 24, 80) };
};

This is the constructor

Screen::Screen(pos ht, pos wd)
{
    contents(ht*wd, ' ');
    height = ht;
    width = wd;
    cursor = 0;
}

Screen::Screen(char c, pos ht, pos wd)  
{
    contents(ht*wd, c);
    height = ht;
    width = wd;
    cursor = 0;
}

this is the class

class Screen {
public:
    friend class Window_mgr;
    void some_member() const;
    typedef std::string::size_type pos;

    Screen(pos ht, pos wd);
    Screen(char c, pos ht, pos wd);

    Screen set(char);
    Screen set(char, pos, pos);

    Screen display(std::ostream &os)
                    { do_display(os); return *this; }
    const Screen &display(std::ostream &os) const
                    { do_display(os); return *this; }

    char get() const { return contents[cursor]; }
    char get(pos ht, pos wd) const; 
    Screen move(pos r, pos c);

private:
    pos cursor;
    pos height, width;
    std::string contents;

    mutable size_t access_ctr;
    void do_display(std::ostream &os) const
        { os << contents; }
};

Recommended Answers

All 9 Replies

What compiler and version are you using?

mingW, version? seems like from 26/04/2012.

The version of gcc matters here because you may not have a version that supports member initialization from C++11. Also, are you using the -std=c++11 or -std=c++0x when compiling?

it seems to me that to use the (size_t,char) string constructor you should probably be constructing a new string. Something like this should work:

Screen::Screen(pos ht, pos wd)
{
    contents = string(ht*wd, ' ');
    height = ht;
    width = wd;
    cursor = 0;
}
Screen::Screen(char c, pos ht, pos wd)
{
    contents = string(ht*wd, c);
    height = ht;
    width = wd;
    cursor = 0;
}

Another potential problem I see, is that you don't have a default constructor:

public:
    Screen();

 Screen::Screen()
{
    contents = "";
    height = 0;
    width = 0;
    cursor = 0;
}           

You should just use the initialization list instead:

Screen::Screen(pos ht, pos wd) :
  cursor(0), height(ht), width(wd),
  contents(ht*wd, ' ')
{ }

Screen::Screen(char c, pos ht, pos wd) :
  cursor(0), height(ht), width(wd),
  contents(ht*wd, c) 
{ }

// and default constructor (not required, but useful in general):
Screen::Screen() :
  cursor(0), height(0), width(0),
  contents() 
{ }

The initialization list is basically just a list of constructor calls for each data member. It is preferrable to put them in the same order as they appear in the class declaration, since that is the actual order in which they are constructed (and most compilers will warn you about that if you don't put them in the same order). Unless you have some complicated code to run in the constructor, you should prefer initializing the data members in the initialization list instead of the body of the constructor.

deceptikon:
I already put an image up there in my thread, showing how I compiled and error produced.

others:
I tried the given method but still, I can't get away from this error:
http://s7.postimg.org/o7zebeyxn/New_Bitmap_Image.png

I thought the problem would be the constructor but it seems I probably wrong, so what is it?

Actually one of your problems was the constructor. Now you can look at the other one.

It looks to me that you might have things being accessed in the wrong order. The error message seems to indicate that Window_mgr can't find Screen. If it's declared after Window_mgr in the same file you could get this error. I would imagine that including the Window_mgr header before the Screen header would also produce this error.

nope, tried this one and the same error occured

#ifndef SCREEN
#define SCREEN

#include <iostream>
#include <string>
#include <vector>

class Screen {
public:
    friend class Window_mgr;
    void some_member() const;
    typedef std::string::size_type pos;

    Screen(pos ht, pos wd):
    cursor(0), height(ht), width(wd), contents(ht*wd, ' ') {
    }

    Screen(char c, pos ht, pos wd):
    cursor(0), height(ht), width(wd), contents(ht*wd, c) {
    }

    Screen():
    cursor(0), height(0), width(0), contents() { 
    }
    // blablabla

private:
    // blablabla
};


class Window_mgr {
public:
    // location ID for each screen on the window
    typedef std::vector<Screen>::size_type ScreenIndex;
    // reset the Screen at the given position to all blanks
    void clear(ScreenIndex);
private:
    std::vector<Screen> screens{ Screen (' ', 24, 80) };
};


#endif

When I put your code into VS2012 I get a red flag here:

std::vector<Screen> screens{ Screen (' ', 24, 80) };

It looks like you're trying to initialize a private member, which isn't allowed.

See if something like this works better:

class Window_mgr {
public:
    // location ID for each screen on the window
    typedef std::vector<Screen>::size_type ScreenIndex;
    // reset the Screen at the given position to all blanks
    void clear(ScreenIndex);
    Window_mgr()
    {
        screens.push_back(Screen(' ', 24, 80));
    }
private:
    std::vector<Screen> screens;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.