errors result when creating header file - visual c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • soni29

    errors result when creating header file - visual c++

    hi,
    i have written the following code, still in the learning stage:
    #include<iostre am.h>

    class CBox {

    public:
    // Constructor definition
    CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
    m_Breadth(bv), m_Height(hv)
    {
    cout << endl << "Constructo r called";
    }

    // Default Constructor
    CBox() {
    cout << endl
    << "Default constructor called.";
    m_Length = m_Breadth = m_Height = 1.0;
    }

    // Function to calculate the volume of a box
    double Volume() const {
    return m_Length * m_Breadth * m_Height;
    }

    private:
    double m_Length;
    double m_Breadth;
    double m_Height;
    };


    int main() {
    CBox boxes[5];
    CBox cigar(8.0, 5.0, 1.0);

    cout << endl
    << "Volume of boxes[3] = " << boxes[3].Volume()
    << endl
    << "Volume of cigar = " <<cigar.Volume( );

    cout << endl;
    return 0;
    }


    when i try to break it into a header and source file, 2 separate
    files, i get errors:
    source file:
    #include <iostream.h>
    class CBox;

    int main() {
    CBox boxes[5];
    CBox cigar(8.0, 5.0, 1.0);

    cout << endl
    << "Volume of boxes[3] = " << boxes[3].Volume()
    << endl
    << "Volume of cigar = " <<cigar.Volume( );

    cout << endl;
    return 0;
    }


    CBox.h:
    #include<iostre am.h>

    class CBox {

    public:
    // Constructor definition
    CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
    m_Breadth(bv), m_Height(hv)
    {
    cout << endl << "Constructo r called";
    }

    // Default Constructor
    CBox() {
    cout << endl
    << "Default constructor called.";
    m_Length = m_Breadth = m_Height = 1.0;
    }

    // Function to calculate the volume of a box
    double Volume() const {
    return m_Length * m_Breadth * m_Height;
    }

    private:
    double m_Length;
    double m_Breadth;
    double m_Height;
    };


    \Microsoft Visual Studio .NET\Vc7\includ e\useoldio.h(29 ): warning
    C4995: '_OLD_IOSTREAMS _ARE_DEPRECATED ': name was marked as #pragma
    deprecated
    Test.cpp(5): error C2133: 'boxes' : unknown size
    Test.cpp(5): error C2512: 'CBox' : no appropriate default constructor
    available
    Test.cpp(5): error C2262: 'boxes' : cannot be destroyed
    Test.cpp(6): error C2079: 'cigar' uses undefined class 'CBox'
    Test.cpp(6): error C2078: too many initializers
    Test.cpp(6): warning C4244: 'initializing' : conversion from 'double'
    to 'int', possible loss of data
    Test.cpp(9): error C2036: 'CBox *' : unknown size
    Test.cpp(9): error C2027: use of undefined type 'CBox'
    Test.cpp(9): error C2228: left of '.Volume' must have
    class/struct/union type
    Test.cpp(11): error C2228: left of '.Volume' must have
    class/struct/union type


    is there a way to make it so that i can keep the files apart, if i
    wanted all my classes to be in a header and the source to be separate,
    please note that i wanted to make the constructors and the Volume
    method inline that's why i put the code in the header.

    Thank you.
  • David

    #2
    Re: errors result when creating header file - visual c++

    I'm not sure of the file split, but it appears that your
    "source file" didn't include cbox.h. You don't need the
    "class CBox;" statement in the source file.

    David

    On Sat, 6 Sep 2003 14:17:17 UTC, soni29@hotmail. com (soni29) wrote:
    [color=blue]
    > hi,
    > i have written the following code, still in the learning stage:
    > #include<iostre am.h>
    >
    > class CBox {
    >
    > public:
    > // Constructor definition
    > CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
    > m_Breadth(bv), m_Height(hv)
    > {
    > cout << endl << "Constructo r called";
    > }
    >
    > // Default Constructor
    > CBox() {
    > cout << endl
    > << "Default constructor called.";
    > m_Length = m_Breadth = m_Height = 1.0;
    > }
    >
    > // Function to calculate the volume of a box
    > double Volume() const {
    > return m_Length * m_Breadth * m_Height;
    > }
    >
    > private:
    > double m_Length;
    > double m_Breadth;
    > double m_Height;
    > };
    >
    >
    > int main() {
    > CBox boxes[5];
    > CBox cigar(8.0, 5.0, 1.0);
    >
    > cout << endl
    > << "Volume of boxes[3] = " << boxes[3].Volume()
    > << endl
    > << "Volume of cigar = " <<cigar.Volume( );
    >
    > cout << endl;
    > return 0;
    > }
    >
    >
    > when i try to break it into a header and source file, 2 separate
    > files, i get errors:
    > source file:
    > #include <iostream.h>
    > class CBox;
    >
    > int main() {
    > CBox boxes[5];
    > CBox cigar(8.0, 5.0, 1.0);
    >
    > cout << endl
    > << "Volume of boxes[3] = " << boxes[3].Volume()
    > << endl
    > << "Volume of cigar = " <<cigar.Volume( );
    >
    > cout << endl;
    > return 0;
    > }
    >
    >
    > CBox.h:
    > #include<iostre am.h>
    >
    > class CBox {
    >
    > public:
    > // Constructor definition
    > CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
    > m_Breadth(bv), m_Height(hv)
    > {
    > cout << endl << "Constructo r called";
    > }
    >
    > // Default Constructor
    > CBox() {
    > cout << endl
    > << "Default constructor called.";
    > m_Length = m_Breadth = m_Height = 1.0;
    > }
    >
    > // Function to calculate the volume of a box
    > double Volume() const {
    > return m_Length * m_Breadth * m_Height;
    > }
    >
    > private:
    > double m_Length;
    > double m_Breadth;
    > double m_Height;
    > };
    >
    >
    > \Microsoft Visual Studio .NET\Vc7\includ e\useoldio.h(29 ): warning
    > C4995: '_OLD_IOSTREAMS _ARE_DEPRECATED ': name was marked as #pragma
    > deprecated
    > Test.cpp(5): error C2133: 'boxes' : unknown size
    > Test.cpp(5): error C2512: 'CBox' : no appropriate default constructor
    > available
    > Test.cpp(5): error C2262: 'boxes' : cannot be destroyed
    > Test.cpp(6): error C2079: 'cigar' uses undefined class 'CBox'
    > Test.cpp(6): error C2078: too many initializers
    > Test.cpp(6): warning C4244: 'initializing' : conversion from 'double'
    > to 'int', possible loss of data
    > Test.cpp(9): error C2036: 'CBox *' : unknown size
    > Test.cpp(9): error C2027: use of undefined type 'CBox'
    > Test.cpp(9): error C2228: left of '.Volume' must have
    > class/struct/union type
    > Test.cpp(11): error C2228: left of '.Volume' must have
    > class/struct/union type
    >
    >
    > is there a way to make it so that i can keep the files apart, if i
    > wanted all my classes to be in a header and the source to be separate,
    > please note that i wanted to make the constructors and the Volume
    > method inline that's why i put the code in the header.
    >
    > Thank you.[/color]


    Comment

    • Rob Williscroft

      #3
      Re: errors result when creating header file - visual c++

      soni29 wrote in news:cad7a075.0 309060617.2d593 ac1@posting.goo gle.com:
      [color=blue]
      > when i try to break it into a header and source file, 2 separate
      > files, i get errors:
      > source file:
      > #include <iostream.h>[/color]

      #include <iostream> /* for std::cout */
      #include <ostream> /* for std::endl */
      [color=blue]
      > class CBox;[/color]

      // replace with:

      #include "CBox.h"
      [color=blue]
      >
      > int main() {[/color]

      using std::cout; /* so we can write cout instead of std::cout */
      using std::endl; /* ... */
      [color=blue]
      > CBox boxes[5];
      > CBox cigar(8.0, 5.0, 1.0);
      >
      > cout << endl
      > << "Volume of boxes[3] = " << boxes[3].Volume()
      > << endl
      > << "Volume of cigar = " <<cigar.Volume( );
      >
      > cout << endl;
      > return 0;
      > }
      >
      >
      > CBox.h:
      > #include<iostre am.h>
      >[/color]

      #include <iostream> /* for std::cout */
      #include <ostream> /* for std::endl */

      [color=blue]
      > class CBox {
      >
      > public:
      > // Constructor definition
      > CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv),
      > m_Breadth(bv), m_Height(hv)
      > {
      > cout << endl << "Constructo r called";[/color]

      std::cout << std::endl << "Constructo r called";
      [color=blue]
      > }
      >
      > // Default Constructor
      > CBox() {
      > cout << endl[/color]

      std::cout << std::endl

      [color=blue]
      > << "Default constructor called.";
      > m_Length = m_Breadth = m_Height = 1.0;
      > }
      >
      > // Function to calculate the volume of a box
      > double Volume() const {
      > return m_Length * m_Breadth * m_Height;
      > }
      >
      > private:
      > double m_Length;
      > double m_Breadth;
      > double m_Height;
      > };
      >[/color]

      The main bit you were missin was #include "CBox.h".

      But also note that all the C++ specific parts of the standard
      library use includes without the .h suffix, also the types,
      object's and function's they declare are in namespace std,
      hence all "std::" prefixing I did above.


      HTH

      Rob.
      --

      Comment

      • Kevin Goodsell

        #4
        Re: errors result when creating header file - visual c++

        David wrote:
        [color=blue]
        > I'm not sure of the file split,[/color]
        <snip>

        Please don't top-post. Re-read section 5 of the FAQ for posting
        guidelines. You could also find this rule in pretty much any netiquette
        reference, including RFC 1855.



        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • soni29

          #5
          Re: errors result when creating header file - visual c++

          Thank you.

          Comment

          • John L Fjellstad

            #6
            Re: errors result when creating header file - visual c++

            Rob Williscroft wrote:
            [color=blue]
            > #include <iostream> /* for std::cout */
            > #include <ostream> /* for std::endl */[/color]

            Is that really necessary? According to my "C++ Programming Language: Special
            Edition" p 633: "Manipulato rs taking istream and ostream are presented in
            <istream> and <ostream>, respectively, and also in <iostream>". std::endl
            is an ostream manipulator.
            Also, since std::cout is an std::ostream object, and is defined in
            <iostream>, doesn't it follow that <ostream> has to have been included by
            <iostream>?

            --
            John L. Fjellstad

            A: Top posting!
            Q: What is the most irritating thing on Usenet?

            Comment

            • Rob Williscroft

              #7
              Re: errors result when creating header file - visual c++

              John L Fjellstad wrote in news:bdidjb.r0s .ln@192.168.1.1 :
              [color=blue]
              > Rob Williscroft wrote:
              >[color=green]
              >> #include <iostream> /* for std::cout */
              >> #include <ostream> /* for std::endl */[/color]
              >
              > Is that really necessary? According to my "C++ Programming Language:
              > Special Edition" p 633: "Manipulato rs taking istream and ostream are
              > presented in <istream> and <ostream>, respectively, and also in
              > <iostream>". std::endl is an ostream manipulator.[/color]

              Logic's good so far.
              [color=blue]
              > Also, since std::cout is an std::ostream object, and is defined in
              > <iostream>, doesn't it follow that <ostream> has to have been included
              > by <iostream>?
              >[/color]

              No <iostream>

              Rob.
              --

              Comment

              • Rob Williscroft

                #8
                Re: errors result when creating header file - visual c++

                John L Fjellstad wrote in news:bdidjb.r0s .ln@192.168.1.1 :

                My appologees for the previous post. I clicked send before
                I was finnished.
                [color=blue]
                > Rob Williscroft wrote:
                >[color=green]
                >> #include <iostream> /* for std::cout */
                >> #include <ostream> /* for std::endl */[/color]
                >
                > Is that really necessary? According to my "C++ Programming Language:
                > Special Edition" p 633: "Manipulato rs taking istream and ostream are
                > presented in <istream> and <ostream>, respectively, and also in
                > <iostream>". std::endl is an ostream manipulator.[/color]

                This is what usually happens, but it isn't what the standard says.
                It's what most people (including me - untill I was told otherwise)
                expect to happen.
                [color=blue]
                > Also, since std::cout is an std::ostream object, and is defined in
                > <iostream>, doesn't it follow that <ostream> has to have been included
                > by <iostream>?
                >[/color]

                <iostream> needs to declare the objects cin, cout, cerr etc.
                The simplest way to do that is to:

                #include <istream>
                #include <ostream>

                and then declare:

                namespace std
                {
                extern istream cin;
                extern ostream cout;
                ...
                }

                But it doesen't have to work like that. All it *has* to do is
                include enough of the declaration's of std::basic_istr eam<> and
                std::basic_ostr eam<> and possibly the typedef's std::istream and
                std::ostream, so that it can declare the cin, cout ... objects.

                It could miss out all the non-inline member defenition's.

                It could also miss out defenition's for the constructor's and
                destructor's for the basic_?stream<> 's even if they are inlined
                as cin etc are magicly constructed before main() is entered and
                aren't destroyed.

                Most importantly, It dosent need to include the declaration and/or
                defenition's for std::endl, std::ends, or std::flush. Firstly
                because the standard dosen't require it too and secondly because
                they aren't required for functioning cin, cout ... objects.

                Rob.
                --

                Comment

                • Alf P. Steinbach

                  #9
                  Re: errors result when creating header file - visual c++

                  On 07 Sep 2003 08:46:59 GMT, Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote:
                  [color=blue]
                  >John L Fjellstad wrote in news:bdidjb.r0s .ln@192.168.1.1 :
                  >
                  >My appologees for the previous post. I clicked send before
                  >I was finnished.
                  >[color=green]
                  >> Rob Williscroft wrote:
                  >>[color=darkred]
                  >>> #include <iostream> /* for std::cout */
                  >>> #include <ostream> /* for std::endl */[/color]
                  >>
                  >> Is that really necessary? According to my "C++ Programming Language:
                  >> Special Edition" p 633: "Manipulato rs taking istream and ostream are
                  >> presented in <istream> and <ostream>, respectively, and also in
                  >> <iostream>". std::endl is an ostream manipulator.[/color]
                  >
                  >This is what usually happens, but it isn't what the standard says.
                  >It's what most people (including me - untill I was told otherwise)
                  >expect to happen.
                  >[color=green]
                  >> Also, since std::cout is an std::ostream object, and is defined in
                  >> <iostream>, doesn't it follow that <ostream> has to have been included
                  >> by <iostream>?
                  >>[/color]
                  >
                  ><iostream> needs to declare the objects cin, cout, cerr etc.
                  >The simplest way to do that is to:
                  >
                  >#include <istream>
                  >#include <ostream>
                  >
                  >and then declare:
                  >
                  >namespace std
                  >{
                  > extern istream cin;
                  > extern ostream cout;
                  > ...
                  >}
                  >
                  >But it doesen't have to work like that. All it *has* to do is
                  >include enough of the declaration's of std::basic_istr eam<> and
                  >std::basic_ost ream<> and possibly the typedef's std::istream and
                  >std::ostream , so that it can declare the cin, cout ... objects.
                  >
                  >It could miss out all the non-inline member defenition's.
                  >
                  >It could also miss out defenition's for the constructor's and
                  >destructor's for the basic_?stream<> 's even if they are inlined
                  >as cin etc are magicly constructed before main() is entered and
                  >aren't destroyed.
                  >
                  >Most importantly, It dosent need to include the declaration and/or
                  >defenition's for std::endl, std::ends, or std::flush. Firstly
                  >because the standard dosen't require it too and secondly because
                  >they aren't required for functioning cin, cout ... objects.[/color]

                  I was simply amazed the first time I heard of this.

                  But it's not like it is an argument that virtually _all_ C++ textbooks,
                  including TCPPPL, are incorrect.

                  It's simply an argument that the standard has an oversight, in addition
                  to numerous other oversights.

                  We should not teach newbies to include more than <iostream> in order to
                  use std::cout and std::endl.

                  Instead, point out (if necessary) that they might run into people who
                  incorrectly think that the common practice is incorrect.

                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: errors result when creating header file - visual c++

                    On 07 Sep 2003 13:06:29 GMT, Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote:
                    [color=blue][color=green]
                    >> Instead, point out (if necessary) that they might run into people who
                    >> incorrectly think that the common practice is incorrect.
                    >>[/color]
                    >
                    >I agree (kind off) but where do you draw the line. std::endl in
                    ><iostream> std::make_pair in <map> or maybe:
                    >
                    >((some_class_t ype)0)->some_non_virtu al_member();[/color]

                    Some cases are not clear-cut.

                    Some are clearly against current practice.

                    The case for <iostream> is, otoh., very simple and clear: there's not
                    one C++ book that I know of that includes <ostream> to get std::endl.

                    Comment

                    Working...