Thread: Including header in order

  1. #16
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    :'( .. I didnt copied and pasted any data. I am a beginner , thats why my code is like this!! BTW, thanks, Window_mgr uses Screen, So there is no way of making clear() as friend;

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tamim Ad Dari View Post
    So there is no way of making clear() as friend;
    Yes there is, as you have already been advised. The fact you have chosen to disregard the advice in this thread does not mean that a solution does not exist.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Tamim Ad Dari View Post
    how to copy paste errors in code blocks?? When I right click on the error button theres no options such as copy..
    Make sure the logs are visible; select the "Build Log"
    [F2 toggles the logs area]
    Right click on the build log; use "select all".
    Right click again and use "copy"

    Code:
    -------------- Clean: Debug in cpptest (compiler: GNU GCC Compiler)---------------
    
    Cleaned "cpptest - Debug"
    
    -------------- Build: Debug in cpptest (compiler: GNU GCC Compiler)---------------
    
    mingw32-g++.exe -std=c++11 -Wall -fexceptions  -g  -Wmissing-include-dirs    -c V:\SourceCode\Projects\cpptest\main.cpp -o obj\Debug\main.o
    V:\SourceCode\Projects\cpptest\main.cpp: In function 'int main()':
    V:\SourceCode\Projects\cpptest\main.cpp:12:13: warning: unused variable 'ht' [-Wunused-variable]
    V:\SourceCode\Projects\cpptest\main.cpp:12:19: warning: unused variable 'wd' [-Wunused-variable]
    V:\SourceCode\Projects\cpptest\main.cpp:15:6: warning: variable 'c' set but not used [-Wunused-but-set-variable]
    mingw32-g++.exe -std=c++11 -Wall -fexceptions  -g  -Wmissing-include-dirs    -c V:\SourceCode\Projects\cpptest\Screen.cpp -o obj\Debug\Screen.o
    mingw32-g++.exe  -o bin\Debug\cpptest.exe obj\Debug\main.o obj\Debug\Screen.o    
    Output size is 1.01 MB
    Process terminated with status 0 (0 minutes, 2 seconds)
    0 errors, 3 warnings (0 minutes, 2 seconds)
    Tim S.
    Last edited by stahta01; 03-31-2013 at 06:04 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    #include "Window_mgr.h"
    
    using namespace std;
    
    int main()
    {
    
    Screen::pos ht=24,wd=10;
    Screen scr(24,10,' ');
    Screen *p=&scr;
    char c=scr.get();
    c=p->get();
    
    }
    Window_mgr.h
    Code:
    #ifndef WINDOW_MGR_H_INCLUDED
    #define WINDOW_MGR_H_INCLUDED
    
    #include <iostream>
    #include <vector>
    
    #include "Screen.h"
    
    struct Window_mgr{
    
    public:
        using ScreenIndex=std::vector<Screen>::size_type;
        void clear(ScreenIndex);
        ScreenIndex addScreen(const Screen&);
    private:
        std::vector<Screen> screens{Screen(24,80,' ')};
    
    
    };
    
    void Window_mgr::clear(ScreenIndex i)
    {
    Screen &s=screens[i];
    s.contents=std::string(s.width*s.height,' ');
    }
    Window_mgr::ScreenIndex Window_mgr::addScreen(const Screen &s)
    {
        screens.push_back(s);
        return screens.size()-1;
    
    
    }
    #endif // WINDOW_MGR_H_INCLUDED
    Screen.h
    Code:
    #ifndef SCREEN_H_INCLUDED
    #define SCREEN_H_INCLUDED
    
    #include <iostream>
    
    class Screen{
        friend std::ostream& storeOn(std::ostream&,Screen&);
    friend class Window_mgr;
    friend void showscreen(const Screen&);
    friend std::istream &read(std::istream&,Screen&);
    public:
       typedef std::string::size_type pos;
       Screen()=default;
       Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c){}
       char get() const {return contents[cursor];}
       inline char get(pos ht,pos wd) const;
       Screen &move(pos r,pos c);
       void some_member() const;
       Screen &set(char);
       Screen &set(pos,pos,char);
       pos size() const;
    
    private:
        mutable size_t access_ctr;
        pos cursor=0;
        pos height=0,width=0;
        std::string contents;
    };
    
    #endif // SCREEN_H_INCLUDED
    Screen.cpp
    Code:
    #include "Screen.h"
    
    using std::cout;
    using std::endl;
    using std::string;
    
    Screen::pos Screen::size() const
    {
        return height*width;
    }
    inline
    Screen &Screen::move(pos r,pos c)
    {
        pos row=r*width;
        cursor =row+c;
        return *this;
    }
    char Screen::get(pos r,pos c) const
    {
        pos row=width*r;
        return contents[row+c];
    }
    void Screen::some_member() const
    {
        ++access_ctr;
    }
    void showscreen(const Screen &in)
    {
        for(unsigned i=0;i<in.height*in.width;++i)
    {
        if(i&&i%in.width==0) cout<<endl;
        cout<<in.contents[i];
    }
    cout<<endl;
    }
    std::istream &read(std::istream &is,Screen &in)
    {
        cout<<"Enter the height,width and character of the screen seperated by space "<<endl;
        is>>in.height>>in.width;
        char c=getwchar();
        string s(in.height*in.width,c);
        in.contents=s;
        return is;
    }
    inline Screen &Screen::set(char c)
    {
        contents[cursor]=c;
        return *this;
    }
    inline Screen &Screen::set(pos r,pos col,char c)
    {
        contents[r*width+col]=c;
        return *this;
    }
    The above code compiled for me; I just did all the suggestions in this thread and know how to understand most of the g++ error messages.
    If you really wrote this code you know more C++ than I do; but, I know what compiler I am using and understand the tools I am using much better than you do. And, I can follow advise.

    I did not even know the below line was possible in C++.
    Code:
    using ScreenIndex=std::vector<Screen>::size_type;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    I did not even know the below line was possible in C++.
    Code:
    using ScreenIndex=std::vector<Screen>::size_type;
    It's a new feature ratified in C++-11 (the 2011 C++ standard), and was not possible in previous standards.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including header
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2013, 02:27 AM
  2. Header Files / Including
    By FlamingAhura in forum C Programming
    Replies: 2
    Last Post: 11-03-2010, 09:03 PM
  3. including header file
    By Delia in forum C Programming
    Replies: 1
    Last Post: 03-20-2010, 04:52 PM
  4. Including my own header files
    By bushymark in forum C Programming
    Replies: 17
    Last Post: 11-03-2009, 09:09 PM
  5. Including header files
    By Emeighty in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2008, 03:02 PM