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

Discussion in 'C++' started by sandy@murdocks.on.ca, Nov 26, 2006.

  1. Guest

    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>
     
    , Nov 26, 2006
    #1
    1. Advertising

  2. Daniel T. Guest

    In article <>,
    "" <> 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.
     
    Daniel T., Nov 26, 2006
    #2
    1. Advertising

  3. Guest

    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.
     
    , Nov 26, 2006
    #3
  4. BobR Guest

    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::eek: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
     
    BobR, Nov 26, 2006
    #4
  5. red floyd Guest

    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.
     
    red floyd, Nov 26, 2006
    #5
  6. Daniel T. Guest

    "" <> 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.
     
    Daniel T., Nov 27, 2006
    #6
  7. Daniel T. Guest

    "BobR" <> wrote:
    > 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.
     
    Daniel T., Nov 27, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    0
    Views:
    356
    darrel
    Jun 21, 2004
  2. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: need second pair of eyes: databinder.eval problem

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 21, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    440
    darrel
    Jun 21, 2004
  3. DaLoverhino

    J2ee: need another pair of eyes.

    DaLoverhino, Nov 22, 2005, in forum: Java
    Replies:
    1
    Views:
    396
    HalcyonWild
    Nov 22, 2005
  4. =?Utf-8?B?cm9kY2hhcg==?=

    another pair of eyes...

    =?Utf-8?B?cm9kY2hhcg==?=, Jul 12, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    =?Utf-8?B?cm9kY2hhcg==?=
    Jul 12, 2006
  5. Steve Hershoff
    Replies:
    3
    Views:
    344
    Steve Hershoff
    Aug 6, 2007
Loading...

Share This Page