+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 15

Thread: Simple Class Question

  1. #1
    Newbie Namesake is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    21

    Simple Class Question

    Hey all,
    I am just getting into writing code in C++ and have written code in JAVA and FORTRAN a while back - so I am a little rusty.
    I am just trying to write a simple Class to get the feel for it all. Basically the class CRectangle has the input of width and length, the area() member returns the area. I have included a number of get functions too, just for practise.

    When I run this without seperating the code into a header file, a function file (?) - where you define the construct and member functions - and a main program, the program works fine.

    Now that I have seperated it, it is not working, with the errors:

    In file included from CRectangle.cpp:2:
    CRectangle.h:9: error: expected unqualified-id before ‘int’
    CRectangle.h:9: error: expected ‘)’ before ‘int’
    CRectangle.h:7: error: an anonymous struct cannot have function members
    CRectangle.h:16: error: abstract declarator ‘<anonymous class>’ used as declaration
    CRectangle.cpp:5: error: expected identifier before ‘(’ token
    CRectangle.cpp:5: error: expected ‘;’ before ‘(’ token
    CRectangle.cpp:5: error: expected unqualified-id before ‘int’
    CRectangle.cpp:5: error: expected ‘)’ before ‘int’
    CRectangle.cpp:10: error: explicit qualification in declaration of ‘int area()’
    CRectangle.cpp: In function ‘int area()’:
    CRectangle.cpp:11: error: ‘length’ was not declared in this scope
    CRectangle.cpp:11: error: ‘width’ was not declared in this scope
    CRectangle.cpp: At global scope:
    CRectangle.cpp:14: error: explicit qualification in declaration of ‘int getlength()’
    CRectangle.cpp: In function ‘int getlength()’:
    CRectangle.cpp:15: error: ‘length’ was not declared in this scope
    CRectangle.cpp: At global scope:
    CRectangle.cpp:18: error: explicit qualification in declaration of ‘int getwidth()’
    CRectangle.cpp: In function ‘int getwidth()’:
    CRectangle.cpp:19: error: ‘width’ was not declared in this scope
    In file included from mainrecttest.cpp:2:
    CRectangle.h:9: error: expected unqualified-id before ‘int’
    CRectangle.h:9: error: expected ‘)’ before ‘int’
    CRectangle.h:7: error: an anonymous struct cannot have function members
    CRectangle.h:16: error: abstract declarator ‘<anonymous class>’ used as declaration
    mainrecttest.cpp: In function ‘int main()’:
    mainrecttest.cpp:34: error: ‘rect’ was not declared in this scope



    I know there is a simple mistake somewhere in the syntax, but for the life of me, I cannot find it. Please help!

  2. #2
    Newbie Namesake is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    21

    Re: Simple Class Question

    I am not sure if I attached the source code correctly, I have attempted to do so twice. So here are the text versions just in case

    Header file CRectangle.h
    Code:
    #ifndef CRectangle
    #define CRectangle
    #include <iostream>
    
    using namespace std;
    
    class CRectangle {
    	public:
    		CRectangle(int, int);
    		int area();
    		int getlength();
    		int getwidth();
    	private:
    		int length, width;
    
    };
    #endif
    The function file for the class, CRectangle.cpp
    Code:
    #include <iostream>
    #include "CRectangle.h"
    using namespace std
    
    CRectangle::CRectangle(int a,int b){
    	length = a;
    	width = b;
    }
    
    int CRectangle::area(){
    	return(length*width);
    }
    /*get the goods*/
    int CRectangle::getlength(){
    	return (length);
    }
    
    int CRectangle::getwidth(){
    	return (width);
    }
    and the main program
    Code:
    #include <iostream>
    #include "CRectangle.h"
    using namespace std;
    
    int main () {
     	CRectangle rect (3,4);
    	cout<< "length: " << rect.getlength()<< endl;
    	cout<< "width: " << rect.getwidth()<< endl;
     	cout << "rect area: " << rect.area() << endl;
      return 0;
    }
    Last edited by Orjan; 02-07-2010 at 10:11 AM. Reason: Please use code tags!

  3. #3
    Programming God outsid3r has a spectacular aura about outsid3r has a spectacular aura about outsid3r's Avatar
    Join Date
    Jul 2008
    Location
    Portugal
    Posts
    564

    Re: Simple Class Question

    You really need to pay attention to your code...

    1º CRectangle is defined first as a macro, so you can't use that name for a class. Change the macro name to CRECTANGLE_H or something else that don't conflict with other names.

    2º CRectangle.cpp, line 3 "using namespace std", is missing a ';' in the end.

  4. #4
    Newbie Namesake is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    21

    Re: Simple Class Question

    Quote Originally Posted by outsid3r View Post
    You really need to pay attention to your code...

    1º CRectangle is defined first as a macro, so you can't use that name for a class. Change the macro name to CRECTANGLE_H or something else that don't conflict with other names.

    2º CRectangle.cpp, line 3 "using namespace std", is missing a ';' in the end.
    forgive my stupidity, but I am not sure about your first point....
    are you saying that, in the header, I do not write CRectangle(int, int), but CRectangle_h(int, int)...?


    Thanks for such a rapid response, too!

  5. #5
    Programming God outsid3r has a spectacular aura about outsid3r has a spectacular aura about outsid3r's Avatar
    Join Date
    Jul 2008
    Location
    Portugal
    Posts
    564

    Re: Simple Class Question

    No, i mean the macro you defined:

    Code:
    #ifndef CRectangle
    #define CRectangle
    
    ...
    That name is reserved for that macro, so you can't use the same name in a class. You need to change the class name or the macro name, so they will not conflict. I suggest you to change the macro name to CRECTANGLE_H.

  6. #6
    Newbie Namesake is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    21

    Re: Simple Class Question

    Thank you!

    You Sir, are a scholar and a gentleman!
    All works well.

    Can I just ask you if what I think a macro is, is correct?

    A macro is the name/handle of the header file?

  7. #7
    Programming God outsid3r has a spectacular aura about outsid3r has a spectacular aura about outsid3r's Avatar
    Join Date
    Jul 2008
    Location
    Portugal
    Posts
    564

    Re: Simple Class Question

    Macros are user defined symbols meant to be used by pre-processor before the real compilation stage, you can use them for a variety of reasons. Macros are defined as:

    #define <identifier> <replacement expression>

    The pre-processor will replace every macro symbol by the replacement expression, for example:

    Code:
    #define OK 0
    #define ERROR 1
    
    ... some code
    
    return (OK);
    
    or maybe
    
    return (ERROR);
    before the compilation, ERROR and OK will be replaced by 1 and 0 respectively.

    Macros also support conditional expressions, like:

    Code:
    #ifndef SOME_SYMBOL
    #    define SYMBOL
    #else
    #    define OTHER_SYMBOL
    #endif
    This is used for conditional compilation, before the real compilation stage the pre-processor will define one of the symbols, based on the condition.


    Now, in a header file, like in your case:

    #ifndef CRECTANGLE_H
    #define CRECTANGLE_H

    #endif
    Prevents the file to be included twice, because the symbol CRECTANGLE_H will be defined, and then you include it again, it is already defined, so the code inside the macro condition will not be included again.

    There are many other reasons for macro usage, i suggest you to read C/C++ pre-processor reference:

    C/C++ Preprocessor Reference (C/C++)

  8. #8
    Newbie Namesake is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    21

    Re: Simple Class Question

    Once again, outsid3r you have been a massive help; many thanks!


  9. #9
    Programming God outsid3r has a spectacular aura about outsid3r has a spectacular aura about outsid3r's Avatar
    Join Date
    Jul 2008
    Location
    Portugal
    Posts
    564

    Re: Simple Class Question

    I'm glad to help

    Now some tips:

    You don't need to define #include <iostream> and using namespace std in your CRectangle.cpp and CRectangle.h files, because non of them uses functions related with iostream, so you are just polluting the space with names. Don't define them, specially in header files, because that's those that will be included in another files...

    If you really need to define some library in an header file, do it like:

    Code:
    #ifndef CRECTANGLE_H
    #define CRECTANGLE_H
    
    #include <string>
    
    
    class CRectangle {
    public:
       std::string getName();
    
       ...
    }
    
    #endif
    do not include the whole namespace because you're polluting the space with names that may conflict with others.
    Another thing is the return 0; in main() function, in C++ you don't need to do that because the compiler puts it implicitly.

  10. #10
    Newbie Namesake is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    21

    Re: Simple Class Question

    Those tips help out a fair bit! It is obviously a good idea to start a good coding practise now.

    Cheers outsid3r!


+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 08:30 AM
  2. Tutorial: Starting C# with C# 2008 Express Edition
    By Jordan in forum CSharp Tutorials
    Replies: 20
    Last Post: 07-27-2009, 04:45 AM
  3. Classes in VB.NET
    By Vswe in forum VB Tutorials
    Replies: 1
    Last Post: 07-21-2009, 12:18 PM
  4. Probably a simple class question
    By utdiscant in forum Python
    Replies: 1
    Last Post: 06-10-2008, 09:10 PM
  5. n00b class question
    By MerakSpielman in forum C and C++
    Replies: 15
    Last Post: 02-20-2008, 10:37 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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