Thread: Declaring an object of class type. Giving errors, need help!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    34

    Declaring an object of class type. Giving errors, need help!

    I'm new to writing classes.
    This is the class file.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    class phones
    
    {
     private:
    string phone;
    string cost;
    int year;
    
    public:
    phones();
    void setPhone (string p );
    string getPhone();
    void setCost (string c );
    string getCost();
    void setYear (int y );
    int getYear();
    void Displayinfo();
    
    #include <"phones.h">
    #include <iostream>
    #include <string>
    
     phones::phones(){}
    
    
    void phones::setPhone (string p )
    {
    phone=p;
    }
    string phones:: getphone()
    {
    return phone};
    
    void phones::setCost (string c )
    {
    cost=c;
    }
    string phones:: getcost()
    {
    return cost};
    
    void phones::setYear (int y )
    {
    year=y;
    }
    intphones:: getyear()
    };
    and this is the main cpp file.
    Code:
    //#include <iostream>
    //#include <"phones.h">
    #include <string>
    using namespace std;
    int main()
    {
        string phone;
    string cost;
    int year;
    
    phones object; //error here
    object.setphone("samsung");
    object.setcost("thousand");
    object.setyear("2010");
    return 0;
    }
    The error in the main file states that the object (phones object) is undeclared.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to #include your include file.

    Jim

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    //#include <"phones.h">
    That's a comment.

    Also, choose one (<) or the other (").

    Soma

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    hmm i did that already to see if it wouldchange anything. still saying that the class phones is undeclared..

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    when i put eihter <> or "" it send the error up to that line saying no such file or directory

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you have misplaced your files. Put them all into a single directory, then use "" to include them.
    Btw, indent your code properly, and stop using strings for everything. There are things known as integers, too. Use them.
    And don't use "using" directives in header files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Also consider:

    Code:
    void phones::setPhone (const std::string & p)
    {
        phone=p;
    }
    
    const std::string & phones::getphone() const 
    {
        return phone;
    } 
    
    void phones::setCost (const std::string &c )
    {
        cost = c;
    }
    
    const std::string & phones::getcost() const
    {
        return cost;
    }
    Consistency is your best friend. If you are going to use caps in the second word of all your methods then do this for every method. You have some that are all lowercase and others that use camelCase. If you do what others have told you in this thread your code should be compiling and running.

    Some basic style rules:
    • Set your compiler to use spaces instead of TABs for tabs. A tab can be 2,4,8,16, or any number of spaces and is not constant across editors. Never use tabs.
    • Indent first line after brace in a class declaration
    • Indent all variables in public, protected, and private sections (and keep public, protected, and privates in the same place)
    • Indent all conditional blocks of code after the starting brace.
    • Indent after the opening brace of a method
    • Ensure all opening and closing braces of methods, conditional blocks, classes, etc. line up. This makes the code easier to read especially in editors like MSVS that will snap to the opening brace from the closing brace and vice versa. (This is optional in conditionals if you elect to place the starting brace on the same line as the conditional, switch, class declaration, etc)
    • Indent after the opening brace of a switch() statement and indent all sections of code in each switch case. Some also use braces in switch statements.
    • Use spaces between operators. IE: int total=x+y+z; is harder to read than int total = x + y + z;


    There are other styles as well but almost everyone adheres to these. I personally indent my public, protected, and private keywords in a class but MSVS insists on placing them in the same column as the opening brace of a class declaration. As well I always use braces for one line conditionals, switch cases, and anywhere I want to or need to create scope. Some people place spaces after every comma. Personally I always place opening braces for any section on their own line. I never use them at the end of a line.

    You will find your own style but if you adhere to a few simple rules most people will never complain about minor differences.
    Last edited by VirtualAce; 02-29-2012 at 08:03 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by VirtualAce View Post
    [*]Set your compiler to use spaces instead of TABs for tabs. A tab can be 2,4,8,16, or any number of spaces and is not constant across editors. Never use tabs.
    I sincerely object! Never use spaces.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Please don't start a tabs vs spaces debate in this thread ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. class definition declaring ifstream object
    By rogster001 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2009, 10:47 AM
  3. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  4. _splitpath giving me errors!
    By Kixdemp in forum C Programming
    Replies: 9
    Last Post: 06-10-2006, 03:21 AM
  5. CreateFile() giving errors
    By neandrake in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2004, 10:29 PM

Tags for this Thread