Thread: Struct / Class Issues

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

    Struct / Class Issues

    Hello, I am new to working with structs and classes and seem to have run into a few issues. For the most part I think I have structs down but I am still have some trouble understand classes.

    I am currently working on a program that will create an employee database for five employees.

    I don't understand why I am getting compiler errors. It is saying that my variables in the bool function are undefined but they are private variables and I was under the impression that function accessing variables from within the same class could use those same variables. This only seems to be an issue with the bool function. Also if anyone could give me some direction as to how to take the program from here it would be greatly appreciated. Thanks!

    So far I have this set up.

    Code:
    #include <iostream>
    using namespace std;
    
    const int MAX = 5;
    
    struct nameType
    {
    	string first;
    	char mid;
    	string last;
    };
    
    struct dateType
    {
    	int month;
    	int day;
    	int year;
    
    };
    
    struct RecType
    {
    	nameType employee;
    	nameType supervisor;
    	dateType dob;
    	dateType starting;
    	float rate;
    	int id_Num;
    	int hours;
    	float gross;
    	float all_Gross;
    };
    
    
    class  ListType
    {
    	public:
    	 	ListType ( );
    		void Insert(RecType);
    		bool GetNext(RecType&);
    		void Reset( );
    	private:
    		RecType DB[MAX];
    		int current;
    		int noemp;
    };
    
    ListType::ListType( )
    {
    	current = 0;
    	noemp = 0;
    }
    void ListType::Insert(RecType rec) 
    {
    	DB[current] = rec;
    	current++;
    	noemp++;
    }
    bool GetNext::(RecType& rec)
    {
    	if (current == noemp ||  noemp == 0) 
    		return false;
    	else
    	{
    		rec = DB[current];
    		current++;
    	}
    }
    void Reset( )
    {
    	current = 0;
    }		
    
    
    int main()
    {
    
    }
    Last edited by GCNDoug; 02-26-2008 at 05:52 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Notice how
    Code:
    void ListType::Insert(RecType rec)
    suddenly became
    Code:
    bool GetNext(RecType& rec)
    Why do you think GetNext isn't seeing any class variables?

  3. #3
    coder
    Join Date
    Feb 2008
    Posts
    127
    also this line:
    Code:
    void Reset( );
    { ...
    has 2 errors
    Last edited by carlorfeo; 02-26-2008 at 03:19 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Look closely at the functions. You've defined them at global scope, not inside your class.
    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
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I made the changes you guys said except for the scope one which I don't understand. I'm still getting a lot of errors.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    bool ListType::GetNext(RecType& rec)
    {
    	if (current == noemp ||  noemp == 0) 
    		return false;
    	else
    	{
    		rec = DB[current];
    		current++;
    	}
    }
    void ListType::Reset( )
    {
    	current = 0;
    }
    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
    Join Date
    Apr 2007
    Posts
    133
    Alright! Thanks I finally got it to compile I just got this program to insert information for all these employees to work with structs. I'm not sure of where to do with the classes though. The classes and functions of the class were given to us by the instructor does anyone know where I can go to get some help or could someone lead me in the right direction? Classes are totally new to me, I've been reading the book but all it is talk about is telling us how to increment the minutes, hours, and seconds of a clock . I don't know this is all just very confusing to me.

  8. #8
    coder
    Join Date
    Feb 2008
    Posts
    127
    there is google man!
    search for class, c++, tutorial, object oriented programming or what else you need.
    http://www.cprogramming.com/tutorial/lesson12.html
    http://www.cplusplus.com/doc/tutorial/classes.html

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Obviously I've tried Google. I have not found any information that is more useful than the text book. I was wondering if you guys new of anything specifically or could help me understand the problem at hand. Thanks.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, long story short: an object (which is an instance of a class) is basically self-maintained. It exposes an interface for you, the programmer, to use. But the class will manage its own state, memory and all that stuff.
    Kind of like a car. You have the pedestals, the steering wheels. Yet when you use them, a lot of stuff is done internally. That stuff is the class's job and the steering wheel and the pedestals are the interface.
    (Btw, an instance refers to a new class, so if you define a variable somewhere of the class, then you've made a new instance.)

    So what you need to think is what classes are necessary? Should the class actually represent an employee for example?
    Then you need to think of what information or the interface you wish to expose. Member functions.
    Then you add necessary data and internal workings to make the class work. So you can set age, get age, set profession, etc.

    I'm not sure what you actually have to do.
    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.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    With that code I have posted I need to take the information for 5 employees using the class save them in an array and them print them out

    The input must be:

    First name
    Last name
    Mid initial
    DOB
    Pay Rate
    Hours
    Start Date
    Supervisor name

    I figured it out with structs I just need to do it now with classes and I'm not really understanding the functions that the teacher gave us.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The functions I see just work like a vector, an array. I'm not sure if you really need to use the code? Are you allowed to use std::vector instead?
    But as I mentioned, create an Employee class and expose an interface that allowed you to set the name of the employee, get the name, etc with all that info you need.

    For example, the class might need a SetFirstName function. The function must take a string as argument. The class must also be able to store the first name, just as in the struct.
    Good luck.
    Last edited by Elysia; 02-26-2008 at 07:51 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.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Those function were the ones he gave us I guess we don't need to use the structs... i dunno. So just make a seperate function within the class for each input?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I actually mean the classes... the ListType class.
    It's pretty poorly made and somewhat unsafe and it's not really necessary since std::vector can do all it does and more.

    But yeah, pretty much a separate function for each.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM