Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help with compile errors please

10 views
Skip to first unread message

nvangogh

unread,
Apr 12, 2013, 12:08:08 PM4/12/13
to
I have his code - Screen.h , but can't see why it won't compile

#ifndef SCREEN_H
#define SCREEN_H
#include <iostream>
#include <string>
#endif


class Screen{
Screen():screen_size defined_screen_size.line_num(0),
defined_screen_size.column(0){}
Screen(int l, int c):screen_size
defined_screen_size.line_num(l),defined_screen_size.column(c){}

// view settings
std::ostream& view_settings(std::ostream &os, const Screen &asc)
{
os << asc.defined_screen_size.line_num << " x " <<
asc.defined_screen_size.column;
return os;
}

private:
// data members
struct screen_size{
int line_num;
int column;
}

screen_size defined_screen_size;

};

---------

So if I try to run it with this stest.cpp:

#include <iostream>
#include "Screen.h"

int main()
{
// program to test Screen class
Screen myscreen(10,20);
std::cout << "A screen object has been created..." << std::endl;
myscreen.view_settings(std::cout,myscreen);
return 0;
}


---------
I get a shed load of errors:
g++ stest.cpp -o screentest
In file included from stest.cpp:2:
Screen.h:26: error: expected �;� before �defined_screen_size�
Screen.h: In constructor �Screen::Screen()�:
Screen.h:9: error: type �Screen::screen_size� is not a direct base of
�Screen�
Screen.h:9: error: expected �(� before �defined_screen_size�
Screen.h:9: error: expected �{� before �defined_screen_size�
Screen.h: In constructor �Screen::Screen(int, int)�:
Screen.h:10: error: type �Screen::screen_size� is not a direct base of
�Screen�
Screen.h:10: error: expected �(� before �defined_screen_size�
Screen.h:10: error: expected �{� before �defined_screen_size�
Screen.h: In member function �std::ostream&
Screen::view_settings(std::ostream&, const Screen&)�:
Screen.h:15: error: �const class Screen� has no member named
�defined_screen_size�
Screen.h:15: error: �const class Screen� has no member named
�defined_screen_size�
Screen.h: In function �int main()�:
Screen.h:10: error: �Screen::Screen(int, int)� is private
stest.cpp:7: error: within this context
Screen.h:13: error: �std::ostream& Screen::view_settings(std::ostream&,
const Screen&)� is private
stest.cpp:9: error: within this context


----------------------
This is strange - The constructor appears to be incorrectly expressed.
But it really is right. Also the message that it is not a direct base is
puzzling.

nvangogh

unread,
Apr 12, 2013, 12:52:12 PM4/12/13
to
i have changed this around and get fewer errors
- the compiler can now see the type before it is used in the constructor:

#ifndef SCREEN_H
#define SCREEN_H
#include <iostream>
#include <string>
#endif


class Screen{
struct screen_size{
int line_num;
int column;
};
Screen():screen_size defined_screen_size.line_num(0),
defined_screen_size.column(0){}
Screen(int l, int c):screen_size
defined_screen_size.line_num(l),defined_screen_size.column(c){}

// view settings
std::ostream& view_settings(std::ostream &os, const Screen &asc)
{
os << asc.defined_screen_size.line_num << " x " <<
asc.defined_screen_size.column;
return os;
}

// data members


screen_size defined_screen_size;

};

The errors now are of main 2 types:
> c++exercises/chapter 7$ g++ stest.cpp -o screentest
> In file included from stest.cpp:2:
> Screen.h: In constructor �Screen::Screen()�:
> Screen.h:13: error: type �Screen::screen_size� is not a direct base of �Screen�
> Screen.h:13: error: expected �(� before �defined_screen_size�
> Screen.h:13: error: expected �{� before �defined_screen_size�
> Screen.h: In constructor �Screen::Screen(int, int)�:
> Screen.h:14: error: type �Screen::screen_size� is not a direct base of �Screen�
> Screen.h:14: error: expected �(� before �defined_screen_size�
> Screen.h:14: error: expected �{� before �defined_screen_size�
> Screen.h: In function �int main()�:
> Screen.h:14: error: �Screen::Screen(int, int)� is private
> stest.cpp:7: error: within this context
> Screen.h:17: error: �std::ostream& Screen::view_settings(std::ostream&, const Screen&)� is private

Geoff

unread,
Apr 12, 2013, 12:52:15 PM4/12/13
to
On Fri, 12 Apr 2013 17:08:08 +0100, nvangogh <nvan...@invalid.net>
wrote:
>Screen.h:26: error: expected �;� before �defined_screen_size�
>Screen.h: In constructor �Screen::Screen()�:
>Screen.h:9: error: type �Screen::screen_size� is not a direct base of
>�Screen�
>Screen.h:9: error: expected �(� before �defined_screen_size�
>Screen.h:9: error: expected �{� before �defined_screen_size�
>Screen.h: In constructor �Screen::Screen(int, int)�:
>Screen.h:10: error: type �Screen::screen_size� is not a direct base of
>�Screen�
>Screen.h:10: error: expected �(� before �defined_screen_size�
>Screen.h:10: error: expected �{� before �defined_screen_size�
>Screen.h: In member function �std::ostream&
>Screen::view_settings(std::ostream&, const Screen&)�:
>Screen.h:15: error: �const class Screen� has no member named
>�defined_screen_size�
>Screen.h:15: error: �const class Screen� has no member named
>�defined_screen_size�
>Screen.h: In function �int main()�:
>Screen.h:10: error: �Screen::Screen(int, int)� is private
>stest.cpp:7: error: within this context
>Screen.h:13: error: �std::ostream& Screen::view_settings(std::ostream&,
>const Screen&)� is private
>stest.cpp:9: error: within this context
>
>
>----------------------
> This is strange - The constructor appears to be incorrectly expressed.
>But it really is right. Also the message that it is not a direct base is
>puzzling.

Where is your definition of defined_screen_size?

nvangogh

unread,
Apr 12, 2013, 12:59:14 PM4/12/13
to
On 12/04/13 17:52, nvangogh wrote:
>
> The errors now are of main 2 types:
>> c++exercises/chapter 7$ g++ stest.cpp -o screentest
>> In file included from stest.cpp:2:
>> Screen.h: In constructor �Screen::Screen()�:
>> Screen.h:13: error: type �Screen::screen_size� is not a direct base of
>> �Screen�

This is a very odd error as I have made screen_size a direct type that
only the class can see, by going out of my way and defining the struct
for it inside and at the start of the class and as can be seen by the
declaration at the end an instance of this type is:

screen_size defined_screen_size;

[type][name]

nvangogh

unread,
Apr 12, 2013, 1:04:43 PM4/12/13
to
On 12/04/13 17:52, Geoff wrote:

>
> Where is your definition of defined_screen_size?

I am assuming you mean declaration? defined_screen_size is a type of
screen_size. Indeed it is the only data member of the class so far. I
have checked and seen that I have included this in previous postings. So
I do not see your point, if you are making one here.

nvangogh

unread,
Apr 12, 2013, 1:31:47 PM4/12/13
to
On 12/04/13 18:04, nvangogh wrote:
I have eliminated one of the big errors by making struct screen_size a
base class:

#ifndef SCREEN_H
#define SCREEN_H
#include <iostream>
#include <string>
#endif

struct screen_size{
int line_num;
int column;
};

class Screen : public screen_size{

Screen():defined_screen_size.line_num(0), defined_screen_size.column(0){}
Screen(int l, int
c):defined_screen_size.line_num(l),defined_screen_size.column(c){}

std::ostream& view_settings(std::ostream &os, const Screen &asc)
{
os << asc.defined_screen_size.line_num << " x " <<
asc.defined_screen_size.column;
return os;
}

// data members
screen_size defined_screen_size;
};


This is the error outputs now:

> Screen.h: In constructor �Screen::Screen()�:
> Screen.h:14: error: expected �(� before �.� token
> Screen.h:14: error: expected �{� before �.� token
> Screen.h: In constructor �Screen::Screen(int, int)�:
> Screen.h:15: error: expected �(� before �.� token
> Screen.h:15: error: expected �{� before �.� token
> Screen.h: In function �int main()�:
> Screen.h:15: error: �Screen::Screen(int, int)� is private
> stest.cpp:7: error: within this context
> Screen.h:18: error: �std::ostream& Screen::view_settings(std::ostream&, const Screen&)� is private
> stest.cpp:9: error: within this context
-----------------------------------

Looks like the way the constructors defined are the main problem now.

Ike Naar

unread,
Apr 12, 2013, 2:34:52 PM4/12/13
to
On 2013-04-12, nvangogh <nvan...@invalid.net> wrote:
> class Screen{
> Screen():screen_size defined_screen_size.line_num(0),
> defined_screen_size.column(0){}
> Screen(int l, int c):screen_size
> defined_screen_size.line_num(l),defined_screen_size.column(c){}

You cannot use a member initializer list like this.
Either initialize defined_screen in the body of the Screen
constructors:

Screen()
{
defined_screen_size.line_num = 0;
defined_screen_size.column = 0;
}
Screen(int l, int c)
{
defined_screen_size.line_num = l;
defined_screen_size.column = c;
}

> private:
> // data members
> struct screen_size{
> int line_num;
> int column;
> }
> screen_size defined_screen_size;
> };

or else write a constructor for screen_size:

struct screen_size
{
int line_num;
int column;
screen_size(int l, int c) : line_num(l), column(c) {}
};

and then write the Screen constructors as:

Screen() : defined_screen_size(0, 0) {}
Screen(int l, int c) : defined_screen_size(l;, c) {}

Ike Naar

unread,
Apr 12, 2013, 2:37:27 PM4/12/13
to
On 2013-04-12, Ike Naar <i...@iceland.freeshell.org> wrote:
> Screen(int l, int c) : defined_screen_size(l;, c) {}

sorry about the spurious ';' here.

Ben Bacarisse

unread,
Apr 12, 2013, 2:43:37 PM4/12/13
to
nvangogh <nvan...@invalid.net> writes:

> On 12/04/13 18:04, nvangogh wrote:
> I have eliminated one of the big errors by making struct screen_size a
> base class:
>
> #ifndef SCREEN_H
> #define SCREEN_H
> #include <iostream>
> #include <string>
> #endif
>
> struct screen_size{
> int line_num;
> int column;
> };

The original posting had a missing ; here. There is no problem with
having this struct defined inside your class -- moving it outside does
not help except that you happened to add the missing semicolon.

> class Screen : public screen_size{

It does not make sense to derive Screen from screen_size *and* include
an object of type screen_size as a member. You are changing things
about to get rid of errors, but it's usually better to keep the code
unchanged until you *understand* the errors. You could have got the
original code (that looks sane to me) to compile if you'd left it alone
until you knew what the compiler was complaining about.

> Screen():defined_screen_size.line_num(0),
> defined_screen_size.column(0){}

You can't use . here. The way to do this is to reply on a constructor
for the member called defined_screen_size.

> Screen(int l, int
> c):defined_screen_size.line_num(l),defined_screen_size.column(c){}
>
> std::ostream& view_settings(std::ostream &os, const Screen &asc)
> {
> os << asc.defined_screen_size.line_num << " x " <<
> asc.defined_screen_size.column;
> return os;
> }
>
> // data members
> screen_size defined_screen_size;
> };
>
>
> This is the error outputs now:
>
>> Screen.h: In constructor ‘Screen::Screen()’:
>> Screen.h:14: error: expected ‘(’ before ‘.’ token
>> Screen.h:14: error: expected ‘{’ before ‘.’ token
>> Screen.h: In constructor ‘Screen::Screen(int, int)’:
>> Screen.h:15: error: expected ‘(’ before ‘.’ token
>> Screen.h:15: error: expected ‘{’ before ‘.’ token

As above.

>> Screen.h: In function ‘int main()’:
>> Screen.h:15: error: ‘Screen::Screen(int, int)’ is private
>> stest.cpp:7: error: within this context
>> Screen.h:18: error: ‘std::ostream& Screen::view_settings(std::ostream&, const Screen&)’ is private
>> stest.cpp:9: error: within this context

These are because you've made everything private (simply by having no
public: part of the class).

> -----------------------------------
>
> Looks like the way the constructors defined are the main problem now.

--
Ben.

Francis Glassborow

unread,
Apr 16, 2013, 7:52:25 PM4/16/13
to
On 12/04/2013 17:08, nvangogh wrote:
> I have his code - Screen.h , but can't see why it won't compile
>
> #ifndef SCREEN_H
> #define SCREEN_H
> #include <iostream>
> #include <string>
> #endif
>
>
> class Screen{
> Screen():screen_size defined_screen_size.line_num(0),
> defined_screen_size.column(0){}
> Screen(int l, int c):screen_size
> defined_screen_size.line_num(l),defined_screen_size.column(c){}
>
> // view settings
> std::ostream& view_settings(std::ostream &os, const Screen &asc)
> {
> os << asc.defined_screen_size.line_num << " x " <<
> asc.defined_screen_size.column;
> return os;
> }
>
> private:
> // data members
> struct screen_size{
> int line_num;
> int column;
> }
>
> screen_size defined_screen_size;
>
> };
>


You have two choices to correct the error in the above definition:

1) add a semi-colon after the closing brace of the struct definition
or
2) remove the second use of 'screen_size'

In simple terms you can write

struct X {
// definition
};
X x;

or

struct X {
// definition
} x ;


The latter is correct but rarely used these days.

Francis


0 new messages