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

Discussion in 'C++' started by sandy, Nov 26, 2006.

  1. sandy

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

  2. sandy

    Daniel T. Guest

    Don't put using declarations or definitions at global or namespace scope
    in header files.
    You need a semi-colon at the end of the class definition.
     
    Daniel T., Nov 26, 2006
    #2
    1. Advertisements

  3. sandy

    sandy Guest

    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.)
    Thanks!

    I knew I needed another pair of eyes!

    You have helped me before. I really appreciate it.
     
    sandy, Nov 26, 2006
    #3
  4. sandy

    BobR Guest

    wrote in message ...
    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.

    Same as above. (your *.cpp will only be 'included' once)
    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/
     
    BobR, Nov 26, 2006
    #4
  5. sandy

    red floyd Guest

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

    Daniel T. Guest

    Call it a necessary evil.
    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.
    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.
    No problem.
     
    Daniel T., Nov 27, 2006
    #6
  7. sandy

    Daniel T. Guest

    Even this is frowned upon (see "C++ Coding Standards" by Sutter and
    Alexandrescu)
    Personally, I think the FAQ is a little too restrictive regarding this
    issue. YMMV.
     
    Daniel T., Nov 27, 2006
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.