Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Student needs a pair of eyes... Simple? problem

Reply
Thread Tools

Student needs a pair of eyes... Simple? problem

 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-26-2006
I think I just need a pair of eyes here... I can't see what I am doing
wrong.

I am creating a new Class for an assignment, Class File.

I have a header and a cpp file. When I try to write the constructor in
the cpp file it tells me:

File.cpp new types may not be defined in a return type
File.cpp return type specification for constructor invalid

I don't see a return type for/from my constructor so I figure it must
be confused and I have done something else wrong. Below are the header
and .cpp.

Thanks.

< HEADER CODE>
#ifndef File_h
#define File_h

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class File
{
private:
string Name, Extension;

public:

/*
Constructor

Inputs: FullName
Outputs: None

Notes: Takes in the full name (name + extension) splits it.
*/
File(string FullName);

/*
GetName

Inputs: None
Outputs Name (minus extension)
*/
string GetName();

/*
GetExtension

Inputs: None
Outputs: File Extension
*/
string GetExtension();

/*
Print

Inputs: None
Outputs: prints to screen full file name (name and extension)
*/
void Print();

/*
operator ==

Inputs: File F
Outputs: bool

Notes: If the file.Name and file.Extension are equal the files
are equal if either is not equal, the files are not equal.
*/
bool operator==(const File & F);

/*
operator <

Inputs: File F
Outputs: bool

Notes: If the full file name is < the 'other' file name True.
*/
bool operator<(const File & F);

/*
operator >

Inputs: File F
Outputs: bool
*/
bool operator>(const File & F);
}

#endif

</HEADER CODE>

<CPP CODE>
#include "File.h"
#include <iostream>
#include <cstdlib>
#include <string>

//using namespace std;

/*
Constructor
*/
File::File(string FullName)
{
int charLoc; // character location
}

</CPP CODE>

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      11-26-2006
In article <(E-Mail Removed) .com>,
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> I think I just need a pair of eyes here... I can't see what I am doing
> wrong.
>
> I am creating a new Class for an assignment, Class File.
>
> I have a header and a cpp file. When I try to write the constructor in
> the cpp file it tells me:
>
> File.cpp new types may not be defined in a return type
> File.cpp return type specification for constructor invalid
>
> I don't see a return type for/from my constructor so I figure it must
> be confused and I have done something else wrong. Below are the header
> and .cpp.
>
> Thanks.
>
> < HEADER CODE>
> #ifndef File_h
> #define File_h
>
> #include <cstdlib>
> #include <iostream>
> #include <string>
>
> using namespace std;


Don't put using declarations or definitions at global or namespace scope
in header files.

> class File
> {
> private:
> string Name, Extension;
>
> public:
>
> /*
> Constructor
>
> Inputs: FullName
> Outputs: None
>
> Notes: Takes in the full name (name + extension) splits it.
> */
> File(string FullName);
>
> /*
> GetName
>
> Inputs: None
> Outputs Name (minus extension)
> */
> string GetName();
>
> /*
> GetExtension
>
> Inputs: None
> Outputs: File Extension
> */
> string GetExtension();
>
> /*
> Print
>
> Inputs: None
> Outputs: prints to screen full file name (name and extension)
> */
> void Print();
>
> /*
> operator ==
>
> Inputs: File F
> Outputs: bool
>
> Notes: If the file.Name and file.Extension are equal the files
> are equal if either is not equal, the files are not equal.
> */
> bool operator==(const File & F);
>
> /*
> operator <
>
> Inputs: File F
> Outputs: bool
>
> Notes: If the full file name is < the 'other' file name True.
> */
> bool operator<(const File & F);
>
> /*
> operator >
>
> Inputs: File F
> Outputs: bool
> */
> bool operator>(const File & F);
> }


You need a semi-colon at the end of the class definition.

> #endif
>
> </HEADER CODE>
>
> <CPP CODE>
> #include "File.h"
> #include <iostream>
> #include <cstdlib>
> #include <string>
>
> //using namespace std;
>
> /*
> Constructor
> */
> File::File(string FullName)
> {
> int charLoc; // character location
> }
>
> </CPP CODE>


--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
 
 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-26-2006

Daniel T. wrote:
> > using namespace std;

>
> Don't put using declarations or definitions at global or namespace scope
> in header files.


Hmmm....

What is the proper way? I could do: std:: where ever needed but that's
a bit of a pain.

Is there some other place to put a 'using' or should I just leave it
out in the headers?

I assume it is okay in the .cpp files?

(It compiles there, but perhaps there is a reason I am not aware of for
not putting it there.)

>
> You need a semi-colon at the end of the class definition.


Thanks!

I knew I needed another pair of eyes!

You have helped me before. I really appreciate it.

> --
> To send me email, put "sheltie" in the subject.


 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      11-26-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in message ...
>
>Daniel T. wrote:
>> > using namespace std;

>>
>> Don't put using declarations or definitions at global or namespace scope
>> in header files.

>
>Hmmm....
>
>What is the proper way? I could do: std:: where ever needed but that's
>a bit of a pain.
>
>Is there some other place to put a 'using' or should I just leave it
>out in the headers?


If you absolutely can't type 'std::' where needed, at least limit the
pollution:

// > #include <cstdlib>
// > #include <iostream>
// move those two to the .cpp file
// unless they are actually needed in the declarations.

#include <string>
using std::string; // only expose what you need.

>
>I assume it is okay in the .cpp files?


Same as above. (your *.cpp will only be 'included' once)

>
>(It compiles there, but perhaps there is a reason I am not aware of for
>not putting it there.)


You open the namespace needlessly in all files that you include your header
in ( remember you will most likely include it in main.cpp + others). It won't
matter much in simple 'toy' programs, but could cause trouble in larger
projects. Try to build good habits early!

As a (not standard practice) example: in my TestBench program, I use:

std::istringstream cin;
std:stringstream cout;
and:
std::cout

cout<<"This goes to my text window."<<std::endl;
std::cout<<"This only goes to the debugger window."<<std::endl;

If I had 'using namespace std;' ANYWHERE, I'd get some surprises (if it even
compiled)!


For a better explanation, see the FAQ:
The comp.lang.c++ FAQ is available at http://www.parashift.com/c++-faq-lite/

--
Bob R
POVrookie


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-26-2006
(E-Mail Removed) wrote:
> Daniel T. wrote:
>>> using namespace std;

>> Don't put using declarations or definitions at global or namespace scope
>> in header files.

>
> Hmmm....
>
> What is the proper way? I could do: std:: where ever needed but that's
> a bit of a pain.
>
> Is there some other place to put a 'using' or should I just leave it
> out in the headers?
>
> I assume it is okay in the .cpp files?
>


The proper way is to leave it out of the headers. Feel free to use it
(properly) inside a non-header file.

The rationale is that there may be other header files included in the
translation unit, and if they follow your header file, the using
directive will be in effect for them, which can lead to errors.

You should put it in your cpp file after all includes.

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      11-27-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
> Daniel T. wrote:
>>> using namespace std;

>>
>> Don't put using declarations or definitions at global or namespace
>> scope in header files.

>
> Hmmm....
>
> What is the proper way? I could do: std:: where ever needed but
> that's a bit of a pain.


Call it a necessary evil.

> Is there some other place to put a 'using' or should I just leave it
> out in the headers?


When you are defining a an inline function in the header file, you can
put a "using namespace ..." within the function body to avoid some of
the std:: references.

> I assume it is okay in the .cpp files?


Yes, after all #includes. You can put it in the cpp file because its
effect is highly localized that way, a namespace collision can be easily
resolved.

>> You need a semi-colon at the end of the class definition.

>
> Thanks!
>
> I knew I needed another pair of eyes!
>
> You have helped me before. I really appreciate it.


No problem.

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      11-27-2006
"BobR" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote in message ...
>> Daniel T. wrote:
>>>
>>>> using namespace std;
>>>
>>> Don't put using declarations or definitions at global or namespace
>>> scope in header files.

>>
>> Hmmm....
>>
>> What is the proper way? I could do: std:: where ever needed but
>> that's a bit of a pain.
>>
>> Is there some other place to put a 'using' or should I just leave
>> it out in the headers?

>
> If you absolutely can't type 'std::' where needed, at least limit
> the pollution:
>
> // > #include <cstdlib>
> // > #include <iostream>
> // move those two to the .cpp file
> // unless they are actually needed in the declarations.
>
> #include <string>
> using std::string; // only expose what you need.


Even this is frowned upon (see "C++ Coding Standards" by Sutter and
Alexandrescu)

> For a better explanation, see the FAQ:
> The comp.lang.c++ FAQ is available at http://www.parashift.com/c++-faq-lite/


Personally, I think the FAQ is a little too restrictive regarding this
issue. YMMV.

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IPython team needs a student for a google "Summer of Code" project. Ville Vainio Python 0 05-03-2006 04:07 PM
Java Student Needs Help beyonder74 Java 5 09-15-2004 06:32 PM
A novice college student needs the help of the true "guru"s! Rodney Edmondson Java 3 11-25-2003 04:46 PM



Advertisments