c++ - How can I create a vector of virtual class? -


I'm new to C ++, so I decided to work on some small project to improve myself. I try to write a simple chess program with class unit, and class King inherited from the unit

  #include  gt; unit list, string sidepick, int expo, int ypos) {int i = 0; While (i! = 3) {int x = unit list [i] .getX (); Int Y = unit list [i] .getY (); String sidetemp = unlistlist [i] .getside (); If ((X == Expo) & amp; (Y == Ypos) & amp; (sidetemp == SidePic)) {return false; } And if ((X == Expo) & amp; (Y == Ypos) & amp; (sidetemp! = SidePic)) {// Unitalist [i] = Faucet; Back true; } I ++; } Back true; } Virtual Zero Drive (vector & lt; unit> unit list, int nextX, int nextY); }; Class King: Public unit (public: king (string sideplay, int expo, int ipso): unit (sideplay, expo, wipe) {} zero driving (vector & lt; unit & gt; unit list, int next x, int next y } {Int diffX = abs (nextX-currentX); Int diffY = abs (next Wi-turn Y); {(DiffX == 1)} (diffY == 1) (if (valid object (entity list, side, next number, next y)) {setpos (nextX, nextY);}}}}   

And here's my main:

  int main () unit> chessunit; chessunit.push_back (king ("white", Chessunit.push_back (chessunit.push_back (king ("black", 3,2)) chessunit.push_back (King ("white", 4,1)); if (chess [0]. Validmove (chess, " White ", 3,2)) {cout & lt;" hehe "& lt; & lt; endl;} chasunit [0]. Movovit (chess, 3,2); int kashmir = chess [0] GetY (); Court << code   

I get the LNK 2001 error I am: How can I fix that bug for my virtual method "invoices" unsolved exotic symbol? "post-text" itemprop = "text">

Fix your problem The easiest way to do is using pointers or smart pointers: vector gt; vector & lt; std :: shared_ptr & lt; unit & gt; & Gt; or vector & lt; Std :: unique_ptr & lt; Unit & gt; & Gt; vector & lt; Unit & gt; instead of code> (thanks @Rubenvab) and then add your kings like this:

  myVector.push_back (new king ...); // or myVector.push_back (std :: shared_ptr & lt; king & gt; (new king ...)); // or myVector.push_back (std :: unique_ptr & lt; king & gt; (new king ...));   

Why?

If you allocate objects to a virtual class (like unit unit ) and you want to specify the purpose of the implementation of that class, for example For:

  entity unit; Unit = king (...);   

Then you will get an error, or at least be in trouble, unless you provide a constructor for the unit which is the King takes an argument or a sufficient move operator because the reason is that if you try to assign an object that is the unit To unit , then find the compiler and / or run-time (back-end of its compiler) It would be difficult to see how the types and what to do if things are not fitted in accordance with the "memory" and how to deal with issues of memory layout.

Read more

  • To learn more about Points vs Smart Points, here's another
  • More information in trying to "squeeze" the object (which is said to be shredded).

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -