![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
Rock(string); // This is one of the places I am having troubles string _____. Variable name of some sort should go there... |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2006
Posts: 3
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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; Also, your second constructor needs to assign the values to the "myName" variable, rather than using the initializer format. |
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Feb 2006
Posts: 3
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
Ah! My bad.
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|