CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Sep 2007
    Posts
    10

    Arrow c++ .h, .cpp question

    Hi all.. I have recently started a c++ class and I need some of your help with...

    Ive understood all thats required but below is the basic header file for which I will have to create an implementation.

    I have a stattest.cpp, .h file, & statexam.cpp and am currently working on all the functions. I will post any questions I may have. In the meantime, if you can leave me with any 'tips' to help me make sure I am on the right path will be fine.

    >>I am not asking for a full code for my program, just some helpful tips!

    Below is the given .h file :


    Code:
    #ifndef STATS_H     
    #define STATS_H
    #include <iostream>
    
    namespace stat
    {
        class statistician
        {
        public:
            // CONSTRUCTOR
            statistician( );
            // MODIFICATION MEMBER FUNCTIONS
            void next(double r);
            void reset( );
            // CONSTANT MEMBER FUNCTIONS
            int length( ) const;
            double sum( ) const;
            double mean( ) const;
            double minimum( ) const;
            double maximum( ) const;
            // FRIEND FUNCTIONS
            friend statistician operator +
                (const statistician& s1, const statistician& s2);
            friend statistician operator *
                (double scale, const statistician& s);
        private:
            int count;       
            double total;    
            double tiniest;  
            double largest;  
        };
    
        // NON-MEMBER functions 
        bool operator ==(const statistician& s1, const statistician& s2);
    }
    
    #endif
    Edit: I am currently working on the program and as soon as I have worked out different functions, I will post any questions I may have.

    Thanks!
    Last edited by GaganW18; September 23rd, 2007 at 01:03 AM.

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Help w/ c++ assignment...

    I know why your post hasn't received much response, and I'm sorry to say this but, we've seen a few requests for a statistician class around here, we know you want help with homework but collectively we have an ethic of not 'doing' homework for anyone.

    What you've posted is like the table of contents, and now have requested that we start writing the story.

    To get help, you'll need to try to create a function - ask questions about that function(s) you're trying to write, but something less obvious than "what do I do?"

    For example....

    // double minimum( ) const
    // Precondition: length( ) > 0
    // Postcondition: The return value is the tinyest number in the
    // statistician's sequence.


    It seems you must ensure that the length() > 0. If it's not, what would you do?

    How would you find the 'tinyest' number in the sequence?

    Can you start out doing that?

    Is there a simpler function you can take a running start at?
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Help w/ c++ assignment...

    Additionally, your chances of getting a reply increase, if you read some of the rules for posting, e.g. the one about [code] tags.

  4. #4
    Join Date
    Sep 2004
    Posts
    236

    Re: Help w/ c++ assignment...

    hah I remember doing this program years ago!
    Probably same book too,
    main_savitch_2C namespace.
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

  6. #6

    Re: Help w/ c++ assignment...

    First thing that comes to mind is for the constructor you have there in that outline header file you're gonna need to put in a simple destructor method.

    That's pretty much the simplest thing you'll have to do.

  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Help w/ c++ assignment...

    Quote Originally Posted by goatslayer
    First thing that comes to mind is for the constructor you have there in that outline header file you're gonna need to put in a simple destructor method.
    No. The automatically generated destructor will do fine.

    One thing you should ask you lector is why count is defined as signed int? And why is the variable called count but the function is called length? Are these supposed to be different things?

  8. #8

    Re: Help w/ c++ assignment...

    Quote Originally Posted by treuss
    No. The automatically generated destructor will do fine.

    One thing you should ask you lector is why count is defined as signed int? And why is the variable called count but the function is called length? Are these supposed to be different things?
    I thought if you explicitly set up a constructor then you explicitly had to set up a destructor?

  9. #9
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Help w/ c++ assignment...

    Quote Originally Posted by goatslayer
    I thought if you explicitly set up a constructor then you explicitly had to set up a destructor?
    Nope. No such rule. There is a rule-of-thumb, that if you need to define a copy-constructor you will probably also need to define a destructor and an assignement operator. Likewise, if you need to define a non-empty destructor (you might define an empty destructor just to have it virtual), you will probably also need the other two. This however is not a rule of the C++ language, rather a rule that newbees should keep in mind.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Help w/ c++ assignment...

    I think the rule you're probably thinking of is if you provide any type of constructor, the compiler will not auto-generate the default constructor for you:
    Code:
    class Foo
    {
       public:
          Foo(int someNum) : m_Num(someNum) {}
    
       private:
          int m_Num;
    };
    
    int main()
    {
       Foo myFoo;   // Should throw a compile error
    }
    Viggy

  11. #11
    Join Date
    Sep 2007
    Posts
    10

    Re: Help w/ .cpp...

    Below are some functions, I havent tested out all parts of my .cpp file yet but I am getting ready to do so.

    I also have question about bool operator ==. The requirement is:
    -The return value is true if s1 and s2 have the zero
    length. Also, if the length is greater than zero, then s1 and
    s2 must have the same length, the same mean, the same
    minimum, the same maximum, and the same sum.

    Code:
    stats::statistician()
    	{
    		reset(); //call reset func
    	}
    void stats::reset()
    	{
    		
    		total = count = 0;
    		tiniest = DBL_MAX; //**this was an example, why is this set to DBL_MAX? 
    		largest = DBL_MIN;
    		
    	}
    I need some help in understanding what the bool operator== is suppose to do and in general what are operator+ or operator* used for?

    Thanks!
    Last edited by GaganW18; September 18th, 2007 at 11:22 PM.

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Help w/ .cpp...

    Quote Originally Posted by GaganW18
    I need some help in understanding what the bool operator== is suppose to do and in general what are operator+ or operator* used for?

    Thanks!
    The '==' operator is used in comparisons, specifically to test for equality. You are allowed to overload any of the operators you want. So, if it makes sense for you to add two objects of type 'stats', then you should add a operator+ overload. The same for the multiplication operator ('*').

    Code:
    bool stats::operator==(const stats &rhs)
    {
       // Put code here that returns true if this == rhs
    }
    Viggy

  13. #13
    Join Date
    Sep 2007
    Posts
    10

    Re: Help w/ c++ assignment...

    Hi All.. I need help.

    Below is the stats.cpp file for which I am getting errors. I mainly am having problems with operator +, *, ==. I have also pasted the errors. Please help me, I am confused!

    Notice, I have to declare my doubles' again or else it states: undeclared.


    Code:
    /***********************
    Purpose:
    Statistician class...
    ************************/
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <string.h>
    #include <iomanip> 
    #include <cfloat>   
    
    #include "stats.h"
    using namespace std; 
    
    namespace stat
    {
    	
    	class statistician
    {
    		double last;//why do i have to declare again?
    		double count;// header file has all these
    		double total;
    		double tiniest;
    		double largest;
    		double s;
    
    	statistician::statistician()
    	{
    		reset();
    	}
    	
    	void statistician::reset()
    	{
    		
    		total = count = 0;
    		tiniest = DBL_MIN;
    		largest = DBL_MAX;
    		
    	}
    	 	
    ...
    errors:

    --------------------Configuration: stats - Win32 Debug--------------------
    Compiling...
    stats.cpp
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(117) : error C2228: left of '.total' must have class/struct/union type
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(118) : error C2228: left of '.tiniest' must have class/struct/union type
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(118) : error C2228: left of '.tiniest' must have class/struct/union type
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(119) : error C2228: left of '.largest' must have class/struct/union type
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(119) : error C2228: left of '.largest' must have class/struct/union type
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(120) : error C2228: left of '.count' must have class/struct/union type
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(132) : error C2660: 'maximum' : function does not take 0 parameters
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(132) : error C2660: 'maximum' : function does not take 0 parameters
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(134) : error C2660: 'minimum' : function does not take 0 parameters
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(134) : error C2660: 'minimum' : function does not take 0 parameters
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(143) : warning C4305: 'return' : truncation from 'const int' to 'bool'
    Error executing cl.exe.

    stats.exe - 10 error(s), 1 warning(s)
    Last edited by GaganW18; September 24th, 2007 at 09:30 AM.

  14. #14
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Help w/ c++ assignment...

    The answer to your question "why do i have to declare again?" is "You don't!".
    The errors you are getting are due to the facts that you define everything inside the class statician. The class itself should be declared in the header file (stats.h). If that is done (according to your original post), just drop the following (and the corresponding closing brace) from the file:
    Code:
    class statistician
    {
    		double last;//why do i have to declare again?
    		double count;// header file has all these
    		double total;
    		double tiniest;
    		double largest;
    		double s;

  15. #15
    Join Date
    Sep 2007
    Posts
    10

    class or namespace error..need compiling help!!

    Why do I get an error: "statistician is not a class or namespace name?"

    What do I need to use for assert?

    Why is their a syntax error for operator + () ??

    Code:
    stats.h file
    Code:
    #ifndef STATS_H     // Prevent duplicate definition
    #define STATS_H
    #include <iostream>
    
    namespace CISP430_A1
    {
        class statistician
        {
        //rest of code
    stats.cpp file
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <string.h>
    #include <iomanip> 
    using namespace std;   
    
    #include "STATS.H"
    
    	
    	statistician::statistician()
    	{
    		reset();
    	}
             //rest of code
    I am not using any makefile. I am just using my Visual Studio C++ and I have a test file with int main ().
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <string.h>    // Provides memcpy function
    #include "stats.h"
    using namespace CISP430_A1;
    using namespace std;
    
    const int SCORE0=0,SCORE1=90, SCORE2=15;
    
    bool close(double a, double b)
    {
        const double EPSILON = 1e-5;
        return (fabs(a-b) < EPSILON);
    }
    
    int test1( )
    //rest of code
    Please let me know why its not seeing the class statistician and header file things.

    Errors:
    --------------------Configuration: stats - Win32 Debug--------------------
    Compiling...
    stats.cpp
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(23) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(25) : error C2065: 'reset' : undeclared identifier
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(26) : warning C4508: 'statistician' : function should return a value; 'void' return type assumed
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(28) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(30) : error C2065: 'total' : undeclared identifier
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(31) : error C2065: 'count' : undeclared identifier
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(32) : error C2065: 'tiniest' : undeclared identifier
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(33) : error C2065: 'largest' : undeclared identifier
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(35) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(36) : error C2373: 'reset' : redefinition; different type modifiers
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(42) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(43) : error C2270: 'length' : modifiers not allowed on nonmember functions
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(47) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(48) : error C2270: 'sum' : modifiers not allowed on nonmember functions
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(52) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(53) : error C2270: 'mean' : modifiers not allowed on nonmember functions
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(54) : error C2065: 'assert' : undeclared identifier
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(58) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(58) : error C2143: syntax error : missing ',' before '&'
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(58) : error C2059: syntax error : '&'
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(64) : error C2653: 'statistician' : is not a class or namespace name
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(64) : error C2143: syntax error : missing ',' before '&'
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(64) : error C2059: syntax error : '&'
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(71) : error C2143: syntax error : missing ';' before '+'
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(71) : error C2501: 'statistician' : missing storage-class or type specifiers
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(71) : error C2373: 'statistician' : redefinition; different type modifiers
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(23) : see declaration of 'statistician'
    C:\Documents and Settings\Gagan\My Documents\Documents\Data Structures\stats\stats\stats.cpp(71) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    stats.exe - 26 error(s), 1 warning(s)

    As Always, thanks!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured