Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 6th, 2006, 8:20 PM   #1
paeck
Newbie
 
Join Date: Feb 2006
Posts: 3
Rep Power: 0 paeck is on a distinguished road
Problem with class constructor

Hello,
I have a programming project due tomorrow. I am having trouble with the constructor in my program. I keep getting C2460 errors in visual c++.
Here is the code. Thanks,
Paul

//Header file

/***************************************************************************
	Rock class declaration.
	Author: 
	Date: 2-4-06
	The Rock class 
***************************************************************************/

#include <string>

enum RockName {BASALT, DOLOMITE, GRANITE, GYPSUM, LIMESTONE, MARBLE, OBSIDIAN, 
			QUARTZITE, SANDSTONE, SHALE, ROCK_OVERFLOW};

class Rock{
	public:
		// Put declarations of things that should be available outside of 
		// class here -- in particular, prototypes of public function members.
		
		// Constructors
		Rock();
		Rock(string);  // This is one of the places I am having troubles

	private:
		// Put declarations of things that should not be available outside
		// the class here -- in particular, declarations of data members.
		RockName myName;	
};


 // Source file

/********************************************************************
Rock implementation file.
Author: Paul Eck
Date: 2-4-06
********************************************************************/

/* Default constructor for Rock class. This will create an object of 
class Rock and insert a default value.                            */

#include "Rock.h"

Rock::Rock()
: myName(BASALT){
}

Rock::Rock(string src){     // This is the other place.
	if (src == "BASALT")
		myName(BASALT);
	else if (src == "DOLOMITE")
		myName(DOLOMITE);
	else if (src == "GRANITE")
		myName(GRANITE);
	else if (src == "GYPSUM")
		myName(GYPSUM);
	else if (src == "LIMESTONE")
		myName(LIMESTONE);
	else if (src == "MARBLE")
		myName(MARBLE);
	else if (src == "OBSIDIAN")
		myName(OBSIDIAN);
	else if (src == "QUARTZITE")
		myName(QUARTZITE);
	else if (src == "SANDSTONE")
		myName(SANDSTONE);
	else if (src == "SHALE")
		myName(SHALE);
	else if (src == "ROCK_OVERFLOW")
		myName(ROCK_OVERFLOW);
}

Last edited by paeck; Feb 6th, 2006 at 8:35 PM.
paeck is offline   Reply With Quote
Old Feb 6th, 2006, 8:29 PM   #2
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
Rock(string); // This is one of the places I am having troubles

string _____. Variable name of some sort should go there...
sackarias is offline   Reply With Quote
Old Feb 6th, 2006, 8:29 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
When you join a community to seek help, you should read the forum's rules/FAQ and possibly a "How to Post...." thread. Had you done so, you would realize that you need to enclose your code in tags to preserve its formatting. You should also give key information such as OS and complete information about the problem. It's a good guess that you are on Windows and using MS C++ of one version of another, but that's because I happen to recognize that as an MS-looking error code. Now that I've fired up my compiler and looked at the documentation, I find this:
[quote]
'identifier1' : uses 'identifier2', which is being defined

The given class or structure (identifier2) was declared as a member of itself (identifier1).

Recursive definitions of classes and structures are not allowed.
[/code]
RockName is not a standard C/C++ type. Where have you defined it prior to its use?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 6th, 2006, 8:41 PM   #4
paeck
Newbie
 
Join Date: Feb 2006
Posts: 3
Rep Power: 0 paeck is on a distinguished road
Sorry about that. I edited it so it should look better. I added the variable name but that didn't help. I'm running Win XP SP1a and Visual Studio.net. Thanks for any help anyone can give.
paeck is offline   Reply With Quote
Old Feb 6th, 2006, 9:09 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
The c++ string class is declared in the "std" namespace.
You should put "std::" (without the quotes) in front of string.
    Rock(std::string);  // This is one of the places I was having troubles

Alternatively you can use a
using namespace std;
But it is not recommended to put that in your header files - it can lead to incompatibilities with other namespaces.

Also, your second constructor needs to assign the values to the "myName" variable, rather than using the initializer format.
The Dark is offline   Reply With Quote
Old Feb 6th, 2006, 9:26 PM   #6
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by sackarias
Rock(string); // This is one of the places I am having troubles

string _____. Variable name of some sort should go there...
For function prototypes you're not required to give the variable names, just the types.
Jason Isom is offline   Reply With Quote
Old Feb 6th, 2006, 9:28 PM   #7
paeck
Newbie
 
Join Date: Feb 2006
Posts: 3
Rep Power: 0 paeck is on a distinguished road
Thanks for the help. Sorry I didn't search harder before posting but I have to get this in tomorrow at 8:00 am for a class and I didn't get a chance to work on it cuz I got sick. I should have known that I was missing the namespace std. I am still getting used to multi-file programs. Thanks again.
paeck is offline   Reply With Quote
Old Feb 7th, 2006, 12:17 PM   #8
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
Ah! My bad.
sackarias is offline   Reply With Quote
Old Feb 7th, 2006, 12:53 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There's a thread in the lounge called "Please be careful."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC