0

I have a class named Creature with 3 protected variables: string name, int quantity and string type. I have two derived classes Phoenix and Basilisk.

I tried redefining some of the variables inside each derived class, so that it becomes that value for that derived class only, but I get these errors for each line I try redefine a variable.

Error   C2059   syntax error: '='   
Error   C2238   unexpected token(s) preceding ';'
class Creature {
protected:
    string name;
    int quantity;
    string type;
};

class Phoenix : public Creature {
    Creature::type = "phoenix";
};

class Basilisk : public Creature {
    Creature::type = "basilisk";
    Creature::quantity = 1;
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The base class should have a constructor that set the value of its members. Each derived class should have a constructor that uses the base class constructor to set them appropriately. – Pete Becker Apr 14 '22 at 18:55
  • @PeteBecker I'm just a bit confused because my assignment's prompt says to create two constructors in the base class, one with no parameters, and one "with two parameters with string data types for name and type of creature (set to “unknown”), and integer data type for quantity" and in the main code we're given Creature c(nameArr[0], 2) is called. Where do I initialize the type variable? Shouldn't it be 3 parameters? –  Apr 14 '22 at 19:12
  • 1
    The code in the question doesn't have any constructors in the base class. So begin by writing the two that the assignment calls for. If you can't get them to work, show the code you've written and ask about the problems you're having. – Pete Becker Apr 14 '22 at 19:55
  • At a minimum, you are looking at [something like this](https://godbolt.org/z/78ehb3K4a). Your instructions, as you've presented them, are mud and I can't demonstrate much more. That said, at the moment the question is pretty much, "I don't have a clue what I'm doing. Help!" Those are hard to answer. Usually it means the asker needs to spend a bit more time working through the examples in their text book in order to have enough knowledge to understand what the instructor is asking. – user4581301 Apr 14 '22 at 19:56
  • @PeteBecker The code is much longer and what I showed was only what I thought the issue was from which is why I didn't show the constructors I made. It's near the time for my lab class now so I will just wait until then to get a clarification on what the teacher is looking for. I appreciate the help :) –  Apr 14 '22 at 20:02
  • @user4581301 The instructions I sent are pretty much word for word. Unfortunately my teacher's English isn't that great so this isn't the first time the instructions have been confusing for students. We also don't really have a textbook, we just have really simple content/examples from lecture slides that don't include what I'm having trouble with. My teacher hasn't responded to my email so I thought to check on here if it was just my own syntax issue. –  Apr 14 '22 at 20:06
  • That sucks. Happened to me in a network programming class. The instructor was a certified genius and expert in his field, but his field was definitely NOT English. You're going to need a good book and teach yourself a lot if you want to make it through the class. Grab yourself [one of the beginner books listed here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Programming and Principles is a bit more up-to-date, but I found C++ primer easier to digest--until you hit stuff that didn't come true or is no longer correct. – user4581301 Apr 14 '22 at 20:19
  • There is a danger in supplying only what you think the problem is: If you don't know what the problem is, how can you be sure you supplied the right information? If you leave out important details by mistake, you'll get bad answers or the question closed as unanswerable. Too many of the later and you'll find Stack Overflow will start throttling your ability to ask questions. – user4581301 Apr 14 '22 at 20:23
  • @user4581301 I'll keep all of that in mind. Thank you very much! This was actually my first question asked here so I was bit hesitant to add too much content in my question. Also, that link looks super helpful! Thank you! –  Apr 14 '22 at 20:33
  • The key to a good example in stack overflow is complete. We like the example to be minimal, but if it doesn't accurately represent the problem, the answers might not be accurate. The standard request is for a [mre] (MRE). MRE is actually a distillation of several good debugging techniques that help isolate the problem, and once isolated you can often solve the problem yourself. If you build the MRE early in the question-asking process, most of your questions won't be asked. – user4581301 Apr 14 '22 at 20:42

1 Answers1

0

THis is not valid c++

class Phoenix : public Creature {
    Creature::type = "phoenix";
};

you need

class Creature {
protected:
    string name;
    int quantity;
    string type;
    Creature(string t) :type(t) {}
};
class Phoenix : public Creature {
    Phoenix() :Creature("phoenix") {};
    
};

NOTE: storing a type in a class heirarchy is considered a c++ code smell. Better to use behavior than to ask for a type. If its for display purposes then the canonical thing to do it to have a method called something like 'creatureType' thats implemented by the derived classes that returns a string.

pm100
  • 48,078
  • 23
  • 82
  • 145