When i am creating object ,compiler gives error C4430 - visual-c++

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;

Related

Error invalid conversion from 'const char*' to 'char*'

I Expect to get the name which I sent to the "class stud" which has a class variable CHAR name
The issue appears specifically at this line this->name->set_name_send(OBJ.name->get_name());
and I get this error " -- invalid conversion from 'const char*' to 'char*' -- "
Any suggestions?
enter image description here
If I make the function set_name as a const and delete the pointers in the destructor code runs but return value is garbage and it has to be 0
Maybe the class *name doesn't have to be pointer ?
All pointer and in OOP
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
class CHAR
{
char *name;
public:
CHAR(char *mes = "\0") {(this)->name = new char[100]; strcpy((this)->name,mes);}
CHAR(const CHAR&OBJ) { (this)->name = new char[100]; strcpy((this)->name,OBJ.name); }
~CHAR(){cout<<"CHAR Destructor!"<<endl; delete name;}
void set_name(char *mes){(this)->name = new char[100]; strcpy((this)->name,mes);}
const char* get_name() const {return (this)->name;}
};
class stud
{
CHAR *name;
public:
stud(char *mes)
{
this->name->set_name(mes);
}
stud( stud&OBJ)
{
this->name->set_name(OBJ.name->get_name()); //!!!!!!!!!!!!!!! issue HERE!!
}
~stud(){cout<<"Destructor!"<<endl;}
const char* get_name() const {return this->name->get_name();}
};
int main()
{
stud S("John");
stud A(S);
cout<<"Name: "<<A.get_name()<<endl;
}

sizeof class inside a member function of that class

#include "stdafx.h"
#include<iostream>
using namespace std;
class Myclass
{
public:
void Displaysize()
{
cout<<sizeof(Myclass);
}
private:
int x, y, z;
};
int _tmain(int argc, _TCHAR* argv[])
{
Myclass Ob;
Ob.Displaysize();
return 0;
}
When I execute this I got the size of class as 12, which is correct, how compiler come to now the size of the class properly. I read in Geeks for Geeks as follows "If a non-static object is member then declaration of class is incomplete and compiler has no way to find out size of the objects of the class."
Could some one answer my query.

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

C++ Windows Form Application: Attempted to read or write protected memory (unmanaged class)

I'm trying to use Boost library in my C++ Windows Form Application and I always get an exception:
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I'm using Visual Studio 2012 and Boost version 1.57.0. Previously I used Boost version 1.56.0 but upgrading didn't solve my issue.
Here are the code:
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void main(cli::array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
TestUnmanaged::MyForm form;
Application::Run(%form);
}
MyForm.h
#pragma once
#include <iostream>
#include <map>
#include <sstream>
#include <cassert>
#include <stdio.h>
#include "ExternalProfileManager.h"
#define DEFAULT_PROFILE_NAME "profile.bin"
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "lib/edk.lib")
namespace TestUnmanaged {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
ExternalProfileManager profileManager;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
profileManager.load(DEFAULT_PROFILE_NAME);
std::vector<std::string> profileList;
profileManager.listProfile(profileList);
}
ExternalProfileManager.h
#ifndef EXTERNAL_PROFILE_MANAGER_H
#define EXTERNAL_PROFILE_MANAGER_H
#include <boost/serialization/string.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/base_object.hpp>
class ExternalProfileManager
{
ExternalProfileManager(const ExternalProfileManager&) {};
ExternalProfileManager& operator = (const ExternalProfileManager&) {};
protected:
std::map<std::string, std::string > _profiles;
typedef std::map<std::string, std::string >::iterator profileItr_t;
// Boost serialization support
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int /*file version */)
{
ar & _profiles;
}
public:
ExternalProfileManager();
virtual ~ExternalProfileManager();
virtual bool save(const std::string& location);
virtual bool load(const std::string& location);
virtual bool insertProfile(const std::string& name, const unsigned char* profileBuf, unsigned int bufSize);
virtual bool listProfile(std::vector<std::string>& profiles);
};
//BOOST_CLASS_EXPORT(ExternalProfileManager);
//BOOST_CLASS_TRACKING(ExternalProfileManager, boost::serialization::track_never);
#endif // EXTERNAL_PROFILE_MANAGER_H
ExternalProfileManager.cpp
#include <fstream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/regex.hpp>
#pragma warning(push)
#pragma warning(disable : 4267) // "conversion from size_t to unsigned int"
#pragma warning(disable : 4996)
#include <boost/archive/archive_exception.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#pragma warning(pop)
#include "ExternalProfileManager.h"
using namespace std;
namespace fs = boost::filesystem;
ExternalProfileManager::ExternalProfileManager()
{
}
ExternalProfileManager::~ExternalProfileManager()
{
}
bool ExternalProfileManager::save(const string& location)
{
ofstream ofs(location.c_str(), ios_base::binary);
if ( !ofs.is_open() ) return false;
try {
boost::archive::binary_oarchive oa(ofs);
oa << *this;
}
catch (boost::archive::archive_exception& )
{
return false;
}
return true;
}
bool ExternalProfileManager::load(const string& location)
{
ifstream ifs(location.c_str(), ios_base::binary);
if ( !ifs.is_open() ) return false;
try {
boost::archive::binary_iarchive ia(ifs);
ia >> *this;
}
catch (boost::archive::archive_exception& )
{
return false;
}
return true;
}
bool ExternalProfileManager::insertProfile(const string& name, const unsigned char* profileBuf, unsigned int bufSize)
{
assert(profileBuf);
// Replace our stored bytes with the contents of the buffer passed by the caller
string bytesIn(profileBuf, profileBuf+bufSize);
_profiles[name] = bytesIn;
return true;
}
bool ExternalProfileManager::listProfile(vector<string>& profiles)
{
profiles.clear();
for ( profileItr_t itr = _profiles.begin(); itr != _profiles.end(); ++itr ) {
profiles.push_back(itr->first);
}
return true;
}
The error occurred in ia >> *this; in ExternalProfileManager::load (thrown in file basic_archive.cpp). So calling profileManager.load(DEFAULT_PROFILE_NAME); from form constructor will trigger the exception.
Calling save will also trigger the same exception but other functions which have no this will work fine.
I tried creating a console application in VS 2012 and call ExternalProfileManager.h and it works perfectly (including save, load, and any other function). Here are the simple console application I created to test it:
Console.cpp
#include <iostream>
#include <map>
#include <sstream>
#include <cassert>
#include <stdio.h>
#include "ExternalProfileManager.h"
#define DEFAULT_PROFILE_NAME "profile.bin"
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "lib/edk.lib")
ExternalProfileManager profileManager;
int main(int argc, char** argv) {
profileManager.load(DEFAULT_PROFILE_NAME);
std::vector<std::string> profileList;
profileManager.listProfile(profileList);
std::cout << "Available profiles:" << std::endl;
for (size_t i=0; i < profileList.size(); i++) {
std::cout << i+1 << ". " << profileList.at(i);
if (i+1 < profileList.size()) {
std::cout << std::endl;
}
}
return true;
}
profile.bin is generated from calling save function in console application and contain serialized data generated by boost. I can provide the file if it is needed to solve this issue.
I have also tried to create a simple class wrapper but the exception still occurred.
WrapperExternalProfileManager.h
#ifndef WRAPPER_EXTERNAL_PROFILE_MANAGER_H
#define WRAPPER_EXTERNAL_PROFILE_MANAGER_H
#include <string>
#include <vector>
class WrapperExternalProfileManager
{
WrapperExternalProfileManager(const WrapperExternalProfileManager&) {};
WrapperExternalProfileManager& operator = (const WrapperExternalProfileManager&) {};
public:
WrapperExternalProfileManager();
virtual ~WrapperExternalProfileManager();
virtual bool save(const std::string& location);
virtual bool load(const std::string& location);
virtual bool insertProfile(const std::string& name, const unsigned char* profileBuf, unsigned int bufSize);
virtual bool listProfile(std::vector<std::string>& profiles);
};
#endif
WrapperExternalProfileManager.cpp
#include "WrapperExternalProfileManager.h"
#include "ExternalProfileManager.h"
using namespace std;
ExternalProfileManager profileManager;
WrapperExternalProfileManager::WrapperExternalProfileManager()
{
std::cout<<"Constructor WrapperExternalProfileManager"<<std::endl;
}
WrapperExternalProfileManager::~WrapperExternalProfileManager()
{
}
bool WrapperExternalProfileManager::save(const string& location)
{
return profileManager.save(location);
}
bool WrapperExternalProfileManager::load(const string& location)
{
return profileManager.load(location);
}
bool WrapperExternalProfileManager::insertProfile(const string& name, const unsigned char* profileBuf, unsigned int bufSize)
{
return profileManager.insertProfile(name, profileBuf, bufSize);
}
bool WrapperExternalProfileManager::listProfile(vector<string>& profiles)
{
return profileManager.listProfile(profiles);
}
save and load still trigger the exception but other functions work perfectly.
Here are some property of the application which might be helpful:
Linker -> System -> SubSystem: Windows (/SUBSYSTEM:WINDOWS)
General -> Common Language Runtime Support: Common Language Runtime Support (/clr)
I know I have done something incorrectly but I don't know which part. Any suggestion to solve this issue would be appreciated.
Thanks in advance
You're going to have to find the source of your Undefined Behaviour (use static analysis tools, heap checking and divide and conquer).
I've just built your code on VS2013 RTM, using a ultra-simple C# console application as the driver:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var f = new TestUnmanaged.MyForm();
f.ShowDialog();
}
}
}
This JustWorks(TM).
I created a profile.bin with 100 random profiles of varying length:
#if 1
for (int i = 0; i < 100; ++i)
{
std::vector<uint8_t> buf;
std::generate_n(back_inserter(buf), rand() % 1024, rand);
insertProfile("profile" + std::to_string(i), buf.data(), buf.size());
}
save(location);
#endif
And they are deserialized just fine.
Good luck.
Download the full project here http://downloads.sehe.nl/stackoverflow/q27032092.zip in case you want to fiddle with it (compare the details?)

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

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.

Resources