Private data question.

This is a discussion on Private data question. within the C++ Programming forums, part of the General Programming Boards category; I have a header file that contain this class in regards to my other thread "Array of class objects" When ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Private data question.

    I have a header file that contain this class in regards to my other thread "Array of class objects"

    When with the include and using namespace statements it will not allow me to declare a string in the private data. And i need to hold the name of a item from a file... Any ways around this?

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    	private:
                    string name;
    		int numberOfItems;
    		int cost;
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean by "it will not allow me to declare a string in the private data"?

    Using declarations are frowned on in headers; I would recommend just using std::string name; instead.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Yes i know but it won't notice the private string data when it compiles, gives errors.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Quote Originally Posted by tabstop View Post
    Using declarations are frowned on in headers; I would recommend just using std::string name; instead.
    I think you mean "using namespace" is frowned upon in headers.
    "Declarations" are a little too unambiguous.



    What errors?
    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.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by GCNDoug View Post
    Yes i know but it won't notice the private string data when it compiles, gives errors.
    It compiles just fine. Since it's private, you can't use it outside of the class (in main or wherever), only inside the class.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    1>Compiling...
    1>candymachinelab.cpp
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\dispensertype.h( 9) : error C2146: syntax error : missing ';' before identifier 'name'
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\dispensertype.h( 9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\dispensertype.h( 9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>Build log was saved at "file://c:\Users\Doug\Documents\Visual Studio 2008\Projects\lab5candy\lab5candy\Debug\BuildLog.h tm"
    1>lab5candy - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by GCNDoug View Post
    1>Compiling...
    1>candymachinelab.cpp
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\dispensertype.h( 9) : error C2146: syntax error : missing ';' before identifier 'name'
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\dispensertype.h( 9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\dispensertype.h( 9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>Build log was saved at "file://c:\Users\Doug\Documents\Visual Studio 2008\Projects\lab5candy\lab5candy\Debug\BuildLog.h tm"
    1>lab5candy - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    As I said, it compiles fine here just the way you have it posted. Is there a difference between the file you're trying to compile and the file you have posted in this thread?

    Also, maybe try std::string name; instead, if you haven't already.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Btw, iostream has no place in that header either.
    So you can remove that and the "using namespace" and use std::string instead.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Notice that the error you posted references line 9, but the code you posted has that variable on line 13. Did you remove the #includes and try to compile it? You can't do that. You need #include<string> and you need either using namespace std; or to change to std::string (there are other solutions as well). The errors you're getting is what happens when you fail to include the proper header and specify the proper namespace.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Code:
    #include <string>
    #include <fstream>
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    	private:
    		string name;
    		int numberOfItems;
    		int cost;
    };
    I don't understand the teacher i making us keep the classes in separate header files. It won't understand the i/o file stuff either...

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Also, put your class implementation in a separate cpp file.
    The reason the class definitions goes into header files of their own is because, if other parts of your program wants to use the class, they need that definition or the compiler will complain.

    And use std::ifstream and std::string, because it's in a header file.
    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.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    still no luck.

    Code:
    std::ifstream // or std::ifstream;
    std::string // or std::string;
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    	private:
    		string name;
    		int numberOfItems;
    		int cost;
    };

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You need the #include's. Put those back.

    Then, you need to specify the namespace. The using directive you had in there originally was fine, but it is bad practice for larger projects so an alternative was given.

    That alternative was to remove the using namespace std directive and instead always refer to string as std::string and ifstream as std::ifstream inside the actual code.


    >> I don't understand the teacher i making us keep the classes in separate header files.
    That's how it's done in C++ for several reasons. I don't understand why you are having a problem when your original code was working. Why are you changing it?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Code:
    #include <string>
    #include <iostream>
    
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(std::ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    	private:
    		std::string name;
    		int numberOfItems;
    		int cost;
    };
    Last edited by Elysia; 03-26-2008 at 01:58 PM.
    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.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Elysia, please add the #include's to your example to avoid confusion.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 2
    Last Post: 05-12-2003, 04:40 PM
  5. Post programs
    By GaPe in forum C# Programming
    Replies: 8
    Last Post: 05-12-2002, 11:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21