error C2079: 'room::goldC' uses undefined class 'goldContainer' - visual-c++

i'm trying to create a room which has a goldContainer
The goldContainer is defined in a separate .h file.
When i'm trying to compile it says
error C2079: 'room::goldC' uses undefined class 'goldContainer'
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:
^->goldContainer->player->roomContainer->room->|
|<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-< v

Let me show you the problem domain:
class Moon;
class Sun
{
void Rotate(Moon);
};
Now, you implement Sun::Rotate, without giving any class declaration (not forward declaration):
void Sun::Rotate(Moon m) // Error C2027
{
}
What you can do:
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.
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 before Sun::Rotate.

Related

Constructing a child object from parent object in C++

I am trying to construct a child class object from a base class object. I have tried the below code.
class A
{
public:
A();
A(A&& objectName) = default;
virtual void setint(int i);
virtual void getint();
int var;
};
class B: public A
{
public:
virtual void getint();
B(A&& objectName);
int j= 20;
};
A::A()
{
}
void A::setint(int i)
{
var = i;
}
void A::getint()
{
qDebug()<<"From A Var"<<var;
}
void B::getint()
{
qDebug()<<"From B j"<<j;
qDebug()<<"From B Var"<<var;
}
B::B(A&& objectName): A(std::move(objectName))
{
}
And in my Main.cpp I am doing this
#include <memory>
int main(int argc, char *argv[])
{
A *obj = new A();
obj->setint(10);
obj->getint();
A *obj1 = new B(std::move(*obj));
obj->getint();
obj1->getint();
return 0;
}
The result I get is
From A Var 10
From A Var 10
From B j 20
From B Var 10
My question is why am I getting the value of Var after A *obj1 = new B(std::move(*obj)); this line. I thought the object pointed by obj must have been destructed.
Let me copy paste from this answer: https://stackoverflow.com/a/15663912/512225
std::move doesn't move from the object. It just returns an rvalue reference whose referand is the object, making it possible to move from the object.
Anyway your code is terrible. I hope you know. If you don't, ask for a review.

When i am creating object ,compiler gives error C4430

I am getting these two errors
Error # 01:
error C2146: syntax error : missing ';' before identifier 'mainObj'
Error # 02
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
i have following classes
Main
Customer
CNode
when i create pointer object of class 'CNode' in class 'Customer' ,there is no error. But when i create non pointer object of Main class in Customer class
,compiler gives above two errors.
while 'Main' class have a main fuction.
please help me out to resolve these errors.
Main.h
#pragma once
#include <iostream>
#include<string>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include <sstream>
#include "Customer.h"
using namespace std;
class Main
{
int acc;
int total = 0;
int rtotal = 0;
int sum = 0;
public:
Main();
void design();
void welcome();
string passcin();
void login_des();
void admin_account();
void add_staff();
void delete_staff(int);
void search_staff(int);
void update_staff(int);
void room_display();
void room_book();
void checkout();
int main();
};
Customer.h
#pragma once
#include <iostream>
#include<string>
#include<fstream>
#include <sstream>
#include "Main.h"
#include "Node.h"
class Customer
{
CNode *head, *tail, * temp;
Main mainObj;
public:
Customer();
bool search(string, string);
void updateAccount(string, string);
CNode* inputNode();
void custAccount(string);
void createAccount();
void createAccount(CNode *);
void addCust();
void deleteCust(int sno);
void searchCust(int sno);
void updateCust(int sno);
void displayCust();
CNode* retriveAllCust();
};
Node.h
#pragma once
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
class CNode
{
string name, email, pass, roomNo, phoneNo, country, creditCardName, cardNo;
bool bookFlag;
CNode *prev, *next;
int length;
public:
CNode();
CNode(string n, string e, string ps, string r, string pn, string country, string creditName, string creditNo, bool bookFlag);
CNode(string n, string e, string ps, string pn, string country);
string getName();
string getPass();
CNode* getNextNode();
void display();
void addCust();
string retriveCust();
friend class Customer;
};
I have put a semi colon before mainObj
like
;Main mainObj;
error occors in Customer Class
in
Main mainObj;

string does not name a type in header file

i've seen alot of topics like this that said to simply change your variables like this:
#include <string>
void function(string name, int number);
to:
#include <string>
void function(std::string name, int number);
But i already done this but it isnt working, here is my code:
#ifndef PERSONNAGE_H_INCLUDED
#define PERSONNAGE_H_INCLUDED
class personnage {
public:
personnage();
void heal(int amount);
void attack(personnage &target);
void changeweapon(string name, int dmg);
void receivedmg(int amount);
bool isAlive();
private:
int m_vie;
string m_weapon;
int m_weaponDamage;
};
#endif // PERSONNAGE_H_INCLUDED

VC++ does not show output

I am new to VC++ and its been a few times now, and this is the third program that does not give output even after it is build succesfully.
#include <AFXWIN.H>
#include <math.h>
#define PI 3.1415926
#define SEGMENTS 500
class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP();
};
CMyApp myAPP;
BOOL CMyApp::InitInstance() {
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(SW_MAXIMIZE);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow() {
Create(NULL,"The Hello Application",WS_OVERLAPPEDWINDOW);
}
void CMainWindow::OnPaint() {
CRect rect;
int nWidth = rect.Width();
int nHeight = rect.Height();
CPaintDC dc (this);
CPoint aPoint[SEGMENTS];
for (int i =0; i < SEGMENTS; i++){
aPoint[i].x = ((i*nWidth)/SEGMENTS );
aPoint[i].y= (int)((nHeight/2)* (1-(sin((2*PI*i)/SEGMENTS))));
}
dc.Polyline(aPoint, SEGMENTS);
UpdateData(false);
}
The above program should give Sine curve as the output, except that I get a blank window. And I don't know why does it happen. If it helps, I am using VC++ 6.0
The problem is probably that the rectangle you use to get the width and height is not initialized. You have to get the rectangle from somewhere, see e.g. CWnd::GetClientRect.

Convert .Net ref (%) to native (&)

How can I convert a C++/CLI int %tmp to native C++ int &tmp?
void test(int %tmp)
{
// here I need int &tmp2 for another pure C++ function call
}
Neither of the existing answers properly handle in/out parameters, let alone any advanced use cases.
This should work for all cases where other_func does not keep the reference after it returns:
void test(int %tmp)
{
pin_ptr<int> pinned_tmp = &tmp;
other_func(*pinned_tmp);
}
Just tried this, works fine:
//in the C++ dll
void testFunc( int& n )
{
n = 5;
}
//in the CLI app
[DllImport( "my.dll", EntryPoint = "?exported_name_here",
CallingConvention = CallingConvention::StdCall )]
void TestFunc( int& );
void test( int% tmp )
{
int n;
TestFunc( n );
tmp = n;
}
void your_function(int *);
void your_function2(int &);
void test(int %tmp)
{
int tmp2;
your_function(&tmp2);
your_function2(tmp2);
tmp=tmp2;
}

Resources