47683

error C2079: 'room::goldC' uses undefined class 'goldContainer'

Question:

i'm trying to create a room which has a goldContainer <br /> The goldContainer is defined in a separate .h file. When i'm trying to compile it says

<blockquote>

error C2079: 'room::goldC' uses undefined class 'goldContainer'

</blockquote>

The class voor room:

#pragma once #include <SFML\Graphics.hpp> #include "screenSettings.h" #include "floorplanPatch.h" #include "floorplanPatchContainer.h" #include "enemyContainer.h" #include "goldContainer.h" class goldContainer; class room{ public: room(int themenr, floorplanPatchContainer &f); void draw(sf::RenderWindow &window); int getStartPoint(); int getEndPoint(); void addFloorplanPatch(int x, int y, int type, floorplanPatch *patch); bool isSolid(sf::Vector2f position); void addEnemy(); static const int FLOOR_TEXTURE1 = 0; static const int FLOOR_TEXTURE2 = 1; static const int FLOOR_TEXTURE3 = 2; static const int FLOOR_TEXTURE4 = 3; static const int WALL = 4; static const int OBSTACLE = 5; static const int COSMETIC = 6; int floorplan[xAs][yAs]; enemyContainer* getEnemyContainer(); void room::addEnemy(int health); private: enemyContainer ec; int startPoint = 1 + rand() % (yAs - 2); int endPoint = 1 + rand() % (yAs - 2); sf::RectangleShape rectangle{ sf::Vector2f{ tileSizeX, tileSizeY } }; sf::Texture wall; sf::Texture obstacle; sf::Texture floor1; sf::Texture floor2; sf::Texture floor3; sf::Texture floor4; sf::Texture cosmetic; void drawBackgroundTile(sf::RenderWindow &window, int i, int x, int y); goldContainer goldC; };

it has class goldContainer; on line 8 otherwise it generates error code 2146.

Could someone maybe explain how to solve this error and/or why this occurs.

#pragma once #include "gold.h" #include "sound.h" #include "player.h" class player; class room; class goldContainer{ public: goldContainer(); ~goldContainer(); void checkPickedUp(player &player); void draw(sf::RenderWindow &window); void addGold(int amount, sf::Vector2f position, sf::Vector2f size); void clearAllGold(); private: std::vector<gold* > goldDrops; sound goldPickup{ "sounds\\goldPickup.wav" }; };

I think it might be a circular depedency trough:<br />

^->goldContainer->player->roomContainer->room->| |<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-< v

Answer1:

Let me show you the problem domain:

class Moon; class Sun { void Rotate(Moon); };

Now, you implement Sun::Rotate, without giving any class declaration (<strong>not</strong> forward declaration):

void Sun::Rotate(Moon m) // Error C2027 { }

What you can do:

<ul><li>Ensure that before Sun::Rotate gets into compilation phase, Moon is declared (i.e. known to compiler by now). You need not to implement any method of Moon, just declare. </li> </ul>

Example:

class Moon; class Sun { void Rotate(Moon); }; // Let it come by now class Moon { public: void PleaseRotate(); }; // Moon is known void Sun::Rotate(Moon m) { m.PleaseRotate(); // It need not to be implemented by now. }

Note that Moon::PleaseRotate definition would be resolved by linker, and hence its implementation is not needed <em>before</em> Sun::Rotate.

Recommend

  • Why can't we satisfy F# static member constraints with type extensions?
  • Doing a matrix multiplication in reverse
  • Avoid duplicate functions for each member variable
  • Find edge that a rectangle is touching on another rectangle
  • friend declaration in protected section
  • Parsing pair of strings fails. Bad spirit x3 grammar
  • How do I clear the screen in C? [duplicate]
  • C++ Text File Reading
  • Link TR1 on Ubuntu?
  • reducers get Function not Object in redux, what's wrong with it?
  • Visual Studio 2010 - 2015 does not use ymm* registers for AVX optimization
  • MOC adding namespace to class names
  • ClipperLib Clip multiple squares with rectangle produces 1 result
  • Could not find the following Boost libraries: boost_asio
  • Objective-C – access extern const with a string containing its name? [duplicate]
  • Manage different base layouts in Angular2
  • Passing unspecialized template as a template parameter
  • springboot + webpack dev server, does not change localhost bundle file after rebuilding
  • Thrust filter by key value
  • Load 24 bit TGA
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • why my app based on boost::asio didn't accept new connection
  • Opaque reference instead of PImpl. Is it possible?
  • Delphi: Where is the shortcut that started the application? [duplicate]
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • How do I mock an exported typescript function in a jasmine test?
  • Invalid Date on validation Date of js
  • C: Incompatible pointer type initializing
  • Blackberry - Custom EditField Cursor
  • Body moving without any force applied? (Box2d)
  • req.body is undefined - nodejs
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • sending/ receiving email in Java
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Android Google Maps API OnLocationChanged only called once
  • Checking variable from a different class in C#