GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Mar-2008, 01:46
GameBoy1984 GameBoy1984 is offline
New Member
 
Join Date: Mar 2008
Location: Western US
Posts: 5
GameBoy1984 is on a distinguished road

Trouble with user defined classes


I have written the following code for a header file, the class file, and a driver file:

CPP / C++ / C Code:
#include <string>

using namespace std;
#ifndef counterH
#define counterH

class counter
{
private:
	int count;
	
public:
	// plusCount function
	// Purpose: adds 1 to the current count
	// Parameters :
	// Returns:
	// Pre-conditions:
	// Post-conditionsl:
	void plusCount ( );
	
	// minusCount function
	// Purpose: subtract one from current count
	// Parameters :
	// Returns:
	// Pre-conditions:
	// Post-conditionsl:
	void minusCount ( );
	
	// resetCount function
	// Purpose: resets the current count to zero
	// Parameters :
	// Returns:
	// Pre-conditions:
	// Post-conditionsl:
	void resetCount ( );
	
	// giveCount function
	// Purpose: returns the current count to the program
	// Parameters :
	// Returns: the current count
	// Pre-conditions:
	// Post-conditionsl:
	int giveCount ( );
}
#endif
CPP / C++ / C Code:
#include "counter.h"

void counter::plusCount()
{
	count ++;
}
void counter::minusCount()
{
	count --;
}

void counter::resetCount()
{
	count = 0;
}

int counter::giveCount()
{
	return count;
}
this code is not complete, but the problem is with the class file.
CPP / C++ / C Code:
#include <iostream>
using namespace std;
#include "counter.h"

int main ()
{
	counter myCount = 0;
	int userInput;
	
	cout << "Max Andrus\nCS 1400 sec 002\nReza Senati\n------------\n";
	cout << "This program is a counter, Please choose one of the following options:\n";
	cout << "\n1 - add 1 to Counter\n2 - subtract 1 from counter\n3 - display contents of counter";
	cout << "\n4 - reset counter\n5 - exit program\n";
	
	
	system("PAUSE");
	return 0;
}

I get the following error messages:

Code:
--------------------Configuration: project - Debug-------------------- Compiling source file(s)... counter.cpp counter.cpp:15: error: new types may not be defined in a return type counter.cpp:15: error: two or more data types in declaration of `plusCount' counter.cpp:15: error: prototype for `counter counter:: plusCount()' does not match any in class `counter' counter.h:30: error: candidate is: void counter:: plusCount() counter.cpp:15: error: `counter counter:: plusCount()' and `void counter:: plusCount()' cannot be overloaded project.exe - 5 error(s), 0 warning(s)
  #2  
Old 24-Mar-2008, 07:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,506
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: trouble with user defined classes


Quote:
Originally Posted by GameBoy1984
...the following code for a header file,
.
.
counter.cpp:15: error: new types may not be defined in a return type
counter.cpp:15: error: two or more data types in declaration of `plusCount'
...

Put a semicolon after the closing brace of the class definition in counter.h

Regards,

Dave
Last edited by davekw7x : 24-Mar-2008 at 08:04.
  #3  
Old 24-Mar-2008, 11:17
GameBoy1984 GameBoy1984 is offline
New Member
 
Join Date: Mar 2008
Location: Western US
Posts: 5
GameBoy1984 is on a distinguished road

Re: Trouble with user defined classes


Ok now I get this error message:

Code:
--------------------Configuration: proj09mpa - Debug-------------------- Compiling source file(s)... counter.cpp driver.cpp driver.cpp: In function `int main()': driver.cpp:18: error: conversion from `int' to non-scalar type `Counter' requested driver.cpp:19: warning: unused variable `int userInput' proj09mpa.exe - 1 error(s), 1 warning(s)
  #4  
Old 24-Mar-2008, 11:51
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 864
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Trouble with user defined classes


You might want to check whatever material that you are using to learn about classes, and seek information about a 'constructor' and its purpose.

That error is complaining about this line:
CPP / C++ / C Code:
	counter myCount = 0;
...which is not how user-types are instantiated.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 24-Mar-2008, 11:56
GameBoy1984 GameBoy1984 is offline
New Member
 
Join Date: Mar 2008
Location: Western US
Posts: 5
GameBoy1984 is on a distinguished road

Re: Trouble with user defined classes


I just needed to use the "resetCount()" function to initialize the the object "myCount"
  #6  
Old 24-Mar-2008, 12:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,506
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Trouble with user defined classes


Quote:
Originally Posted by GameBoy1984
Ok now I get this error message:

Now, the error you reported in your first post might have confused anyone. But for this one:

Look (yes look) at the message:
Quote:
Originally Posted by GameBoy1984

Code:
driver.cpp: In function `int main()': driver.cpp:18: error: conversion from `int' to non-scalar type `Counter' requested

Now look at the line that it is complaining about:

CPP / C++ / C Code:
	counter myCount = 0;

myCount is not an integer data type or any other kind of data type that can be initialized with an integer value.

If you want to make sure that myCount has a counter value equal to zero when it is created, you can make a constructor that sets the object member count equal to zero.

Regards,

Dave

Footnote: I know that when you are just getting started, error messages from compilers can be intimidating. You have to get used to seeing and dealing with them. I mean, it's always OK to ask, but it might be quicker to try to puzzle it out on your own.


After all:

"No one was born knowing this stuff, you know."
---davekw7x
  #7  
Old 24-Mar-2008, 12:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,506
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Trouble with user defined classes


Quote:
Originally Posted by GameBoy1984
I just needed to use the "resetCount()" function to initialize the the object "myCount"

I think that, in general, it is not good program design to allow an object to be created in an unknown state. Use a constructor that sets the value. Default value of zero is probably a Good Thing for this particular class.

Regards,

Dave
 

Recent GIDBlogUpdates On The All New Toyota VIOS - Part III by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Candy machine simulation (m0g0l) cplus2x CPP / C++ Forum 1 07-Feb-2008 05:21
passing locally defined user type to function from function earachefl CPP / C++ Forum 1 01-Jul-2006 17:54
how to access User defined functions of my View class to MainFrm class? prabhakar157 MS Visual C++ / MFC Forum 1 17-May-2006 03:09
User defined headers davis Miscellaneous Programming Forum 6 16-Feb-2006 18:40
[C++]Structures and Classes redthestudent CPP / C++ Forum 12 31-Jul-2005 08:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.