473,671 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 19 '05 #1
9 5538
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, so****@hotmail. com (soni29) wrote:
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.

Jul 19 '05 #2
soni29 wrote in news:ca******** *************** ***@posting.goo gle.com:
when i try to break it into a header and source file, 2 separate
files, i get errors:
source file:
#include <iostream.h>
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */
class CBox;
// replace with:

#include "CBox.h"

int main() {
using std::cout; /* so we can write cout instead of std::cout */
using std::endl; /* ... */
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>

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

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";
std::cout << std::endl << "Constructo r called";
}

// Default Constructor
CBox() {
cout << endl
std::cout << std::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;
};


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3
David wrote:
I'm not sure of the file split,

<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.

http://www.parashift.com/c++-faq-lite/

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

Jul 19 '05 #4
Thank you.
Jul 19 '05 #5
Rob Williscroft wrote:
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */


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?
Jul 19 '05 #6
John L Fjellstad wrote in news:bd******** ***@192.168.1.1 :
Rob Williscroft wrote:
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */
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.


Logic's good so far.
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>?


No <iostream>

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #7
John L Fjellstad wrote in news:bd******** ***@192.168.1.1 :

My appologees for the previous post. I clicked send before
I was finnished.
Rob Williscroft wrote:
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */
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.


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.
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>?


<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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #8
On 07 Sep 2003 08:46:59 GMT, Rob Williscroft <rt*@freenet.RE MOVE.co.uk> wrote:
John L Fjellstad wrote in news:bd******** ***@192.168.1.1 :

My appologees for the previous post. I clicked send before
I was finnished.
Rob Williscroft wrote:
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */


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.


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.
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>?


<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.


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.

Jul 19 '05 #9
On 07 Sep 2003 13:06:29 GMT, Rob Williscroft <rt*@freenet.RE MOVE.co.uk> wrote:
Instead, point out (if necessary) that they might run into people who
incorrectly think that the common practice is incorrect.


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();


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.

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3174
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a i386-pc-solaris2.9. Seeing reference to gcc-2.7.2 in the Makefile of the code, I downloaded it and compiled in my home directory. Then changed the referenes of LIBDIR and INCLUDES to this installation .and ran with g++ for 2.7.2 then there are still...
12
2516
by: Russ | last post by:
Hello. My new dev machine is running XP Pro. In the past all equipment has only used Windows 2000. I have had a lot of problems getting my projects up and running on the new machine. The current one is a permission error. The project is a VC++ Web Service. It works fine when the service is hosted on the old W2K dev machine, but on the new XP machine I get a permission error when the service tries to open a text file on the Windows...
24
2486
by: Massimo Soricetti | last post by:
Hello, I'm not a C newbie, but I'm teaching C programming (well... FIRST programming and then C) to other guys these days and it's driving me to some reflexions on the language. It's not uncommon to forget a } writing code, and at compiling time get an error 18956778 lines after the mistake, in an otherwise absolutely correct piece of code. Or, sometimes in my journeys I got errors reported in a file, checked and found it correct, and...
0
2018
by: Krishnan | last post by:
Hi I am trying to build a small client-server program using OpenSSL functions. I get errors on trying to build the program using Visual Studio .Net (Visual C++ 7.0). I am new to Visual Studio environment, pls bear with me if this one is a trivial question Here's the procedure I followed (1) Copied libeay32.lib and ssleay32.lib to C:\program files\Microsoft Visual Studio .NET\Vc7\li (2) Copied the OpenSSL header files to c:\program...
2
1634
by: jen_designs | last post by:
I am trying to create a template in dot net. All the articles I have read on the net are rather confusing. I need a simple solution to create a header and a footer on each page. Can someone point me into the right direction. I started off creating a file template.cs: using System; using System.Web; using System.Web.UI;
1
1738
by: Givisi | last post by:
Hello all! I am a completly newbie at C and I dont have good basis from Java so I cant get the linked lists part from C. I am trying to run the code from the function that is written at the end of this message but it has run-time errors so its hard to find the bugs. I really know that my logic is not so good since I cant understand that bit so well. I am using dm compiler. Could somebody possibly tell me what my mistakes are. Thank you...
14
1767
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for something undeclared, the first thing I do is go through my header files and check that the inclusion-guard macro is specific to that header file. For instance, if the header file is called "data.h", I make sure the inclusion guard is "H__DATA" and...
4
2812
by: rhivka | last post by:
I'm going to try to keep this question within the student posting guidelines. I've been working on a class file, and I'm not sure I've constructed it correctly. I'm getting several errors concerning the function definitions and matching { with }. I've checked my brackets several times and it looks right to me. I've made changes, changed back, and I'm at a loss. I'm using Visual C++ Express 2005 on Windows Vista OS. It may just be something...
4
2786
by: Dan | last post by:
Hi All, I've got a problem with my C++ application that calls a Java class that I've built with GCJ, I can't run it because I get errors: multiple definition of `atexit' first defined here multiple definition of `_onexit' first defined here multiple definition of `__do_sjlj_init'
0
8473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.